파이썬 백준 10866번
실버4
https://www.acmicpc.net/problem/10866
문제 보기
문제 풀기
각각의 텍스트 입력에 대해 if 문으로 다양한 deque 명령을 실행하였다.
덱 deque 사용을 연습하고 있어요.
코드 보기
import sys
inputdata = sys.stdin.readline
from collections import deque
N = int(inputdata().strip())
dq = deque()
for _ in range(N):
command = list(map(str, inputdata().split()))
if command[0] == 'push_front':
dq.append(command[1])
elif command[0] == 'push_back':
dq.appendleft(command[1])
elif command[0] == 'pop_front':
if len(dq) == 0:
print(-1)
else:
print(dq.pop())
elif command[0] == 'pop_back':
if len(dq) == 0:
print(-1)
else:
print(dq.popleft())
elif command[0] == 'size':
print(len(dq))
elif command[0] == 'empty':
if len(dq) == 0:
print(1)
else:
print(0)
elif command[0] == 'front':
if len(dq) == 0:
print(-1)
else:
print(dq[-1])
elif command[0] == 'back':
if len(dq) == 0:
print(-1)
else:
print(dq[0])
# 22
# front
# back
# pop_front
# pop_back
# push_front 1
# front
# pop_back
# push_back 2
# back
# pop_front
# push_front 10
# push_front 333
# front
# back
# pop_back
# pop_back
# push_back 20
# push_back 1234
# front
# back
# pop_back
# pop_back
# -1
# -1
# -1
# -1
# 1
# 1
# 2
# 2
# 333
# 10
# 10
# 333
# 20
# 1234
# 1234
# 20
추가 하기
for 문이 매번 돌 때마다 덱의 상태와 명령어 그리고 결과를 프린트해보았음.
deque([])
command: front
-1
deque([])
command: back
-1
deque([])
command: pop_front
-1
deque([])
command: pop_back
-1
deque([])
command: push_front
input: 1
deque(['1'])
command: front
1
deque(['1'])
command: pop_back
1
deque([])
command: push_back
input: 2
deque(['2'])
command: back
2
deque(['2'])
command: pop_front
2
deque([])
command: push_front
input: 10
deque(['10'])
command: push_front
input: 333
deque(['10', '333'])
command: front
333
deque(['10', '333'])
command: back
10
deque(['10', '333'])
command: pop_back
10
deque(['333'])
command: pop_back
333
deque([])
command: push_back
input: 20
deque(['20'])
command: push_back
input: 1234
deque(['1234', '20'])
command: front
20
deque(['1234', '20'])
command: back
1234
deque(['1234', '20'])
command: pop_back
1234
deque(['20'])
command: pop_back
20
'공부하기 > 백준' 카테고리의 다른 글
[Python] 백준 풀기 24416 - 알고리즘 수업, 피보나치 수 1 (0) | 2022.09.27 |
---|---|
[Python] 백준 풀기 14425 - 문자열 집합 (0) | 2022.09.26 |
[Python] 백준 풀기 25501 - 재귀의 귀재 (2) | 2022.09.23 |
[Python] 백준 풀기 10870 - 피보나치 수 5 (0) | 2022.09.22 |
[Python] 백준 풀기 4949 - 균형잡힌 세상 (0) | 2022.09.21 |