-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.py
More file actions
124 lines (105 loc) · 3.28 KB
/
ArrayStack.py
File metadata and controls
124 lines (105 loc) · 3.28 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
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
import numpy as np
from Interfaces import Stack
from Interfaces import List
class ArrayStack(Stack, List):
'''
ArrayStack: Implementation of the Stack interface based on Arrays.
All the @abstractemthods should be implemented.
An instance of ArrayStack has access to all the methods in ArrayStack and
all the methods of the base class (Stack). When executing a method, it executes
the method of ArrayStack, if it does not exists, it executes the method in the
Base class (Stack).
For exmaple,
s = ArrayStack()
print(s)
print(len(s))
'''
def __init__(self):
self.a = self.new_array(1)
self.n = 0
def new_array(self, n: int) -> np.array:
return np.zeros(n, object)
def resize(self):
'''
Resize the array
'''
b = self.new_array(max(1, 2*self.n))
for i in range(self.n):
b[i] = self.a[i]
self.a = b
def get(self, i : int) -> object:
if i < 0 or i >= self.n:
raise IndexError()
return self.a[i]
def set(self, i : int, x : object) -> object:
if i < 0 or i >= self.n:
raise IndexError()
y = self.a[i]
self.a[i] = x
return y
def add(self, i: int, x : object) :
'''
shift all j > i one position to the right
and add element x in position i
'''
if i < 0 or i > self.n:
raise Exception
if len(self.a) == self.n:
self.resize()
for j in range(self.n-1, i-1, -1):
self.a[j+1] = self.a[j]
self.a[i] = x
self.n += 1
def remove(self, i : int) -> object:
'''
remove element i and shift all j > i one
position to the left
'''
if i < 0 or i >= self.n:
raise IndexError()
x = self.a[i]
for j in range(i, self.n, -1):
self.a[j] = self.a[j+1]
self.n -= 1
if len(self.a) > 3*self.n:
self.resize()
return x
def push(self, x : object) :
self.add(self.n, x)
def pop(self) -> object :
return self.remove(self.n-1)
def size(self) :
'''
size: Returns the size of the stack
Return: an integer greater or equal to zero representing the number
of elements in the stack
'''
return self.n
def __str__(self) -> str:
'''
__str__: Returns the content of the string using print(s)
where s is an instance of the ArrayStack
Return: String with the content of the stack
'''
s = "["
for i in range(0, self.n):
s += "%r" % self.a[i]
if i < self.n-1:
s += ","
return s + "]"
def __iter__(self):
'''
Initialize the iterator. It is to be use in for loop
'''
self.iterator = 0
return self
def __next__(self):
'''
Move to the next item. It is to be use in for loop
'''
if self.iterator < self.n:
x = self.a[self.iterator]
self.iterator += 1
else:
raise StopIteration()
return x