Skip to content

Check if a item exist in stack or not #1931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from May 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion data_structures/stacks/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ def is_empty(self):
def size(self):
""" Return the size of the stack."""
return len(self.stack)


def __contains__(self, item) -> bool:
"""Check if item is in stack"""
return item in self.stack


class StackOverflowError(BaseException):
pass
Expand All @@ -66,3 +70,7 @@ 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 num in stack:
print(f"{num} is in stack")