- push를 구현하기위해 append를 사용했고
- pop을 구현하기위해선 pop함수,
- size와 empty를 구현하기위해서는 len함수를 사용했다.
- 예외 사항들은 try, except 구문을 이용해 구현하였다
import sys
input = sys.stdin.readline # 시간단축을 위한 코드
N = int(input())
stack = []
for i in range(N):
command = list(input().split())
if command[0] == 'push':
stack.append(command[1])
elif command[0] == 'pop':
try:
print(stack.pop())
except:
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':
try:
print(stack[-1])
except:
print('-1')
'Python > 백준' 카테고리의 다른 글
[백준/Python] 15651. N과 M (3) (0) | 2022.10.31 |
---|---|
[백준/Python] 10773. 제로 (0) | 2022.10.28 |
[백준/Python] 9095. 1,2,3 더하기 (0) | 2022.10.24 |
[백준/Python] 15649. N과 M (1) | 2022.10.23 |
[백준/Python] 2468. 안전 영역 (0) | 2022.10.19 |