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

Added Arrays in Linear Data Structures #1

Merged
merged 4 commits into from
Jun 17, 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 pydatastructs/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .linear_data_structures import *
7 changes: 7 additions & 0 deletions pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__all__ = []

from . import arrays
from .arrays import (
OneDimensionalArray,
)
__all__.extend(arrays.__all__)
84 changes: 84 additions & 0 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from __future__ import print_function, division

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

__all__ = [
'OneDimensionalArray'
]

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

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

Parameters
==========

size: int
The number of elements in the array.
elements: list/tuple
The elements in the array, all should
be of same type.
init: a python type
The inital value with which the element has
to be initialized. By default none, used only
when the data is not given.

Raises
======

ValueError
When the number of elements in the list do not
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.

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

def __new__(cls, *args, **kwargs):
if not args or len(args) not in (1, 2):
raise ValueError("1D array cannot be created due to incorrect"
" information.")
obj = object.__new__(cls)
if len(args) == 2:
if _check_type(args[0], (list, tuple)) and \
_check_type(args[1], int):
size, data = args[1], args[0]
elif _check_type(args[1], (list, tuple)) and \
_check_type(args[0], int):
size, data = args[0], args[1]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")
if size != len(data):
raise ValueError("Conflict in the size %s and length of data %s"
%(size, len(data)))
obj._size, obj._data = size, data

elif len(args) == 1:
if _check_type(args[0], int):
obj._size = args[0]
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]
else:
raise TypeError("Expected type of size is int and "
"expected type of data is list/tuple.")

return obj
Empty file.
15 changes: 15 additions & 0 deletions pydatastructs/linear_data_structures/tests/test_arrays.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
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])
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]))
Empty file added pydatastructs/utils/__init__.py
Empty file.
16 changes: 16 additions & 0 deletions pydatastructs/utils/raises_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest

def raises(exception, code):
"""
Utility for testing exceptions.

Parameters
==========

exception
A valid python exception
code: lambda
Code that causes exception
"""
with pytest.raises(exception):
code()