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 Stack #14

Merged
merged 9 commits into from
Jun 25, 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 @@ -4,3 +4,4 @@ __pycache__/
*.pyo
.vscode/
.pytest_cache/
pre_commit.ps1
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ python:
install:
- pip install -r requirements.txt
script:
- python -m unittest discover pydatastructs
- pytest --doctest-modules
2 changes: 2 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
Gagandeep Singh<singh.23@iitj.ac.in>
Kartikei Mittal<kartikeimittal@gmail.com>

2 changes: 2 additions & 0 deletions pydatastructs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
from .linear_data_structures import *
from .trees import *
from .miscellaneous_data_structures import *
from .utils import *
5 changes: 4 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
__all__ = []

from . import arrays
from . import (
arrays,
)

from .arrays import (
OneDimensionalArray,
)
Expand Down
9 changes: 5 additions & 4 deletions pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from __future__ import print_function, division
from pydatastructs.utils.misc_util import _check_type, NoneType

__all__ = [
'OneDimensionalArray'
]

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

class Array(object):
'''
Abstract class for arrays in pydatastructs.
Expand Down Expand Up @@ -105,7 +103,10 @@ def __getitem__(self, i):
return self._data.__getitem__(i)

def __setitem__(self, idx, elem):
self._data[idx] = self._dtype(elem)
if elem is None:
self._data[idx] = None
else:
self._data[idx] = self._dtype(elem)

def fill(self, elem):
elem = self._dtype(elem)
Expand Down
10 changes: 10 additions & 0 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
__all__ = []

from . import (
stack,
)

from .stack import (
Stack,
)
__all__.extend(stack.__all__)
114 changes: 114 additions & 0 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

from __future__ import print_function, division
from pydatastructs.linear_data_structures import OneDimensionalArray
from copy import deepcopy as dc

__all__ = [
'Stack'
]

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

class Stack(object):
"""Respresentation of stack data structure

Parameters
==========

implementation : str
Implementation to be used for stack.
By default, 'array'
Currently only supports 'array'
implementation.
maxsize : int
The maximum size of the stack.
For array implementation.
top : int
The top element of the stack.
For array implementation.
items : OneDimensionalArray
Optional, by default, None
The inital items in the stack.
For array implementation.
dtype : A valid python type
Optional, by default int if item
is None, otherwise takes the data
type of OneDimensionalArray
For array implementation.

Example
=======

>>> from pydatastructs import Stack
>>> s = Stack(maxsize=5, top=0)
>>> s.push(1)
>>> s.push(2)
>>> s.push(3)
>>> str(s)
'[1, 2, 3, None, None]'
>>> s.pop()
3

References
==========

.. [1] https://en.wikipedia.org/wiki/Stack_(abstract_data_type)
"""

def __new__(cls, implementation='array', **kwargs):
if implementation == 'array':
return ArrayStack(
kwargs.get('maxsize', None),
kwargs.get('top', None),
kwargs.get('items', None),
kwargs.get('dtype', int))
raise NotImplementedError(
"%s hasn't been implemented yet."%(implementation))

def push(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")

def pop(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")

class ArrayStack(Stack):

def __new__(cls, maxsize=None, top=0, items=None, dtype=int):
if not _check_type(maxsize, int):
raise ValueError("maxsize is missing.")
if not _check_type(top, int):
raise ValueError("top is missing.")
if items == None:
items = OneDimensionalArray(dtype, maxsize)
if not _check_type(items, OneDimensionalArray):
raise ValueError("items is not of type, OneDimensionalArray")
if items._size > maxsize:
raise ValueError("Overflow, size of items %s is greater "
"than maxsize, %s"%(items._size, maxsize))
obj = object.__new__(cls)
obj.maxsize, obj.top, obj.items, obj.dtype = \
maxsize, top, items, items._dtype
return obj

def push(self, x):
if self.top == self.maxsize:
raise ValueError("Stack is full.")
self.items[self.top] = self.dtype(x)
self.top += 1

def pop(self):
if self.top == 0:
raise ValueError("Stack is already empty.")
self.top -= 1
r = self.items[self.top]
self.items[self.top] = None
return r

def __str__(self):
"""
Used for printing.
"""
return str(self.items._data)
Empty file.
22 changes: 22 additions & 0 deletions pydatastructs/miscellaneous_data_structures/tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from pydatastructs.miscellaneous_data_structures import Stack
from pydatastructs.linear_data_structures import OneDimensionalArray
from pydatastructs.utils.raises_util import raises

def test_Stack():

s = Stack(maxsize=3, top=0)
s.push(1)
s.push(2)
s.push(3)
assert s.top == 3
assert str(s) == '[1, 2, 3]'
raises(ValueError, lambda: s.push(4))
assert s.pop() == 3
assert s.pop() == 2
assert s.pop() == 1
assert s.top == 0
raises(ValueError, lambda: s.pop())
raises(ValueError, lambda: Stack())
raises(ValueError, lambda: Stack(maxsize=5, top=0, items=[1, 2, 3]))
raises(ValueError, lambda: Stack(maxsize=5, top=0,
items=OneDimensionalArray(6)))
7 changes: 5 additions & 2 deletions pydatastructs/trees/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
__all__ = []

from . import binary_trees
from . import (
binary_trees,
space_partitioning_trees
)

from .binary_trees import (
Node, BinaryTree, BinarySearchTree
)
__all__.extend(binary_trees.__all__)

from . import space_partitioning_trees
from .space_partitioning_trees import (
OneDimensionalSegmentTree
)
Expand Down
33 changes: 1 addition & 32 deletions pydatastructs/trees/binary_trees.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,12 @@
from __future__ import print_function, division
from pydatastructs.utils import Node

__all__ = [
'Node',
'BinaryTree',
'BinarySearchTree'
]

class Node(object):
"""
Represents node in trees.

Parameters
==========

data
Any valid data to be stored in the node.
key
Required for comparison operations.
left: int
Optional, index of the left child node.
right: int
Optional, index of the right child node.
"""

__slots__ = ['key', 'data', 'left', 'right', 'is_root']

def __new__(cls, key, data):
obj = object.__new__(cls)
obj.data, obj.key = data, key
obj.left, obj.right = None, None
obj.is_root = False
return obj

def __str__(self):
"""
Used for printing.
"""
return str((self.left, self.key, self.data, self.right))

class BinaryTree(object):
"""
Abstract binary tree.
Expand Down
2 changes: 1 addition & 1 deletion pydatastructs/trees/space_partitioning_trees.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydatastructs.trees.binary_trees import Node
from pydatastructs.utils import Node
from collections import deque as Queue
from pydatastructs.linear_data_structures.arrays import _check_type

Expand Down
7 changes: 7 additions & 0 deletions pydatastructs/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
__all__ = []

from . import misc_util
from .misc_util import (
Node
)
__all__.extend(misc_util.__all__)
40 changes: 40 additions & 0 deletions pydatastructs/utils/misc_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import print_function, division

__all__ = [
'Node'
]

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

class Node(object):
"""
Represents node in trees.

Parameters
==========

data
Any valid data to be stored in the node.
key
Required for comparison operations.
left: int
Optional, index of the left child node.
right: int
Optional, index of the right child node.
"""

__slots__ = ['key', 'data', 'left', 'right', 'is_root']

def __new__(cls, key, data):
obj = object.__new__(cls)
obj.data, obj.key = data, key
obj.left, obj.right = None, None
obj.is_root = False
return obj

def __str__(self):
"""
Used for printing.
"""
return str((self.left, self.key, self.data, self.right))