
소스 코드
import sys
def push(n):
stack.append(n)
def pop(): #stack의 길이가 0인데 pop을 할경우 -1
if(len(stack) == 0):
return -1
else:
return stack.pop()
def size():
return len(stack)
def empty(): #stack이 비었으면 0 아니면 1
return 0 if stack else 1
def top():
return stack[-1] if stack else -1
stack = []
#rstrip ()은 문자열의 지정된 문자열의 끝을 삭제
n = int(sys.stdin.readline().rstrip())
for i in range(n):
inputSplit = sys.stdin.readline().rstrip().split() # 문자열을 나눔 : 명령어 분리하기 위함
order = inputSplit[0] # 입력해야하는 명령어
if(order == "push"):
push(inputSplit[1])
elif(order == "pop"):
print(pop())
elif(order == "size"):
print(size())
elif(order == "empty"):
print(empty())
elif(order == "top"):
print(top())
출력 결과
