코딩 테스트(Python)/백준, 프로그래머스
10845 큐
5_hyun
2022. 2. 7. 19:30
반응형
https://www.acmicpc.net/problem/10845
10845번: 큐
첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지
www.acmicpc.net
내 풀이(맞음)
from collections import deque
class Queue:
def __init__(self, ary = deque()):
self.ary = ary
def push(self, ary, item):
ary.appendleft(item)
def pop(self, ary):
if ary:
t = ary.pop()
return t
else:
return -1
def size(self, ary):
return len(ary)
def empty(self, ary):
if len(ary) == 0:
return 1
else:
return 0
def front(self, ary):
if ary:
return ary[len(ary)-1]
else:
return -1
def back(self, ary):
if ary:
return ary[0]
else:
return -1
n = int(input())
a = deque()
q = Queue(a)
rst = []
for i in range(n):
x = input().split()
if x[0] == 'push':
q.push(a, x[1])
elif x[0] == 'pop':
rst.append(q.pop(a))
elif x[0] == 'size':
rst.append(q.size(a))
elif x[0] == 'empty':
rst.append(q.empty(a))
elif x[0] == 'front':
rst.append(q.front(a))
elif x[0] == 'back':
rst.append(q.back(a))
for i in rst:
print(i)
init에서 ary를 덱으로 생성합니다. 그리고 큐는 선입선출이라 push에서 왼쪽에 추가합니다. 그리고 pop에서 오른쪽부터 삭제합니다. size는 len으로 구하고 empty는 있으면 1 비었으면 0을 출력합니다. front는 맨 오른쪽 값, back는 맨 왼쪽 값을 출력합니다.
반응형