diff --git a/pydatastructs/linear_data_structures/arrays.py b/pydatastructs/linear_data_structures/arrays.py index 27209a886..7dce4b943 100644 --- a/pydatastructs/linear_data_structures/arrays.py +++ b/pydatastructs/linear_data_structures/arrays.py @@ -130,6 +130,9 @@ def fill(self, elem): def __len__(self): return self._size + def __str__(self): + return str(self._data) + class DynamicArray(Array): """ @@ -260,6 +263,13 @@ def delete(self, idx): def size(self): return self._size + def __str__(self): + to_be_printed = ['' for i in range(self._last_pos_filled + 1)] + for i in range(self._last_pos_filled + 1): + if self._data[i] is not None: + to_be_printed[i] = str(self._data[i]) + return str(to_be_printed) + def __reversed__(self): for i in range(self._last_pos_filled, -1, -1): yield self._data[i] diff --git a/pydatastructs/linear_data_structures/tests/test_arrays.py b/pydatastructs/linear_data_structures/tests/test_arrays.py index b534d82ed..921164c08 100644 --- a/pydatastructs/linear_data_structures/tests/test_arrays.py +++ b/pydatastructs/linear_data_structures/tests/test_arrays.py @@ -7,6 +7,7 @@ def test_OneDimensionalArray(): ODA = OneDimensionalArray A = ODA(int, 5, [1.0, 2, 3, 4, 5], init=6) A[1] = 2.0 + assert str(A) == '[1, 2, 3, 4, 5]' assert A assert ODA(int, [1.0, 2, 3, 4, 5], 5) assert ODA(int, 5) @@ -27,6 +28,7 @@ def test_DynamicOneDimensionalArray(): A.append(2) A.append(3) A.append(4) + assert str(A) == "['1', '2', '3', '4']" A.delete(0) A.delete(0) A.delete(15) @@ -34,6 +36,7 @@ def test_DynamicOneDimensionalArray(): A.delete(1) A.delete(2) assert A._data == [4, None, None] + assert str(A) == "['4']" assert A.size == 3 A.fill(4) assert A._data == [4, 4, 4]