기본 콘텐츠로 건너뛰기

10828 : 스택 [Python]

Stack = []

def push(num):
    Stack.append(int(num))
def pop():
    if len(Stack) > 0:
        print(Stack.pop())
    else:
        print(-1)
def size():
    print(len(Stack))
def empty():
    if len(Stack) == 0:
        print(1)
    else:
        print(0)
def top():
    if len(Stack) > 0:
        print(Stack[len(Stack) - 1])
    else:
        print(-1)

TestCase = int(input())

while TestCase > 0:
    Command = input()

    if Command == 'top':
        top()
    elif Command == 'pop':
        pop()
    elif Command == 'empty':
        empty()
    elif Command == 'size':
        size()
    else:
        push(Command[5:])

    TestCase -= 1

댓글