지나가던 개발자

[Python] 백준 10828번(스택) 문제 풀이 본문

PS/Python

[Python] 백준 10828번(스택) 문제 풀이

KwonYongHyeon 2022. 12. 9. 20:09

 

import sys

stack = []
for i in range(int(sys.stdin.readline())):
    command = sys.stdin.readline()
    if "push" in command:
        stack.append(int(command.split()[1]))
        continue
    if command == "pop\n":
        try:
            print(stack.pop())
        except:
            print(-1)
    elif command == "size\n":
        print(len(stack))
    elif command == "empty\n":
        if stack == []:
            print(1)
        else:
            print(0)
    else:
        try:
            print(stack[-1])
        except:
            print(-1)
Comments