From 96f231737540a68ff490fc20cbd108351de0f360 Mon Sep 17 00:00:00 2001 From: Akash Date: Sat, 2 May 2020 22:33:48 +0530 Subject: [PATCH 1/3] Check if a item exist in stack or not --- data_structures/stacks/stack.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index 53a40a7b7ebc..2ed7845db695 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -46,7 +46,11 @@ def is_empty(self): def size(self): """ Return the size of the stack.""" return len(self.stack) - + + def is_exist(self, item): + """Check if a particular item is in set""" + return item in self.stack + class StackOverflowError(BaseException): pass @@ -66,3 +70,5 @@ class StackOverflowError(BaseException): print("After push(100), the stack is now: " + str(stack)) print("is_empty(): " + str(stack.is_empty())) print("size(): " + str(stack.size())) + num = 5 + if stack.is_exist(num): print("num is in stack") From eab47be5a592866dbd5e015ed4ca218f807ee9ea Mon Sep 17 00:00:00 2001 From: Akash Date: Sun, 3 May 2020 18:26:43 +0530 Subject: [PATCH 2/3] implemented __contains__ method in stack --- 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 2ed7845db695..ab4908b7c3e4 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -47,8 +47,8 @@ def size(self): """ Return the size of the stack.""" return len(self.stack) - def is_exist(self, item): - """Check if a particular item is in set""" + def __contains__(self, item): + """Check if a particular item is in stack""" return item in self.stack From 4d50f9e53f4aacf477e6f991e85cd458702f23b0 Mon Sep 17 00:00:00 2001 From: Akash Date: Sun, 3 May 2020 19:37:08 +0530 Subject: [PATCH 3/3] made changes in __contains__ --- data_structures/stacks/stack.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/data_structures/stacks/stack.py b/data_structures/stacks/stack.py index ab4908b7c3e4..a96275aa8df2 100644 --- a/data_structures/stacks/stack.py +++ b/data_structures/stacks/stack.py @@ -47,8 +47,8 @@ def size(self): """ Return the size of the stack.""" return len(self.stack) - def __contains__(self, item): - """Check if a particular item is in stack""" + def __contains__(self, item) -> bool: + """Check if item is in stack""" return item in self.stack @@ -71,4 +71,6 @@ class StackOverflowError(BaseException): print("is_empty(): " + str(stack.is_empty())) print("size(): " + str(stack.size())) num = 5 - if stack.is_exist(num): print("num is in stack") + if num in stack: + print(f"{num} is in stack") +