data_structures.arrays.dynamic_array ==================================== .. py:module:: data_structures.arrays.dynamic_array Classes ------- .. autoapisummary:: data_structures.arrays.dynamic_array.DynamicArray Module Contents --------------- .. py:class:: DynamicArray .. py:method:: __len__() -> 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 .. py:method:: __setitem__(index: int, value: int) -> None .. py:method:: __str__() -> 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]' .. py:method:: _resize(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] .. py:method:: append(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] .. py:method:: get(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 .. py:attribute:: array :value: [None] .. py:attribute:: capacity :value: 1 .. py:attribute:: size :value: 0