코딩테스트/백준

백준(BAEKJOON) 10866 덱 - Python

Moonsu99 2023. 11. 24. 17:56

https://www.acmicpc.net/problem/10866

 

10866번: 덱

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

www.acmicpc.net

 

 

 

풀이

import sys
from collections import deque

input = sys.stdin.readline  
n = int(input()) 
que = deque()

for i in range(n):
    command = input().split()

    #push_back
    if command[0] == 'push_back':
        que.append(int(command[1]))
    
    # push_front
    if command[0] == 'push_front':
        que.appendleft(int(command[1]))

    #pop_front
    elif command[0] == "pop_front":
        if len(que) == 0:
            print(-1)
        else:
            print(que.popleft())
    
    #pop_back
    elif command[0] == "pop_back":
        if len(que) == 0:
            print(-1)
        else:
            print(que.pop())
    
    #size
    elif command[0] == "size":
        print(len(que))
    
    #empty
    elif command[0] == "empty":
        if len(que) == 0:
            print(1)
        else:
            print(0)
    
    #front
    elif command[0] == "front":
        if len(que) == 0:
            print(-1)
        else:
            print(que[0])
    
    #back
    elif command[0] == "back":
        if len(que) == 0:
            print(-1)
        else:
            print(que[-1])