상세 컨텐츠

본문 제목

Boj 10828 스택 python

Algorithm/algorithm feedback

by 개복신 개발자 2021. 7. 19. 17:01

본문

728x90
반응형

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

 

10828번: 스택

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

www.acmicpc.net


내 풀이

import sys

input=sys.stdin.readline

class Stack:
    def __init__(self):
        self.list=[]

    def push(self, num):
        self.list.append(num)

    def pop(self):
        if self.list:
            pop_num=self.list[-1]
            self.list = self.list[:-1]
            return pop_num
        else:
            return -1

    def size(self):
        return len(self.list)

    def empty(self):
        if self.list:
            return 0
        else:
            return 1

    def top(self):
        if len(self.list)>0:
            return self.list[-1]
        else:
            return -1
        

num=int(input())
stack=Stack()
for i in range(num):
    input_split=input().split()

    if input_split[0]=="push":
        stack.push(input_split[1])
    elif input_split[0]=="pop":
        print(stack.pop())
    elif input_split[0]=="size":
        print(stack.size())
    elif input_split[0]=="empty":
        print(stack.empty())
    elif input_split[0]=="top":
        print(stack.top())

-input().split()--> 띄어쓰기 된 단어대로 분리하여 입력받음

push 1--> input_split=["push", "1"]

 

반응형

'Algorithm > algorithm feedback' 카테고리의 다른 글

Boj 큐2 python  (0) 2021.07.23
Boj 9012 괄호 python  (0) 2021.07.22
Boj 3079 입국심사 python  (0) 2021.07.16
Boj 1477 휴게소 세우기 python  (0) 2021.07.16
Boj 2512번 예산 python  (0) 2021.07.13

관련글 더보기

댓글 영역