본문 바로가기
Algorithm

[알고리즘] 스택 백준 10828 (Python)

by 가영리 2022. 1. 14.
728x90

 

코드

import sys

n = int(sys.stdin.readline())

stack = []

for i in range(n):
  command = sys.stdin.readline().split()

  if command[0] == 'push':
    stack.append(command[1])
    
  elif command[0] == 'pop':
    if len(stack) == 0:
      print(-1)
    else:
      print(stack.pop())

  elif command[0] == 'size':
    print(len(stack))

  elif command[0] == 'empty':
    if(len(stack)==0):
      print(1)
    else :
      print(0)
      
  elif command[0] == 'top':
    if len(stack) == 0:
      print(-1)
    else:
      print(stack[-1])

 

입출력