Skip to content
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

Array has been completed #4

Merged
merged 1 commit into from
Jun 18, 2019
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
__pycache__/
*.pyo
.vscode/
.pytest_cache/
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ python:
- "3.5"
install:
- pip install -r requirements.txt
script: pytest
script:
- pytest --doctest-modules
- pytest
54 changes: 39 additions & 15 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,27 @@
from __future__ import print_function, division

_check_type = lambda a, t: isinstance(a, t)

__all__ = [
'OneDimensionalArray'
]

_check_type = lambda a, t: isinstance(a, t)
NoneType = type(None)

class Array(object):
"""
'''
Abstract class for arrays in pydatastructs.
"""
'''
pass

class OneDimensionalArray(Array):
"""
'''
Represents one dimensional arrays.

Parameters
==========

dtype: type
A valid object type.
size: int
The number of elements in the array.
elements: list/tuple
Expand All @@ -37,31 +40,38 @@ class OneDimensionalArray(Array):
match with the size.
More than three parameters are passed as arguments.
Types of arguments is not as mentioned in the docstring.
TypeError
When all the elements are not of same type.

Note
====

At least one parameter should be passed as an argument.
At least one parameter should be passed as an argument along
with the dtype.

Examples
========
"""
__slots__ = ['_size', '_data']

def __new__(cls, *args, **kwargs):
if not args or len(args) not in (1, 2):
>>> from pydatastructs import OneDimensionalArray as ODA
>>> arr = ODA(int, 5)
>>> arr.fill(6)
>>> arr[0]
6
>>> arr[0] = 7.2
>>> arr[0]
7
'''
def __new__(cls, dtype=NoneType, *args, **kwargs):
if dtype == NoneType or len(args) not in (1, 2):
raise ValueError("1D array cannot be created due to incorrect"
" information.")
obj = object.__new__(cls)
obj._dtype = dtype
if len(args) == 2:
if _check_type(args[0], (list, tuple)) and \
_check_type(args[1], int):
size, data = args[1], args[0]
size, data = args[1], [dtype(arg) for arg in args[0]]
elif _check_type(args[1], (list, tuple)) and \
_check_type(args[0], int):
size, data = args[0], args[1]
size, data = args[0], [dtype(arg) for arg in args[1]]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")
Expand All @@ -76,9 +86,23 @@ def __new__(cls, *args, **kwargs):
init = kwargs.get('init', None)
obj._data = [init for i in range(args[0])]
elif _check_type(args[0], (list, tuple)):
obj._size, obj._data = len(args[0]), args[0]
obj._size, obj._data = len(args[0]), \
[dtype(arg) for arg in args[0]]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")

return obj

def __getitem__(self, i):
if i >= self._size or i < 0:
raise IndexError("Index out of range.")
return self._data.__getitem__(i)

def __setitem__(self, idx, elem):
self._data[idx] = self._dtype(elem)

def fill(self, elem):
elem = self._dtype(elem)
for i in range(self._size):
self._data[i] = elem
19 changes: 10 additions & 9 deletions pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
from pydatastructs.linear_data_structures import OneDimensionalArray
from pydatastructs.utils.raises_util import raises


def test_OneDimensionalArray():
ODA = OneDimensionalArray
assert ODA(5, [1, 2, 3, 4, 5], init=6)
assert ODA((1, 2, 3, 4, 5), 5)
assert ODA(5)
assert ODA([1, 2, 3])
assert ODA(int, 5, [1, 2, 3, 4, 5], init=6)
assert ODA(int, (1, 2, 3, 4, 5), 5)
assert ODA(int, 5)
assert ODA(int, [1, 2, 3])
raises(ValueError, lambda: ODA())
raises(ValueError, lambda: ODA(1, 2, 3))
raises(TypeError, lambda: ODA(5.0, set([1, 2, 3])))
raises(TypeError, lambda: ODA(5.0))
raises(TypeError, lambda: ODA(set([1, 2, 3])))
raises(ValueError, lambda: ODA(3, [1]))
raises(ValueError, lambda: ODA(int, 1, 2, 3))
raises(TypeError, lambda: ODA(int, 5.0, set([1, 2, 3])))
raises(TypeError, lambda: ODA(int, 5.0))
raises(TypeError, lambda: ODA(int, set([1, 2, 3])))
raises(ValueError, lambda: ODA(int, 3, [1]))