-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamic_array.py
More file actions
180 lines (148 loc) · 4.71 KB
/
dynamic_array.py
File metadata and controls
180 lines (148 loc) · 4.71 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# DynamicArray: An array that grows to accommodate new elements.
# Matthew Dyer
import numpy as np
class DynamicArray:
def __init__(self):
self.capacity = 10
self.index = 0
self.next_index = 0
self.length = 0
self.data = np.empty(self.capacity, dtype='O')
def __len__(self):
return self.length
def __iter__(self):
return self
def __next__(self):
if self.length == 0:
raise StopIteration
self.index += 1
return self.data[self.index]
def __getitem__(self, index):
if isinstance(index, int):
if index >= 0:
if index < self.length:
return self.data[index]
else:
raise IndexError
else:
raise IndexError
else:
raise TypeError
def __setitem__(self, index, value):
if isinstance(index, int):
if index == self.capacity:
self.data.resize(self.capacity*2)
self.capacity = self.capacity*2
self.data[index] = value
def __delitem__(self, index):
self[index] = None
# pass
# def __contains__(self, item):
# pass
def is_empty(self):
if self.length == 0:
return True
def append(self, value):
self[self.length] = value
self.length += 1
self.next_index += 1
def clear(self):
for i in range(self.length):
del self[i]
self.length = 0
def pop(self):
if self.length == 0:
raise IndexError
else:
last = self[self.length - 1]
del self[self.length - 1]
self.length -= 1
self.next_index -= 1
return last
def delete(self, index):
if self.length == 0:
raise IndexError
else:
if isinstance(index, int):
if index >= 0:
if index < self.length:
del self[index]
for i in range(index+1, self.length):
self[i-1] = self[i]
self.length -= 1
self.next_index -= 1
if self.length < self.capacity//2:
self.data.resize(self.capacity//2)
self.capacity = self.capacity//2
else:
raise IndexError
else:
raise IndexError
else:
raise TypeError
def insert(self, index, value):
if isinstance(index, int):
if index >= 0:
if index <= self.length:
self.length += 1
if self.length >= self.capacity:
self.data.resize(self.capacity*2)
self.capacity = self.capacity*2
self.next_index += 1
for i in range(self.length, index, -1):
self[i] = self[i-1]
self[index] = value
else:
raise IndexError
else:
raise IndexError
else:
raise TypeError
def is_full(self):
if self.length == self.capacity:
return True
else:
return False
def max(self):
if self.length > 0:
maxval = 0
for i in range(self.length):
if self[i] > maxval:
maxval = self[i]
return maxval
else:
return None
def min(self):
if self.length > 0:
minval = None
for i in range(self.length):
if minval is None or self[i] < minval:
minval = self[i]
return minval
else:
return None
def sum(self):
if self.length > 0:
sumval = 0
for i in range(self.length):
sumval = sumval + self[i]
return sumval
else:
return None
def linear_search(self, value):
for i in range(self.length):
if value == self[i]:
return i
def binary_search(self, value):
left = 0
right = self.length - 1
while left <= right:
mid = left + (right - left) // 2
mid_val = self[mid]
if mid_val == value:
return mid
elif value < mid_val:
right = mid - 1
else:
left = mid + 1
return None