Skip to content

Commit 854c88c

Browse files
Increasing test for dynamic_array.py
1 parent 99ffcb7 commit 854c88c

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

data_structures/arrays/dynamic_array.py

+18-12
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
"""
2-
Author : Yashwanth Adimulam
3-
Date : October 22, 2024
4-
5-
Implement the class of DynamicArray with useful functions based on it.
6-
References: https://en.wikipedia.org/wiki/Dynamic_array
7-
https://www.geeksforgeeks.org/how-do-dynamic-arrays-work/
8-
9-
"""
10-
11-
121
class DynamicArray:
132
def __init__(self) -> None:
143
self.size = 0 # Number of elements in the array
@@ -28,6 +17,12 @@ def append(self, item: int) -> None:
2817
>>> arr.append(3)
2918
>>> arr.array[:arr.size] # Display only the filled part of the array
3019
[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]
3126
"""
3227
if self.size == self.capacity:
3328
self._resize(2 * self.capacity) # Double the capacity
@@ -45,10 +40,11 @@ def _resize(self, new_capacity: int) -> None:
4540
>>> arr = DynamicArray()
4641
>>> arr.append(1)
4742
>>> arr.append(2)
48-
>>> arr.append(3)
4943
>>> arr._resize(10)
5044
>>> arr.capacity
5145
10
46+
>>> arr.array[:arr.size]
47+
[1, 2]
5248
"""
5349
new_array = [None] * new_capacity
5450
for i in range(self.size):
@@ -74,6 +70,10 @@ def get(self, index: int) -> int:
7470
Traceback (most recent call last):
7571
...
7672
IndexError: index out of range
73+
>>> arr.get(-1)
74+
Traceback (most recent call last):
75+
...
76+
IndexError: index out of range
7777
"""
7878
if index < 0 or index >= self.size:
7979
raise IndexError("index out of range")
@@ -98,6 +98,9 @@ def __len__(self) -> int:
9898
>>> arr.append(2)
9999
>>> len(arr)
100100
2
101+
>>> arr.append(3)
102+
>>> len(arr)
103+
3
101104
"""
102105
return self.size
103106

@@ -111,6 +114,9 @@ def __str__(self) -> str:
111114
>>> arr.append(3)
112115
>>> str(arr)
113116
'[1, 2, 3]'
117+
>>> arr.append(4)
118+
>>> str(arr)
119+
'[1, 2, 3, 4]'
114120
"""
115121
return "[" + ", ".join(str(self.array[i]) for i in range(self.size)) + "]"
116122

0 commit comments

Comments
 (0)