Coding Test

[Python] 백준 10828 - 스택

AlienCoder 2024. 11. 8. 14:28
반응형

문제

https://www.acmicpc.net/problem/10828

 

해설

스택을 구현하는 문제였다.

 

def solution(command):
    stack = []
    for c in command:
        if c[0] == "push":
            stack.append(c[1])
        elif c[0] == "pop":
            print("-1" if len(stack) == 0 else stack.pop())
        elif c[0] == "size":
            print(len(stack))
        elif c[0] == "empty":
            print("1" if len(stack) == 0 else "0")
        elif c[0] == "top":
            print("-1" if len(stack) == 0 else stack[-1])


n = int(input())
commands = [input().split(" ") for _ in range(n)]
solution(commands)
반응형