CodingTest/Baekjoon

[baekjoon] 백준 10866번(파이썬): 덱

JunJangE 2021. 9. 6. 20:50

문제

 

10866번: 덱

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

알고리즘

- 입력에 따라 명령을 수행한다.

코드

import sys
from collections import deque

n = int(sys.stdin.readline())
queue = deque()
for i in range(n):
    order = list(map(str, sys.stdin.readline().split()))

    if order[0] == "push_front":
        queue.appendleft(order[1])

    elif order[0] == "push_back":
        queue.append(order[1])

    elif order[0] == "pop_front":
        if queue:
            print(queue.popleft())
        else:
            print(-1)

    elif order[0] == "pop_back":
        if queue:
            print(queue.pop())
        else:
            print(-1)

    elif order[0] == "size":
        print(len(queue))

    elif order[0] == "empty":
        if queue:
            print(0)
        else:
            print(1)

    elif order[0] == "front":
        if queue:
            print(queue[0])
        else:
            print(-1)

    elif order[0] == "back":
        if queue:
            print(queue[-1])
        else:
            print(-1)

github

 

GitHub - junjange/CodingTest: 내가 푼 코딩 테스트 문제와 해결법

내가 푼 코딩 테스트 문제와 해결법. Contribute to junjange/CodingTest development by creating an account on GitHub.

github.com