From 831fab38ad84d94960dc6e71fb755300f3f00e95 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Thu, 9 Nov 2023 12:31:40 +0530 Subject: [PATCH 1/3] Added doctest to stack.py --- data_structures/stacks/stack.py | 96 ++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index a14f4648a399..ccd907f2273f 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -33,7 +33,23 @@ def __str__(self) -> str: return str(self.stack) def push(self, data: T) -> None: - """Push an element to the top of the stack.""" + """ + Push an element to the top of the stack. + + >>> S =Stack(2) # stack size = 2 + >>> S.push(10) + >>> S.push(20) + >>> print(S) + [10, 20] + + >>> S =Stack(1) # stack size = 1 + >>> S.push(10) + >>> S.push(20) + Traceback (most recent call last): + ... + stack.StackOverflowError + + """ if len(self.stack) >= self.limit: raise StackOverflowError self.stack.append(data) @@ -42,10 +58,16 @@ def pop(self) -> T: """ Pop an element off of the top of the stack. + >>> S = Stack() + >>> S.push(-5) + >>> S.push(10) + >>> S.pop() + 10 + >>> Stack().pop() Traceback (most recent call last): ... - data_structures.stacks.stack.StackUnderflowError + stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError @@ -55,28 +77,84 @@ def peek(self) -> T: """ Peek at the top-most element of the stack. - >>> Stack().pop() + >>> S = Stack() + >>> S.push(-5) + >>> S.push(10) + >>> S.peek() + 10 + + >>> Stack().peek() Traceback (most recent call last): ... - data_structures.stacks.stack.StackUnderflowError + stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError return self.stack[-1] def is_empty(self) -> bool: - """Check if a stack is empty.""" + """ + Check if a stack is empty. + + >>> S = Stack() + >>> S.is_empty() + True + + >>> S = Stack() + >>> S.push(10) + >>> S.is_empty() + False + """ return not bool(self.stack) def is_full(self) -> bool: + """ + >>> S = Stack() + >>> S.is_full() + False + + >>> S = Stack(1) + >>> S.push(10) + >>> S.is_full() + True + """ return self.size() == self.limit def size(self) -> int: - """Return the size of the stack.""" + """ + Return the size of the stack. + + >>> S = Stack(3) + >>> S.size() + 0 + + >>> S = Stack(3) + >>> S.push(10) + >>> S.size() + 1 + + >>> S = Stack(3) + >>> S.push(10) + >>> S.push(20) + >>> S.size() + 2 + """ return len(self.stack) def __contains__(self, item: T) -> bool: - """Check if item is in stack""" + """ + Check if item is in stack + + >>> S = Stack(3) + >>> S.push(10) + >>> S.__contains__(10) + True + + >>> S = Stack(3) + >>> S.push(10) + >>> S.__contains__(20) + False + """ return item in self.stack @@ -131,3 +209,7 @@ def test_stack() -> None: if __name__ == "__main__": test_stack() + + import doctest + + doctest.testmod() From 51214e8c4f9ee9e733e1c1c7dabc8acef56b2923 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Thu, 9 Nov 2023 12:37:51 +0530 Subject: [PATCH 2/3] Update stack.py --- 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 ccd907f2273f..2cf6a99aa300 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -47,7 +47,7 @@ def push(self, data: T) -> None: >>> S.push(20) Traceback (most recent call last): ... - stack.StackOverflowError + data_structures.stacks.stack.StackOverflowError """ if len(self.stack) >= self.limit: @@ -67,7 +67,7 @@ def pop(self) -> T: >>> Stack().pop() Traceback (most recent call last): ... - stack.StackUnderflowError + data_structures.stacks.stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError @@ -86,7 +86,7 @@ def peek(self) -> T: >>> Stack().peek() Traceback (most recent call last): ... - stack.StackUnderflowError + data_structures.stacks.stack.StackUnderflowError """ if not self.stack: raise StackUnderflowError From e7e02c8d33b3f5408313852362520d9b27e30764 Mon Sep 17 00:00:00 2001 From: Suyash Dongre <109069262+Suyashd999@users.noreply.github.com> Date: Sat, 11 Nov 2023 08:45:52 +0530 Subject: [PATCH 3/3] 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 2cf6a99aa300..93698f5aa116 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -36,13 +36,13 @@ def push(self, data: T) -> None: """ Push an element to the top of the stack. - >>> S =Stack(2) # stack size = 2 + >>> S = Stack(2) # stack size = 2 >>> S.push(10) >>> S.push(20) >>> print(S) [10, 20] - >>> S =Stack(1) # stack size = 1 + >>> S = Stack(1) # stack size = 1 >>> S.push(10) >>> S.push(20) Traceback (most recent call last): @@ -147,12 +147,12 @@ def __contains__(self, item: T) -> bool: >>> S = Stack(3) >>> S.push(10) - >>> S.__contains__(10) + >>> 10 in S True >>> S = Stack(3) >>> S.push(10) - >>> S.__contains__(20) + >>> 20 in S False """ return item in self.stack