What I Learned/Algorithm Practice

[백준 - python] 10828번: 스택

Interrobang 2023. 7. 31. 19:38

문제 링크

 

10828번: 스택

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지

www.acmicpc.net

문제 풀이

from sys import stdin

input = stdin.readline

n = int(input())
stack = []

for _ in range(n):
    command = input().rstrip().split()
    
    match command[0]:
        case 'push':
            stack.append(int(command[1]))
        case 'pop':
            if len(stack) == 0:
                print(-1)
            else:
                print(stack.pop())
        case 'size':
            print(len(stack))
        case 'empty':
            if len(stack) == 0:
                print(1)
            else:
                print(0)
        case 'top':
            if len(stack) == 0:
                print(-1)
            else:
                print(stack[-1])

10828 스택

*key point: 파이썬 최신 문법 중 하나인 match-case문을 이용한다.