-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy path3_3_set_of_stacks.py
52 lines (43 loc) · 1.2 KB
/
3_3_set_of_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
50
51
52
#!/usr/bin/env python
"""
SetOfStacks should be composed of several stacks, and should create a new
stack one the previous one exceeds capacity. SetOfStacks push and pop methods
should behave identically to a single stack.
"""
class SetOfStacks(list):
def __init__(self, capacity=4):
self.stacks = []
self.last_stack = []
self.capacity = capacity
self.stacks.append(self.last_stack)
def __repr__(self):
return str(self.stacks)
def push(self, number):
last_stack = self.last_stack
if len(last_stack) is self.capacity:
last_stack = []
self.last_stack = last_stack
self.stacks.append(last_stack)
last_stack.append(number)
def pop(self):
last_stack = self.last_stack
number = last_stack.pop()
if len(last_stack) is 0:
self.stacks.pop()
self.last_stack = self.stacks[-1]
return number
def main():
stack = SetOfStacks()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
stack.push(5)
stack.push(6)
print stack
stack.pop()
stack.pop()
stack.pop()
print stack
if __name__ == '__main__':
main()