File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed
learntosurf/Data Structures Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ import sys
2+ input = sys .stdin .read # 한 번에 모든 입력을 읽어옴
3+ output = sys .stdout .write
4+
5+ def stack_operations (N , data ):
6+ stack = []
7+ results = []
8+
9+ for i in range (1 , N + 1 ):
10+ command = data [i ].split () # 명령어를 공백을 기준으로 나눠서 리스트로 저장
11+
12+ if command [0 ] == '1' :
13+ X = int (command [1 ])
14+ stack .append (X )
15+ elif command [0 ] == '2' :
16+ if stack :
17+ results .append (str (stack .pop ()))
18+ else :
19+ results .append ('-1' )
20+ elif command [0 ] == '3' :
21+ results .append (str (len (stack )))
22+ elif command [0 ] == '4' :
23+ results .append ('1' if not stack else '0' )
24+ elif command [0 ] == '5' :
25+ if stack :
26+ results .append (str (stack [- 1 ]))
27+ else :
28+ results .append ('-1' )
29+
30+ output ("\n " .join (results ) + "\n " )
31+
32+ data = input ().splitlines () # 입력을 줄 단위로 나눠서 리스트로 저장
33+ N = int (data [0 ])
34+ stack_operations (N , data )
You can’t perform that action at this time.
0 commit comments