Skip to content

Commit b6fcee3

Browse files
author
Akash
authored
Check if a item exist in stack or not (TheAlgorithms#1931)
* Check if a item exist in stack or not * implemented __contains__ method in stack * made changes in __contains__
1 parent 853741e commit b6fcee3

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

data_structures/stacks/stack.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ def is_empty(self):
4646
def size(self):
4747
""" Return the size of the stack."""
4848
return len(self.stack)
49-
49+
50+
def __contains__(self, item) -> bool:
51+
"""Check if item is in stack"""
52+
return item in self.stack
53+
5054

5155
class StackOverflowError(BaseException):
5256
pass
@@ -66,3 +70,7 @@ class StackOverflowError(BaseException):
6670
print("After push(100), the stack is now: " + str(stack))
6771
print("is_empty(): " + str(stack.is_empty()))
6872
print("size(): " + str(stack.size()))
73+
num = 5
74+
if num in stack:
75+
print(f"{num} is in stack")
76+

0 commit comments

Comments
 (0)