https://www.acmicpc.net/problem/10866
10866번: 덱
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
from collections import deque
import sys
order_count = int(input())
queue = deque()
for _ in range(order_count):
order = sys.stdin.readline().strip()
if order.startswith("push_f"):
queue.appendleft(order.split()[1])
elif order.startswith("push_b"):
queue.append(order.split()[1])
elif order.startswith("pop_f"):
print(queue.popleft() if queue else -1)
elif order.startswith("pop_b"):
print(queue.pop() if queue else -1)
elif order.startswith("s"):
print(len(queue))
elif order.startswith("e"):
print(0 if queue else 1)
elif order.startswith("f"):
print(queue[0] if queue else -1)
else:
print(queue[-1] if queue else -1)
collections 모듈의 deque 객체(큐)를 사용할 수 있으면 쉬운 문제이다.
'알고리즘 > 백준-파이썬' 카테고리의 다른 글
[백준] 10799번 (python 파이썬) (0) | 2022.04.06 |
---|---|
[백준] 17413번 (python 파이썬) (0) | 2022.04.06 |
[백준] 1158번 (python 파이썬) (0) | 2022.04.05 |
[백준] 10845번 (python 파이썬) (0) | 2022.04.05 |
[백준] 1406번 (python 파이썬) (0) | 2022.04.05 |