Solution
import collections
import sys
from typing import List
def solution(commands: List[List[str]]) -> List[int]:
answer = []
dq = collections.deque()
for command in commands:
x1, x2 = make_x1_x2(command)
if x1 == "push_front":
dq.appendleft(x2)
continue
if x1 == "push_back":
dq.append(x2)
continue
if x1 == "pop_front":
if not dq:
answer.append(-1)
continue
answer.append(dq.popleft())
continue
if x1 == "pop_back":
if not dq:
answer.append(-1)
continue
answer.append(dq.pop())
continue
if x1 == 'size':
answer.append(len(dq))
continue
if x1 == 'empty':
if not dq:
answer.append(1)
continue
answer.append(0)
continue
if x1 == 'front':
if not dq:
answer.append(-1)
continue
answer.append(dq[0])
continue
if x1 == 'back':
if not dq:
answer.append(-1)
continue
answer.append(dq[-1])
continue
return answer
def make_x1_x2(command):
x1, x2 = '', ''
if len(command) == 2:
x1, x2 = command[0], command[1]
else:
x1 = command[0]
return x1, x2
N = int(input())
array = []
for _ in range(N):
array.append(sys.stdin.readline().split())
print(*solution(array), sep='\n')
Leave a comment