-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0232_Implement_Queue_Using_Stacks.py
49 lines (40 loc) · 1.26 KB
/
0232_Implement_Queue_Using_Stacks.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
37
38
39
40
41
42
43
44
45
46
47
48
49
from unittest import TestCase, main
class MyQueue:
def __init__(self):
self.enqueue = []
self.dequeue = []
def push(self, x: int) -> None:
self.enqueue.append(x)
def pop(self) -> int:
if not self.dequeue: self._move()
return self.dequeue.pop()
def peek(self) -> int:
if not self.dequeue: self._move()
return self.dequeue[-1]
def empty(self) -> bool:
return len(self.enqueue) <= 0 and len(self.dequeue) <= 0
def _move(self) -> None:
while self.enqueue:
self.dequeue.append(self.enqueue.pop())
class Test(TestCase):
def test_given_case(self):
queue = MyQueue()
queue.push(1)
queue.push(2)
self.assertEqual(queue.peek(), 1)
self.assertEqual(queue.pop(), 1)
self.assertFalse(queue.empty())
def test_move_property(self):
queue = MyQueue()
queue.push(1)
queue.push(2)
self.assertEqual(queue.peek(), 1)
queue.push(3)
queue.push(4)
self.assertEqual(queue.pop(), 1)
self.assertEqual(queue.pop(), 2)
self.assertEqual(queue.pop(), 3)
self.assertEqual(queue.pop(), 4)
self.assertTrue(queue.empty())
if __name__ == "__main__":
main()