Skip to content

Commit

Permalink
Added str methods for ODA and DODA (#208)
Browse files Browse the repository at this point in the history
  • Loading branch information
HarsheetKakar authored Mar 25, 2020
1 parent 37a9483 commit 6cb5520
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@ def fill(self, elem):
def __len__(self):
return self._size

def __str__(self):
return str(self._data)


class DynamicArray(Array):
"""
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 3 additions & 0 deletions pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -27,13 +28,15 @@ 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)
A.delete(-1)
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]
Expand Down

0 comments on commit 6cb5520

Please sign in to comment.