Day Coding

ChoiDevv·2022년 12월 15일
0

문제

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

내가 쓴 코드

from collections import deque

def push(stack, number):
    stack.append(number)
    return stack

def pop(stack):
    if not stack:
        return -1
    return stack.pop()

def size(stack):
    return len(stack)

def empty(stack):
    if not stack:
        return 1
    return 0

def top(stack):
    if not stack:
        return -1
    return stack[len(stack) - 1]

stack = deque()
count = int(input())

for i in range(count):
    command = input().split()

    if command[0] == "push":
        push(stack, int(command[1]))
    
    if command[0] == "pop":
        print(pop(stack))
    
    if command[0] == "size":
        print(size(stack))
    
    if command[0] == "empty":
        print(empty(stack))
    
    if command[0] == "top":
        print(top(stack))

수정한 코드

# 스택

import sys
from collections import deque

def push(stack, number):
    stack.append(number)
    return stack

def pop(stack):
    if not stack:
        return -1
    return stack.pop()

def size(stack):
    return len(stack)

def empty(stack):
    if not stack:
        return 1
    return 0

def top(stack):
    if not stack:
        return -1
    return stack[len(stack) - 1]

stack = deque()
count = int(sys.stdin.readline())

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

    if command[0] == "push":
        push(stack, int(command[1]))
    
    if command[0] == "pop":
        print(pop(stack))
    
    if command[0] == "size":
        print(size(stack))
    
    if command[0] == "empty":
        print(empty(stack))
    
    if command[0] == "top":
        print(top(stack))

input()으로 입력하면 런타임 에러가 나서 sys의 stdin.readline() 메소드를 사용했다.

profile
기억보단 기록을

0개의 댓글