This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import sys | |
| n = int(sys.stdin.readline().rstrip()) | |
| stack = [] | |
| def push(): | |
| li = list(command.split()) | |
| stack.append(li[1]) | |
| def pop(): | |
| if len(stack) == 0: | |
| print(-1) | |
| else: | |
| k = stack.pop() | |
| print(k) | |
| def empty(): | |
| if len(stack) == 0: | |
| print(1) | |
| else: | |
| print(0) | |
| def top(): | |
| if len(stack) == 0: | |
| print(-1) | |
| else: | |
| print(stack[len(stack) - 1]) | |
| for i in range(n): | |
| command = sys.stdin.readline().rstrip() | |
| if 'push' in command: | |
| push() | |
| elif 'pop' in command: | |
| pop() | |
| elif 'size' in command: | |
| print(len(stack)) | |
| elif 'empty' in command: | |
| empty() | |
| elif 'top' in command: | |
| top() |
'코딩테스트 준비' 카테고리의 다른 글
| 정렬 알고리즘 (0) | 2021.02.10 |
|---|---|
| 백준 9095번 1, 2, 3 더하기 (0) | 2021.02.08 |
| 백준 1924번 2007년 (0) | 2021.02.06 |
| 백준 4673번 셀프넘버 (0) | 2021.02.06 |
| 백준 1065번 한수 (0) | 2021.02.06 |