algorithm/baekjoon

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

support_u 2023. 3. 5. 06:18

문제

 

10866번: 덱

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

www.acmicpc.net

 

코드

 

from collections import deque
import sys

input = sys.stdin.readline

n = int(input())
li = deque([])

for _ in range(n):
    s = input().split()

    if "push_front" == s[0]:
        li.appendleft(s[1])
    elif "push_back" == s[0]:
        li.append(s[1])
    elif "pop_front" == s[0]:
        if li:
            print(li.popleft())
        else:
            print(-1)
    elif "pop_back" == s[0]:
        if li:
            print(li.pop())
        else:
            print(-1)
    elif "size" == s[0]:
        print(len(li))
    elif "empty" == s[0]:
        if li:
            print(0)
        else:
            print(1)
    elif "front" == s[0]:
        if li:
            print(li[0])
        else:
            print(-1)
    else:
        if li:
            print(li[-1])
        else:
            print(-1)