학과공부/자료구조

[백준] 10866번 - 덱 (By Python)

tlsrhksdn1 2022. 7. 24. 20:27

 

풀이

 

import sys
N=int(sys.stdin.readline())
deque=[]

for i in range(N):
    words=sys.stdin.readline().split()
    
    if words[0]=='push_front':
        deque.insert(0,words[1])
    elif words[0]=='push_back':
        deque.append(words[1])
    elif words[0]=='pop_front':
        if deque:
            print(deque.pop(0))
        else:
            print(-1)
    elif words[0]=='pop_back':
        if deque:
            print(deque.pop())
        else:
            print(-1)
    elif words[0]=='size':
        print(len(deque))
    elif words[0]=='empty':
        if deque:
            print(0)
        else:
            print(1)
    elif words[0]=='front':
        if deque:
            print(deque[0])
        else:
            print(-1)
    elif words[0]=='back':
        if deque:
            print(deque[-1])
        else:
            print(-1)