-
Notifications
You must be signed in to change notification settings - Fork 0
/
365.py
69 lines (54 loc) · 1.79 KB
/
365.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
Problem:
A quack is a data structure combining properties of both stacks and queues. It can be
viewed as a list of elements written left to right such that three operations are
possible:
push(x): add a new item x to the left end of the list
pop(): remove and return the item on the left end of the list
pull(): remove the item on the right end of the list.
Implement a quack using three stacks and O(1) additional memory, so that the amortized
time for any push, pop, or pull operation is O(1).
"""
from DataStructures.Stack import Stack
class Quack:
def __init__(self) -> None:
self.stack_1 = Stack()
self.stack_2 = Stack()
self.stack_3 = Stack()
self.elements = 0
def __len__(self) -> int:
return self.elements
def push(self, x: int) -> None:
self.stack_1.push(x)
self.stack_2.push(x)
self.elements += 1
def pop(self) -> int:
if self.elements == 0:
raise RuntimeWarning("Quack underflow")
if len(self.stack_2) == 0:
while not self.stack_3.is_empty():
self.stack_2.push(self.stack_3.pop())
self.elements -= 1
self.stack_2.pop()
return self.stack_1.pop()
def pull(self) -> int:
if self.elements == 0:
raise RuntimeWarning("Quack underflow")
if len(self.stack_3) == 0:
while not self.stack_2.is_empty():
self.stack_3.push(self.stack_2.pop())
self.elements -= 1
return self.stack_3.pop()
if __name__ == "__main__":
quack = Quack()
quack.push(1)
quack.push(2)
quack.push(3)
quack.push(4)
quack.push(5)
print(quack.pop())
print(quack.pull())
print(quack.pop())
print(quack.pull())
print(quack.pull())
print(f"Length: {len(quack)}")