Skip to content

Commit 00e0a1b

Browse files
authored
Update Pop Function
Added condition to check underflow of stack
1 parent 7402a02 commit 00e0a1b

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

data_structures/stacks/stack.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ class StackOverflowError(BaseException):
77
class StackUnderflowError(BaseException):
88
pass
99

10-
1110
class Stack:
1211
"""A stack is an abstract data type that serves as a collection of
1312
elements with two principal operations: push() and pop(). push() adds an
@@ -35,8 +34,8 @@ def push(self, data):
3534

3635
def pop(self):
3736
"""Pop an element off of the top of the stack."""
38-
if len(self.stack) <= 0:
39-
raise StackUnderflowError
37+
if not self.stack: # Condition to check Underflow of stack
38+
pass
4039
return self.stack.pop()
4140

4241
def peek(self):
@@ -69,6 +68,7 @@ def test_stack() -> None:
6968
assert stack.is_full() is False
7069
assert str(stack) == "[]"
7170

71+
7272
try:
7373
_ = stack.pop()
7474
assert False # This should not happen

0 commit comments

Comments
 (0)