From 7402a02905b5bf56ba3b1b37956d8641613a07ec Mon Sep 17 00:00:00 2001 From: Limbad Yash <56826569+limbad-YK@users.noreply.github.com> Date: Thu, 21 Oct 2021 18:46:34 +0530 Subject: [PATCH 1/8] Updated Pop function Added underflow condition --- data_structures/stacks/stack.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index c62412150626..13b2eb4ef63e 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -4,6 +4,9 @@ class StackOverflowError(BaseException): pass +class StackUnderflowError(BaseException): + pass + class Stack: """A stack is an abstract data type that serves as a collection of @@ -32,6 +35,8 @@ def push(self, data): def pop(self): """Pop an element off of the top of the stack.""" + if len(self.stack) <= 0: + raise StackUnderflowError return self.stack.pop() def peek(self): From 00e0a1b9a20568ea60db8d3af36cf55ed37ee746 Mon Sep 17 00:00:00 2001 From: Limbad Yash <56826569+limbad-YK@users.noreply.github.com> Date: Sat, 23 Oct 2021 15:28:13 +0530 Subject: [PATCH 2/8] Update Pop Function Added condition to check underflow of stack --- data_structures/stacks/stack.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 13b2eb4ef63e..11d40f98533c 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -7,7 +7,6 @@ class StackOverflowError(BaseException): class StackUnderflowError(BaseException): pass - class Stack: """A stack is an abstract data type that serves as a collection of elements with two principal operations: push() and pop(). push() adds an @@ -35,8 +34,8 @@ def push(self, data): def pop(self): """Pop an element off of the top of the stack.""" - if len(self.stack) <= 0: - raise StackUnderflowError + if not self.stack: # Condition to check Underflow of stack + pass return self.stack.pop() def peek(self): @@ -69,6 +68,7 @@ def test_stack() -> None: assert stack.is_full() is False assert str(stack) == "[]" + try: _ = stack.pop() assert False # This should not happen From 501af2c7b24fbe648c73b223b7d87c31e8ee75fe Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:36:20 +0200 Subject: [PATCH 3/8] Update stack.py --- data_structures/stacks/stack.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 11d40f98533c..eac454c69974 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -4,9 +4,11 @@ class StackOverflowError(BaseException): pass + class StackUnderflowError(BaseException): pass + class Stack: """A stack is an abstract data type that serves as a collection of elements with two principal operations: push() and pop(). push() adds an @@ -15,7 +17,6 @@ class Stack: Last In, First Out (LIFO). https://en.wikipedia.org/wiki/Stack_(abstract_data_type) """ - def __init__(self, limit: int = 10): self.stack: list[int] = [] self.limit = limit @@ -34,7 +35,7 @@ def push(self, data): def pop(self): """Pop an element off of the top of the stack.""" - if not self.stack: # Condition to check Underflow of stack + if not self.stack: # Condition to check Underflow of stack pass return self.stack.pop() @@ -68,7 +69,6 @@ def test_stack() -> None: assert stack.is_full() is False assert str(stack) == "[]" - try: _ = stack.pop() assert False # This should not happen @@ -85,9 +85,9 @@ def test_stack() -> None: assert stack.size() == i stack.push(i) - assert bool(stack) is True - assert stack.is_empty() is False - assert stack.is_full() is True + assert bool(stack) + assert not stack.is_empty() + assert stack.is_full() assert str(stack) == str(list(range(10))) assert stack.pop() == 9 assert stack.peek() == 8 @@ -101,7 +101,7 @@ def test_stack() -> None: except StackOverflowError: assert True # This should happen - assert stack.is_empty() is False + assert not stack.is_empty() assert stack.size() == 10 assert 5 in stack From 904284b7a6d8012a5cadbd3b37ad013e07a24ab8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:44:03 +0200 Subject: [PATCH 4/8] if not self.stack: raise StackUnderflowError --- data_structures/stacks/stack.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index eac454c69974..00f2962a4fd5 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -17,6 +17,7 @@ class Stack: Last In, First Out (LIFO). https://en.wikipedia.org/wiki/Stack_(abstract_data_type) """ + def __init__(self, limit: int = 10): self.stack: list[int] = [] self.limit = limit @@ -35,12 +36,14 @@ def push(self, data): def pop(self): """Pop an element off of the top of the stack.""" - if not self.stack: # Condition to check Underflow of stack - pass + if not self.stack: + raise StackUnderflowError return self.stack.pop() def peek(self): """Peek at the top-most element of the stack.""" + if not self.stack: + raise StackUnderflowError return self.stack[-1] def is_empty(self) -> bool: From 2fe1dc594da54e9f812691015a03ba9b093d20f2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:50:44 +0200 Subject: [PATCH 5/8] Add doctests --- data_structures/stacks/stack.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 00f2962a4fd5..488aafd53022 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -35,13 +35,27 @@ def push(self, data): self.stack.append(data) 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): + .... + StackOverflowError + """ if not self.stack: raise StackUnderflowError return self.stack.pop() 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): + .... + StackOverflowError + """ if not self.stack: raise StackUnderflowError return self.stack[-1] From 6efa164fcf2cfbd89e0a66689a5f866206bb9ea2 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 14:59:21 +0200 Subject: [PATCH 6/8] StackUnderflowError --- data_structures/stacks/stack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 488aafd53022..1138ed0a1781 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -41,7 +41,7 @@ def pop(self): >>> Stack().pop() Traceback (most recent call last): .... - StackOverflowError + StackUnderflowError """ if not self.stack: raise StackUnderflowError @@ -54,7 +54,7 @@ def peek(self): >>> Stack().pop() Traceback (most recent call last): .... - StackOverflowError + StackUnderflowError """ if not self.stack: raise StackUnderflowError From 91498dfc034cb94dd8de96861aacd2c6e9a08626 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 15:05:38 +0200 Subject: [PATCH 7/8] ..., not .... --- data_structures/stacks/stack.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 1138ed0a1781..6aec7c80c164 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -40,7 +40,7 @@ def pop(self): >>> Stack().pop() Traceback (most recent call last): - .... + ... StackUnderflowError """ if not self.stack: @@ -53,7 +53,7 @@ def peek(self): >>> Stack().pop() Traceback (most recent call last): - .... + ... StackUnderflowError """ if not self.stack: From 0f2bf9a67b265409e7ba522e038039c7a6a90eb8 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sat, 23 Oct 2021 15:14:20 +0200 Subject: [PATCH 8/8] Update stack.py --- data_structures/stacks/stack.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 6aec7c80c164..4bc032f72561 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -41,7 +41,7 @@ def pop(self): >>> Stack().pop() Traceback (most recent call last): ... - StackUnderflowError + data_structures.stacks.stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError @@ -54,7 +54,7 @@ def peek(self): >>> Stack().pop() Traceback (most recent call last): ... - StackUnderflowError + data_structures.stacks.stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError @@ -89,13 +89,13 @@ def test_stack() -> None: try: _ = stack.pop() assert False # This should not happen - except IndexError: + except StackUnderflowError: assert True # This should happen try: _ = stack.peek() assert False # This should not happen - except IndexError: + except StackUnderflowError: assert True # This should happen for i in range(10):