Update pop function (#5544)

* Updated Pop function

Added underflow condition

* Update Pop Function

Added condition to check underflow of stack

* Update stack.py

* if not self.stack: raise StackUnderflowError

* Add doctests

* StackUnderflowError

* ..., not ....

* Update stack.py

Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
Limbad Yash 2021-10-23 18:48:09 +05:30 committed by GitHub
parent b72a66b713
commit 80a885c975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -5,6 +5,10 @@ class StackOverflowError(BaseException):
pass pass
class StackUnderflowError(BaseException):
pass
class Stack: class Stack:
"""A stack is an abstract data type that serves as a collection of """A stack is an abstract data type that serves as a collection of
elements with two principal operations: push() and pop(). push() adds an elements with two principal operations: push() and pop(). push() adds an
@ -31,11 +35,29 @@ class Stack:
self.stack.append(data) self.stack.append(data)
def pop(self): def pop(self):
"""Pop an element off of the top of the stack.""" """
Pop an element off of the top of the stack.
>>> Stack().pop()
Traceback (most recent call last):
...
data_structures.stacks.stack.StackUnderflowError
"""
if not self.stack:
raise StackUnderflowError
return self.stack.pop() return self.stack.pop()
def peek(self): def peek(self):
"""Peek at the top-most element of the stack.""" """
Peek at the top-most element of the stack.
>>> Stack().pop()
Traceback (most recent call last):
...
data_structures.stacks.stack.StackUnderflowError
"""
if not self.stack:
raise StackUnderflowError
return self.stack[-1] return self.stack[-1]
def is_empty(self) -> bool: def is_empty(self) -> bool:
@ -67,22 +89,22 @@ def test_stack() -> None:
try: try:
_ = stack.pop() _ = stack.pop()
assert False # This should not happen assert False # This should not happen
except IndexError: except StackUnderflowError:
assert True # This should happen assert True # This should happen
try: try:
_ = stack.peek() _ = stack.peek()
assert False # This should not happen assert False # This should not happen
except IndexError: except StackUnderflowError:
assert True # This should happen assert True # This should happen
for i in range(10): for i in range(10):
assert stack.size() == i assert stack.size() == i
stack.push(i) stack.push(i)
assert bool(stack) is True assert bool(stack)
assert stack.is_empty() is False assert not stack.is_empty()
assert stack.is_full() is True assert stack.is_full()
assert str(stack) == str(list(range(10))) assert str(stack) == str(list(range(10)))
assert stack.pop() == 9 assert stack.pop() == 9
assert stack.peek() == 8 assert stack.peek() == 8
@ -96,7 +118,7 @@ def test_stack() -> None:
except StackOverflowError: except StackOverflowError:
assert True # This should happen assert True # This should happen
assert stack.is_empty() is False assert not stack.is_empty()
assert stack.size() == 10 assert stack.size() == 10
assert 5 in stack assert 5 in stack