forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_053.py
36 lines (30 loc) · 806 Bytes
/
problem_053.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Queue:
def __init__(self):
self.main_stack = list()
self.aux_stack = list()
def __repr__(self):
return str(self.main_stack)
def enqueue(self, val):
self.main_stack.append(val)
def dequeue(self):
if not self.main_stack:
return None
while self.main_stack:
self.aux_stack.append(self.main_stack.pop())
val = self.aux_stack.pop()
while self.aux_stack:
self.main_stack.append(self.aux_stack.pop())
return val
q = Queue()
q.enqueue(1)
assert q.main_stack == [1]
q.enqueue(2)
assert q.main_stack == [1, 2]
q.enqueue(3)
assert q.main_stack == [1, 2, 3]
val = q.dequeue()
assert val == 1
assert q.main_stack == [2, 3]
val = q.dequeue()
assert val == 2
assert q.main_stack == [3]