Skip to content

Commit 6dfe637

Browse files
yashwanth-adimulampre-commit-ci[bot]cclauss
authored
Implementing Dynamic array (#12271)
* Adding dynamic array implementation * Adding references * updating DIRECTORY.md * Trying to solve pre-commit error of type overload * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Increasing test for dynamic_array.py --------- Co-authored-by: yashwanth-adimulam <yashwanth-adimulam@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 42a7354 commit 6dfe637

2 files changed

Lines changed: 128 additions & 0 deletions

File tree

‎DIRECTORY.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,7 @@
262262

263263
## [Data Structures](data_structures)
264264
* Arrays
265+
* [Dynamic Array](data_structures/arrays/dynamic_array.py)
265266
* [Equilibrium Index In Array](data_structures/arrays/equilibrium_index_in_array.py)
266267
* [Find Triplets With 0 Sum](data_structures/arrays/find_triplets_with_0_sum.py)
267268
* [Gas Station](data_structures/arrays/gas_station.py)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
class DynamicArray:
2+
def __init__(self) -> None:
3+
self.size = 0 # Number of elements in the array
4+
self.capacity = 1 # Initial capacity of the array
5+
self.array = [None] * self.capacity # Create an array with initial capacity
6+
7+
def append(self, item: int) -> None:
8+
"""
9+
The function adds an item to the end of the dynamic array.
10+
11+
Runtime : O(1) amortized
12+
Space: O(1) amortized
13+
14+
>>> arr = DynamicArray()
15+
>>> arr.append(1)
16+
>>> arr.append(2)
17+
>>> arr.append(3)
18+
>>> arr.array[:arr.size] # Display only the filled part of the array
19+
[1, 2, 3]
20+
>>> arr.append(4)
21+
>>> arr.array[:arr.size]
22+
[1, 2, 3, 4]
23+
>>> arr.append(5)
24+
>>> arr.array[:arr.size]
25+
[1, 2, 3, 4, 5]
26+
"""
27+
if self.size == self.capacity:
28+
self._resize(2 * self.capacity) # Double the capacity
29+
30+
self.array[self.size] = item
31+
self.size += 1
32+
33+
def _resize(self, new_capacity: int) -> None:
34+
"""
35+
Resizes the array to the new capacity.
36+
37+
Runtime : O(n)
38+
Space: O(n)
39+
40+
>>> arr = DynamicArray()
41+
>>> arr.append(1)
42+
>>> arr.append(2)
43+
>>> arr._resize(10)
44+
>>> arr.capacity
45+
10
46+
>>> arr.array[:arr.size]
47+
[1, 2]
48+
"""
49+
new_array = [None] * new_capacity
50+
for i in range(self.size):
51+
new_array[i] = self.array[i]
52+
self.array = new_array
53+
self.capacity = new_capacity
54+
55+
def get(self, index: int) -> int:
56+
"""
57+
The function returns the item at the specified index.
58+
59+
Runtime : O(1)
60+
Space: O(1)
61+
62+
>>> arr = DynamicArray()
63+
>>> arr.append(1)
64+
>>> arr.append(2)
65+
>>> arr.get(0)
66+
1
67+
>>> arr.get(1)
68+
2
69+
>>> arr.get(2)
70+
Traceback (most recent call last):
71+
...
72+
IndexError: index out of range
73+
>>> arr.get(-1)
74+
Traceback (most recent call last):
75+
...
76+
IndexError: index out of range
77+
"""
78+
if index < 0 or index >= self.size:
79+
raise IndexError("index out of range")
80+
return self.array[index]
81+
82+
def __setitem__(self, index: int, value: int) -> None:
83+
if index < 0 or index >= self.size:
84+
raise IndexError("index out of range")
85+
self.array[index] = value
86+
87+
def __len__(self) -> int:
88+
"""
89+
Returns the number of elements in the dynamic array.
90+
91+
Runtime : O(1)
92+
Space: O(1)
93+
94+
>>> arr = DynamicArray()
95+
>>> arr.append(1)
96+
>>> len(arr)
97+
1
98+
>>> arr.append(2)
99+
>>> len(arr)
100+
2
101+
>>> arr.append(3)
102+
>>> len(arr)
103+
3
104+
"""
105+
return self.size
106+
107+
def __str__(self) -> str:
108+
"""
109+
Returns a string representation of the dynamic array.
110+
111+
>>> arr = DynamicArray()
112+
>>> arr.append(1)
113+
>>> arr.append(2)
114+
>>> arr.append(3)
115+
>>> str(arr)
116+
'[1, 2, 3]'
117+
>>> arr.append(4)
118+
>>> str(arr)
119+
'[1, 2, 3, 4]'
120+
"""
121+
return "[" + ", ".join(str(self.array[i]) for i in range(self.size)) + "]"
122+
123+
124+
if __name__ == "__main__":
125+
import doctest
126+
127+
doctest.testmod()

0 commit comments

Comments
 (0)