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 1 commit
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
8 changes: 7 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 is_exist(self, item):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"""Check if a particular item is in set"""
return item in self.stack


class StackOverflowError(BaseException):
pass
Expand All @@ -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")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
num = 5
if stack.is_exist(num): print("num is in stack")
num = 5
if num in stack:
print(f"{num} is in stack")