Skip to content

Added str methods for ODA and DODA #208

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
merged 7 commits into from
Mar 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -255,6 +258,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