Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
stacks complete
  • Loading branch information
czgdp1807 committed Jun 25, 2019
commit d6d8c3912b0fbb16c4fc23f8a91aafe8d70c67f5
1 change: 0 additions & 1 deletion pydatastructs/linear_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from . import (
arrays,
stack
)

from .arrays import (
Expand Down
5 changes: 4 additions & 1 deletion pydatastructs/linear_data_structures/arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,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
75 changes: 54 additions & 21 deletions pydatastructs/miscellaneous_data_structures/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,39 @@ class Stack(object):
Parameters
==========

max_size : int
Maximum size of stack allowed

Raises
======

TypeError
max_size argument should of type 'int'.
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
>>> my_stack = Stack()
>>> my_stack.push(1)
>>> my_stack.push(2)
>>> my_stack.pop()
2
>>> str(my_stack)
<Stack length:[1]>
>>> 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
"""

def __new__(cls, implementation='array', **kwargs):
Expand All @@ -46,11 +59,7 @@ def __new__(cls, implementation='array', **kwargs):
kwargs.get('items', None),
kwargs.get('dtype', int))
raise NotImplementedError(
"LinkedListStack hasn't been implemented yet.")

def initialize(self, *args, **kwargs):
raise NotImplementedError(
"This is an abstract method.")
"%s hasn't been implemented yet."%(implementation))

def push(self, *args, **kwargs):
raise NotImplementedError(
Expand All @@ -62,7 +71,7 @@ def pop(self, *args, **kwargs):

class ArrayStack(Stack):

def __new__(maxsize=None, top=None, items=None, dtype=int):
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):
Expand All @@ -74,3 +83,27 @@ def __new__(maxsize=None, top=None, items=None, dtype=int):
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)
70 changes: 19 additions & 51 deletions pydatastructs/miscellaneous_data_structures/tests/test_stack.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,22 @@
from pydatastructs.miscellaneous_data_structures import Stack
from pydatastructs.linear_data_structures import OneDimensionalArray
from pydatastructs.utils.raises_util import raises

class TestStack(unittest.TestCase):
"""Stack Tests"""
def setUp(self):
"""Set Up"""
self.s = Stack(max_size=5, type_restriction=['int', 'str'])
def test_Stack():

def test_length(self):
""""""
self.s.push('2')
self.s.push('A')
self.assertEqual(len(self.s), 2)
self.s.pop()
self.assertEqual(len(self.s), 1)

def test_error_init(self):
"""Testing Errors in init"""
with self.assertRaises(TypeError):
stack_temp = Stack(type_restriction='int')
with self.assertRaises(TypeError):
stack_temp = Stack(type_restriction=[int])
with self.assertRaises(TypeError):
stack_temp = Stack(max_size='10')

def test_push_pop(self):
"""Testing consecutive push and pop"""
self.s.push(2)
self.s.push(1)
self.assertEqual(self.s.pop(), 1)
self.assertEqual(self.s.pop(), 2)

def test_stack_overflow(self):
"""Testing Stack overflow condition"""
self.s.push(2)
self.s.push(1)
self.s.push(3)
self.s.push(4)
self.s.push(5)
with self.assertRaises(ValueError):
self.s.push(6)

def test_stack_underflow(self):
"""Testing Stack underflow condition"""
self.s.push(2)
self.s.push(1)
self.assertEqual(self.s.pop(), 1)
self.assertEqual(self.s.pop(), 2)
with self.assertRaises(ValueError):
self.s.pop()

def tearDown(self):
"""Tear Down"""
del self.s
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)))