-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Implementing Dynamic array #12271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
cclauss
merged 9 commits into
TheAlgorithms:master
from
yashwanth-adimulam:dynamic_array
Sep 23, 2026
Merged
Implementing Dynamic array #12271
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fc55fcd
Adding dynamic array implementation
yashwanth-adimulam a16fb23
Adding references
yashwanth-adimulam 7a8d1b5
Merge branch 'TheAlgorithms:master' into dynamic_array
yashwanth-adimulam f9ddbfb
updating DIRECTORY.md
yashwanth-adimulam 7427a23
Trying to solve pre-commit error of type overload
yashwanth-adimulam 99ffcb7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 854c88c
Increasing test for dynamic_array.py
yashwanth-adimulam 5cf7b66
Merge branch 'master' into dynamic_array
cclauss de54c4a
Merge branch 'master' into dynamic_array
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| class DynamicArray: | ||
| def __init__(self) -> None: | ||
| self.size = 0 # Number of elements in the array | ||
| self.capacity = 1 # Initial capacity of the array | ||
| self.array = [None] * self.capacity # Create an array with initial capacity | ||
|
|
||
| def append(self, item: int) -> None: | ||
| """ | ||
| The function adds an item to the end of the dynamic array. | ||
|
|
||
| Runtime : O(1) amortized | ||
| Space: O(1) amortized | ||
|
|
||
| >>> arr = DynamicArray() | ||
| >>> arr.append(1) | ||
| >>> arr.append(2) | ||
| >>> arr.append(3) | ||
| >>> arr.array[:arr.size] # Display only the filled part of the array | ||
| [1, 2, 3] | ||
| >>> arr.append(4) | ||
| >>> arr.array[:arr.size] | ||
| [1, 2, 3, 4] | ||
| >>> arr.append(5) | ||
| >>> arr.array[:arr.size] | ||
| [1, 2, 3, 4, 5] | ||
| """ | ||
| if self.size == self.capacity: | ||
| self._resize(2 * self.capacity) # Double the capacity | ||
|
|
||
| self.array[self.size] = item | ||
| self.size += 1 | ||
|
|
||
| def _resize(self, new_capacity: int) -> None: | ||
| """ | ||
| Resizes the array to the new capacity. | ||
|
|
||
| Runtime : O(n) | ||
| Space: O(n) | ||
|
|
||
| >>> arr = DynamicArray() | ||
| >>> arr.append(1) | ||
| >>> arr.append(2) | ||
| >>> arr._resize(10) | ||
| >>> arr.capacity | ||
| 10 | ||
| >>> arr.array[:arr.size] | ||
| [1, 2] | ||
| """ | ||
| new_array = [None] * new_capacity | ||
| for i in range(self.size): | ||
| new_array[i] = self.array[i] | ||
| self.array = new_array | ||
| self.capacity = new_capacity | ||
|
|
||
| def get(self, index: int) -> int: | ||
| """ | ||
| The function returns the item at the specified index. | ||
|
|
||
| Runtime : O(1) | ||
| Space: O(1) | ||
|
|
||
| >>> arr = DynamicArray() | ||
| >>> arr.append(1) | ||
| >>> arr.append(2) | ||
| >>> arr.get(0) | ||
| 1 | ||
| >>> arr.get(1) | ||
| 2 | ||
| >>> arr.get(2) | ||
| Traceback (most recent call last): | ||
| ... | ||
| IndexError: index out of range | ||
| >>> arr.get(-1) | ||
| Traceback (most recent call last): | ||
| ... | ||
| IndexError: index out of range | ||
| """ | ||
| if index < 0 or index >= self.size: | ||
| raise IndexError("index out of range") | ||
| return self.array[index] | ||
|
|
||
| def __setitem__(self, index: int, value: int) -> None: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As there is no test file in this pull request nor any test function or class in the file |
||
| if index < 0 or index >= self.size: | ||
| raise IndexError("index out of range") | ||
| self.array[index] = value | ||
|
|
||
| def __len__(self) -> int: | ||
| """ | ||
| Returns the number of elements in the dynamic array. | ||
|
|
||
| Runtime : O(1) | ||
| Space: O(1) | ||
|
|
||
| >>> arr = DynamicArray() | ||
| >>> arr.append(1) | ||
| >>> len(arr) | ||
| 1 | ||
| >>> arr.append(2) | ||
| >>> len(arr) | ||
| 2 | ||
| >>> arr.append(3) | ||
| >>> len(arr) | ||
| 3 | ||
| """ | ||
| return self.size | ||
|
|
||
| def __str__(self) -> str: | ||
| """ | ||
| Returns a string representation of the dynamic array. | ||
|
|
||
| >>> arr = DynamicArray() | ||
| >>> arr.append(1) | ||
| >>> arr.append(2) | ||
| >>> arr.append(3) | ||
| >>> str(arr) | ||
| '[1, 2, 3]' | ||
| >>> arr.append(4) | ||
| >>> str(arr) | ||
| '[1, 2, 3, 4]' | ||
| """ | ||
| return "[" + ", ".join(str(self.array[i]) for i in range(self.size)) + "]" | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file
data_structures/arrays/dynamic_array.py, please provide doctest for the function__setitem__