반응형
import sys
class Dequeue:
def __init__(self):
self.items = []
def push_front(self, item):
self.items.insert(0, item)
def push_back(self, item):
self.items.append(item)
def pop_front(self):
if len(self.items) == 0:
return -1
else:
tmp = self.items.pop(0)
return tmp
def pop_back(self):
if len(self.items) == 0:
return -1
else:
tmp = self.items.pop()
return tmp
def size(self):
return len(self.items)
def empty(self):
if len(self.items) == 0:
return 1
else:
return 0
def front(self):
if self.items:
return self.items[0]
else:
return -1
def back(self):
if self.items:
return self.items[-1]
else:
return -1
n = int(sys.stdin.readline())
q = Dequeue()
for _ in range(n):
command = list(map(str, sys.stdin.readline().split()))
if command[0] == "push_front":
q.push_front(command[1])
elif command[0] == "push_back":
q.push_back(command[1])
elif command[0] == "pop_front":
print(q.pop_front())
elif command[0] == "pop_back":
print(q.pop_back())
elif command[0] == "size":
print(q.size())
elif command[0] == "empty":
print(q.empty())
elif command[0] == "front":
print(q.front())
elif command[0] == "back":
print(q.back())
'Algorithm > BaekJoon' 카테고리의 다른 글
[Python][정렬] 백준 10825 (리스트 컴프리헨션 사용) (1) | 2024.07.13 |
---|