-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamicArray2.py
137 lines (115 loc) · 4.24 KB
/
dynamicArray2.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
# unguided implementation of a mutable array
# with automatic resizing and other standard methods
import ctypes, os
class DynamicArray():
def __init__(self):
self._n = 0
self._capacity = 1
self._Array = self._make_array(self._capacity)
def __len__(self):
return self._n
def __getitem__(self, index):
if 0 <= index < self._n:
return self._Array[index]
else:
raise ValueError("invalid index")
def _resize(self, capacity):
B = self._make_array(capacity)
for i in range(self._n):
B[i] = self._Array[i]
self._Array = B
self._capacity = capacity
def _make_array(self, capacity):
return (capacity * ctypes.py_object)()
def append(self, item):
if self._n == self._capacity:
self._resize(2 * self._capacity)
self._Array[self._n] = item
self._n += 1
def find(self, item):
for i in range(self._n):
if self._Array[i] == item:
return i
return -1
def insert(self, index, item):
if not 0 <= index <= self._n:
raise ValueError("invalid index")
if self._n == self._capacity:
self._resize(2 * self._capacity)
for j in range(self._n, index, -1):
self._Array[j] = self[j - 1]
self._Array[index] = item
self._n += 1
def remove(self, item):
# search for item in list
for i in range(self._n):
if self._Array[i] == item:
removed_index = i
# shift elements left
for j in range(self._n - 1, removed_index, -1):
self._Array[j - 1] = self._Array[j]
# resize if capacity utilization <= 25%
if self._n / self._capacity <= 0.25:
self._resize(self._capacity // 2)
# update count of items in list
self._n -= 1
return removed_index
def pop(self, index = None):
# check if index was given, else remove last one
if index == None:
index = self._n - 1
# error checking on index
if not 0 <= index < self._n:
raise ValueError("invalid index")
# archive item and remove it
item_removed = self._Array[index]
self._Array[index] = None
# shift array elements if needed
for j in range(self._n - 1, index, -1):
self._Array[j - 1] = self._Array[j]
# resize if capacity utilization <= 25%
if self._n / self._capacity <= 0.25:
self._resize(self._capacity // 2)
# update count of items in list
self._n -= 1
return item_removed
def capacity(self):
return int(self._capacity)
if __name__ == '__main__':
list = DynamicArray()
print("Initial items in list:", len(list))
print("Appending some items...")
list.append(2)
list.append(5)
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
print("Adding some more values and reprinting...")
list.append(4)
list.append(7)
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
print("Popping off last value")
print("Removed value was: {0}".format(list.pop()))
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
print("Removing 5 from list...")
list.remove(5)
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
print("Inserting 5 back into the list at index 1...")
list.insert(1, 5)
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
for i in range(10):
list.append(i)
print("Appending {0}... now the capacity is {1}".format(i, list.capacity()))
for i in range(10):
list.pop()
print("Popping last element... now the capacity is {0}".format(list.capacity()))
for i in range(len(list)):
print("The value at index {0} is: {1}".format(i, list[i]))
found_index = list.find(2)
if found_index >= 0:
print(".find() worked and found 2 at index {0}".format(found_index))
else:
print(".find() did not find 2")