풀이
import sys
cnt = int(sys.stdin.readline())
stack = []
for i in range(cnt):
command = sys.stdin.readline().split()
if command[0] == "push":
stack.append(command[1])
elif command[0] == "pop":
if len(stack):
print(stack.pop())
else:
print(-1)
elif command[0] == "size":
print(len(stack))
elif command[0] == "empty":
if len(stack):
print(0)
else:
print(1)
elif command[0] == "top":
if len(stack):
print(stack[-1])
else:
print(-1)
C++에서는 stack 기본 함수로, push(), top() 등이 있었는데, 파이썬에는 없어서 순간 당황했다.
>> 파이썬에는 비슷하게 collections에 deque라는 것이 있다고 한다.
'Study > Algorithm' 카테고리의 다른 글
[프로그래머스] 주식가격 (Python) (0) | 2021.12.02 |
---|---|
[백준] 9663번 : N-Queen (Python) (0) | 2021.11.30 |
[백준] 5430번: AC (Python) (1) | 2021.11.29 |
[백준] 1920번: 수 찾기, 10815번: 숫자 카드 (Python) (0) | 2021.11.25 |
[백준] 2108번: 통계학 (Python) (0) | 2021.11.24 |