[백준/10866] 덱 (파이썬/python)
1️⃣ 문제
문제 링크 : https://www.acmicpc.net/problem/10866
2️⃣ 코드
import sys
from collections import deque
n=int(input())
deq = deque()
for i in range(n):
str = list(sys.stdin.readline().split())
if str[0]=='push_front':
deq.appendleft(str[1])
elif str[0]=='push_back':
deq.append(str[1])
elif str[0]=='pop_front':
if deq:
num = deq.popleft()
print(num)
else:
print(-1)
elif str[0]=='pop_back':
if deq:
num = deq.pop()
print(num)
else:
print(-1)
elif str[0]=='size':
print(len(deq))
elif str[0]=='empty':
if len(deq):
print(0)
else:
print(1)
elif str[0]=='front':
if deq:
print(deq[0])
else:
print(-1)
elif str[0]=='back':
if deq:
print(deq[-1])
else:
print(-1)
댓글남기기