diff --git a/pydatastructs/__init__.py b/pydatastructs/__init__.py new file mode 100644 index 000000000..dd863b051 --- /dev/null +++ b/pydatastructs/__init__.py @@ -0,0 +1 @@ +from .linear_data_structures import * diff --git a/pydatastructs/linear_data_structures/__init__.py b/pydatastructs/linear_data_structures/__init__.py new file mode 100644 index 000000000..663cdfd8f --- /dev/null +++ b/pydatastructs/linear_data_structures/__init__.py @@ -0,0 +1,7 @@ +__all__ = [] + +from . import arrays +from .arrays import ( + OneDimensionalArray, +) +__all__.extend(arrays.__all__) diff --git a/pydatastructs/linear_data_structures/arrays.py b/pydatastructs/linear_data_structures/arrays.py new file mode 100644 index 000000000..b046e4831 --- /dev/null +++ b/pydatastructs/linear_data_structures/arrays.py @@ -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 diff --git a/pydatastructs/linear_data_structures/tests/__init__.py b/pydatastructs/linear_data_structures/tests/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pydatastructs/linear_data_structures/tests/test_arrays.py b/pydatastructs/linear_data_structures/tests/test_arrays.py new file mode 100644 index 000000000..607f038d1 --- /dev/null +++ b/pydatastructs/linear_data_structures/tests/test_arrays.py @@ -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])) diff --git a/pydatastructs/utils/__init__.py b/pydatastructs/utils/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/pydatastructs/utils/raises_util.py b/pydatastructs/utils/raises_util.py new file mode 100644 index 000000000..3d55b5552 --- /dev/null +++ b/pydatastructs/utils/raises_util.py @@ -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()