-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdailycoding053.py
More file actions
60 lines (41 loc) · 1.4 KB
/
dailycoding053.py
File metadata and controls
60 lines (41 loc) · 1.4 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
"""
This problem was asked by Apple.
Implement a queue using two stacks. Recall that a queue is a FIFO
(first-in, first-out) data structure with the following methods:
enqueue, which inserts an element into the queue, and dequeue, which removes it.
Solution:
Continually push elements to the first stack for the enqueue operation, and for the
dequeue operation, flip all elements in the stack onto the second stack to retrieve
the first in element. This works even when enqueuing new elements onto stack one.
"""
class Queue:
def __init__(self):
self.s1 = list()
self.s2 = list()
def enqueue(self, elem):
self.s1.append(elem)
def dequeue(self):
if len(self.s2) == 0:
while len(self.s1) > 0:
self.s2.append(self.s1.pop())
return self.s2.pop()
def main():
q = Queue()
# add test elements
for i in range(10):
q.enqueue(i)
# dequeue first 5 elements
for i in range(5):
assert q.dequeue() == i, print("Failed")
# add three elements
for i in range(3):
q.enqueue(i + 3)
# check remaining 5 elements are properly dequeued
for i in range(5):
assert q.dequeue() == i + 5, print("Failed")
# check remaining elements dequeued are correct
for i in range(3):
assert q.dequeue() == i + 3, print("Failed")
print("Passed")
if __name__ == '__main__':
main()