-
Notifications
You must be signed in to change notification settings - Fork 0
/
dynamic_arrays.py
112 lines (89 loc) · 2.67 KB
/
dynamic_arrays.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
import ctypes
class DynamicArray:
"""Dynamic Array which can resize based on usage"""
def __init__(self, capacity=1):
"""Initailize the array with capactiy 1"""
self.length = 0
self.capacity = capacity
self.array = self.make_array(capacity)
def __len__(self):
"""Returns the length of the array"""
return self.length
def __getitem__(self, index):
""""Returns the element at a particular index"""
try:
if self.length > index >= 0:
return self.array[index]
except:
IndexError("Out of bound")
def __repr__(self):
"""Returns the representaion of the class"""
values = ''
for i in range(self.length):
values += str(self.array[i]) + ', '
values = values[: len(values) - 2]
return "DynamicArray([" + values + "])"
def append(self, value):
"""Add value to the end of the array"""
if self.length == self.capacity:
self.__re_size()
self.array[self.length] = value
self.length += 1
def removeAt(self, index):
""""Removes the element from the array if the index is within the range of the array"""
if self.length < index or index < 0:
return IndexError("Index out of bound")
if self.length == 0:
print("Array is already empty")
return -1
value = self.array[index]
for i in range(index, self.length - 1):
self.array[i] = self.array[i + 1]
self.array[self.length - 1] = 0
self.length -= 1
self.__re_size()
return value
def remove(self, value):
""""Removes the value if it exits in the array"""
for i in range(self.length):
if value == self.array[i]:
self.removeAt(i)
return value
print("Value not found in the array")
def clear(self):
"""Clears the entire array"""
for i in range(self.length):
self.array[i] = 0
self.length = 0
self.__re_size()
return
def insertAt(self, index, value):
"""Inserts the value at a particular index"""
if self.length < index or index < 0:
return IndexError("Index out of bound")
if self.length == self.capacity:
self.__re_size()
for i in range(self.length - 1, index - 1, -1):
self.array[i + 1] = self.array[i]
self.length += 1
self.array[index] = value
return
def __re_size(self):
"""private method to dynamically resize the arrya"""
if self.capacity == self.length:
temp = self.make_array(2 * self.capacity)
elif self.capacity == self.length * 4:
temp = self.make_arrya(self.capacity // 2)
elif self.length == 0:
self.array = self.make_array(1)
return
else:
return
for i in range(self.length):
temp[i] = self.array[i]
self.array = temp
return
def make_array(self, new_capacity):
"""Returns the new array with the desired capacity"""
self.capacity = new_capacity
return (new_capacity * ctypes.py_object)()