forked from Dstar4/Hash-Tables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_array.py
46 lines (37 loc) · 1.2 KB
/
dynamic_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
class DynamicArray:
def __init__(self, capacity=8):
self.count = 0
self.capacity = capacity
self.storage = [None] * self.capacity
def insert(self, index, value):
if self.count == self.capacity:
# increase size
print("ERROR:, Array is full")
return
if index >= self.count:
print('Error: Index out of bounds')
return
for i in range(self.count, index -1):
self.storage[i] = self.storage[i-1]
self.storage[index] = value
self.count += 1
def append(self, value):
if self.count == self.capacity:
# increase size
print("ERROR:, Array is full")
return
# self.count += 1
# # account for zero index... - 1
# self.storage[self.count - 1] = value
#
# same as above
self.storage[self.count] = value
self.count += 1
# Double the size
def double_size(self):
self.capacity *= 2
new_storage = [None] * self.capacity
for i in range(self.count):
new_storage[i] = self.storage[i]
# change pointer
self.storage = new_storage