## 문제
스택 구현하기
## CODE
This file contains 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 | |
input = lambda : sys.stdin.readline().rstrip() | |
def stackdef(n,usestack): | |
stack = usestack | |
if n[0] == 'push': | |
stack += [int(n[1])] | |
elif n[0] == 'pop': | |
if len(stack) == 0: | |
print(-1) | |
else: | |
print(stack[-1]) | |
stack = stack[:-1] | |
elif n[0] == 'size': | |
print(len(stack)) | |
elif n[0] == 'empty': | |
if len(stack) == 0: | |
print(1) | |
else: print(0) | |
elif n[0] == 'top': | |
if len(stack) == 0: | |
print(-1) | |
else: print(stack[-1]) | |
else: | |
print(stack) | |
return stack | |
stack = [] | |
for k in range(int(input())): | |
n = input().split() | |
stack = stackdef(n,stack) |
sys.stdin.readline() 을 사용할때 매번 개행문자와 EOF을 지우는 과정을 거쳤었는데
```python
import sys
input = lambda : sys.stdin.readline().rstrip()
```
이거면 깔끔하게 된다.
0 댓글