-
Notifications
You must be signed in to change notification settings - Fork 17
/
stack.py
158 lines (121 loc) · 3.44 KB
/
stack.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
class StackOverflowError(Exception):
pass
class StackUnderflowError(Exception):
pass
class ArrayStack():
top = 0
def __init__(self, length):
self._stack_length = length
self._stack = [None] * length
def push(self, element):
if self.top < self._stack_length:
self._stack[self.top] = element
else:
raise StackOverflowError
self.top += 1
def pop(self):
if self.top == 0:
raise StackUnderflowError
else:
element = self._stack[self.top - 1]
self.top -= 1
return element
def stack_empty(self):
return self.top == 0
def __eq__(self, other):
return self._stack[:self.top] == other
def __repr__(self):
return str({"top": self.top, "stack": self._stack})
# Tests
array_stack = ArrayStack(7)
assert array_stack.stack_empty()
array_stack.push(15)
array_stack.push(6)
array_stack.push(2)
array_stack.push(9)
assert array_stack == [15, 6, 2, 9]
assert array_stack.top == 4
array_stack.push(17)
array_stack.push(3)
assert array_stack == [15, 6, 2, 9, 17, 3]
assert array_stack.top == 6
assert array_stack.pop() == 3
assert array_stack.top == 5
array_stack.push(51)
array_stack.push(15)
try:
array_stack.push(666)
raise Exception("We can't go there, stack overflow must be raised")
except StackOverflowError:
pass
array_stack.pop()
array_stack.pop()
array_stack.pop()
array_stack.pop()
array_stack.pop()
array_stack.pop()
array_stack.pop()
assert array_stack.stack_empty()
try:
array_stack.pop()
raise Exception("We can't go there, stack underflow must be raised")
except StackUnderflowError:
pass
# 10.1-1
array_stack = ArrayStack(6)
print(array_stack)
array_stack.push(4)
print(array_stack)
array_stack.push(1)
print(array_stack)
array_stack.pop()
print(array_stack)
array_stack.push(8)
print(array_stack)
array_stack.pop()
print(array_stack)
# 10.1-2
class SharedArrayStack():
top = 0
def __init__(self, array_free_positions, array):
self._array_free_positions = array_free_positions
self._array = array
self._array_taken_positions = ArrayStack(len(array))
def push(self, value):
if self._array_free_positions.top == 0:
raise StackOverflowError
else:
position = self._array_free_positions.pop()
self._array[position] = value
self._array_taken_positions.push(position)
def pop(self):
position = self._array_taken_positions.pop()
value = self._array[position]
self._array_free_positions.push(position)
return value
array = [None] * 7
array_free_positions = ArrayStack(len(array))
for pos in range(len(array), 0, -1):
array_free_positions.push(pos - 1)
shared_array_stack1 = SharedArrayStack(array_free_positions, array)
shared_array_stack2 = SharedArrayStack(array_free_positions, array)
shared_array_stack1.push(1)
shared_array_stack1.push(2)
shared_array_stack1.push(3)
shared_array_stack2.push(4)
shared_array_stack2.push(5)
shared_array_stack2.push(6)
shared_array_stack2.push(7)
try:
shared_array_stack2.push(8)
raise Exception("We can't go there, stack overflow must be raised")
except StackOverflowError:
pass
shared_array_stack1.pop()
shared_array_stack1.pop()
shared_array_stack1.pop()
try:
shared_array_stack1.pop()
raise Exception("We can't go there, stack underflow must be raised")
except StackUnderflowError:
pass