-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_225_stack_using_queues.py
More file actions
64 lines (53 loc) · 1.47 KB
/
test_225_stack_using_queues.py
File metadata and controls
64 lines (53 loc) · 1.47 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import unittest
"""
write down thoughts
in pop method, the queue is iterated to the end to get the last element, the second queue is acting as a space
to store the emptied queue.
"""
from collections import deque
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self._top = None
self.q1 = deque()
self.q2 = deque()
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self._top = x
self.q1.append(x)
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
while len(self.q1)>1:
self._top = self.q1.popleft()
self.q2.append(self._top)
res = self.q1.popleft()
self.q1, self.q2 = self.q2, self.q1
return res
def top(self) -> int:
"""
Get the top element.
"""
return self._top
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return False if len(self.q1)>0 else True
class TestSolution(unittest.TestCase):
def test1(self):
obj = MyStack()
obj.push(1)
obj.push(2)
self.assertEqual(2,obj.top())
self.assertEqual(2,obj.pop())
self.assertEqual(False,obj.empty())
self.assertEqual(1, obj.pop())
self.assertEqual(True, obj.empty())
if __name__ == '__main__':
unittest.main()