반응형
from collections import deque
import sys
n = int(input())
queue = deque([])
for _ in range(n):
cmd = list(sys.stdin.readline().rstrip().split())
if len(cmd) == 2:
if cmd[0] == 'push':
queue.append(cmd[1])
else:
if queue:
if cmd[0] == 'pop':
print(queue.popleft())
elif cmd[0] == 'size':
print(len(queue))
elif cmd[0] == 'empty':
print(0)
elif cmd[0] == 'front':
print(queue[0])
else:
print(queue[-1])
else:
if cmd[0] == 'empty':
print(1)
elif cmd[0] == 'size':
print(0)
else:
print(-1)
중복된 코드를 많이 줄였다!!!!!!!!!
from collections import deque
import sys
N = int(input())
q = deque()
for i in range(N):
cmd = list(sys.stdin.readline().split())
if cmd[0] == "push":
q.append(cmd[1])
elif cmd[0] == "pop":
if len(q) == 0:
print(-1)
else:
print(q.popleft())
elif cmd[0] == "size":
print(len(q))
elif cmd[0] == "empty":
if len(q) == 0:
print(1)
else:
print(0)
elif cmd[0] == "front":
if len(q) == 0:
print(-1)
else:
print(q[0])
elif cmd[0] == "back":
if len(q) == 0:
print(-1)
else:
print(q[-1])
반응형
'취준 > 코딩테스트' 카테고리의 다른 글
백준 11866 (python): 요세푸스 문제 0 (0) | 2024.06.24 |
---|---|
백준 2164 (python): 카드2 (0) | 2024.06.24 |
백준 12789 (python): 도키도키 간식 드리미 (0) | 2024.06.21 |
백준 4949 (python): 균형잡힌 세상 (0) | 2024.06.19 |
백준 9012 (python): 괄호 (0) | 2024.06.19 |