mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
Check if a item exist in stack or not (#1931)
* Check if a item exist in stack or not * implemented __contains__ method in stack * made changes in __contains__
This commit is contained in:
parent
853741e518
commit
b6fcee3114
|
@ -47,6 +47,10 @@ class Stack:
|
||||||
""" Return the size of the stack."""
|
""" Return the size of the stack."""
|
||||||
return len(self.stack)
|
return len(self.stack)
|
||||||
|
|
||||||
|
def __contains__(self, item) -> bool:
|
||||||
|
"""Check if item is in stack"""
|
||||||
|
return item in self.stack
|
||||||
|
|
||||||
|
|
||||||
class StackOverflowError(BaseException):
|
class StackOverflowError(BaseException):
|
||||||
pass
|
pass
|
||||||
|
@ -66,3 +70,7 @@ if __name__ == "__main__":
|
||||||
print("After push(100), the stack is now: " + str(stack))
|
print("After push(100), the stack is now: " + str(stack))
|
||||||
print("is_empty(): " + str(stack.is_empty()))
|
print("is_empty(): " + str(stack.is_empty()))
|
||||||
print("size(): " + str(stack.size()))
|
print("size(): " + str(stack.size()))
|
||||||
|
num = 5
|
||||||
|
if num in stack:
|
||||||
|
print(f"{num} is in stack")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user