-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththree_stacks_with_array.py
51 lines (40 loc) · 1.38 KB
/
three_stacks_with_array.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
"""
3.1 Three in One: Describe how you could use a single array to implement three stacks.
"""
class MultiStack(object):
def __init__(self, noOfStack):
self.value = [None] * noOfStack * 10
self.size = [None] * noOfStack
self.noOfStack = noOfStack
def push(self, stackNum, value):
if stackNum >= (self.noOfStack):
raise IndexError("Invalid stack selected")
if self.size[stackNum] and self.size[stackNum] >= len(self.value):
# TODO: Increase the size of the stack in this case
raise AssertionError("Size of stack exceeded!")
if self.size[stackNum] is None:
index = stackNum
else:
index = self.size[stackNum] + self.noOfStack
self.size[stackNum] = index
self.value[index] = value
def show(self):
print(self.value)
def pop(self, stackNum):
if stackNum >= (self.noOfStack):
raise IndexError("Invalid stack selected")
value = self.value[self.size[stackNum]]
self.value[self.size[stackNum]] = None
self.size[stackNum] -= self.noOfStack
return value
if __name__ == "__main__":
stack = MultiStack(3)
for idx in range(3):
stack.push(idx, 1)
stack.push(idx, 2)
stack.push(idx, 3)
stack.show()
stack.pop(0)
stack.show()
stack.push(0, 3)
stack.show()