-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
added stack_using_two_queues.py #10080
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8ebf863
added stack_using_two_queues.py
Samar-28 494e92f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] add19ec
Update stack_using_two_queues.py
Samar-28 98bb070
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 6ead57f
Update stack_using_two_queues.py
Samar-28 d195328
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a04fcae
Update stack_using_two_queues.py
Samar-28 1b3fba0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 952b5a4
Update stack_using_two_queues.py
Samar-28 4e6c600
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 3403da7
Update stack_using_two_queues.py
Samar-28 57c235a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
from collections import deque | ||
|
||
class StackUsingQueues: | ||
|
||
def __init__(self): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide return type hint for the function: |
||
self.queue1 = deque() | ||
self.queue2 = deque() | ||
|
||
def push(self, value: int) -> None: | ||
""" | ||
Pushes an element onto the stack. | ||
|
||
Args: | ||
value (int): The element to push onto the stack. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.push(3) | ||
""" | ||
self.queue2.append(value) | ||
while self.queue1: | ||
self.queue2.append(self.queue1.popleft()) | ||
self.queue1, self.queue2 = self.queue2, self.queue1 | ||
|
||
def pop(self) -> None: | ||
""" | ||
Pops and removes the top element from the stack. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.pop() | ||
""" | ||
if self.queue1: | ||
self.queue1.popleft() | ||
|
||
def top(self) -> int: | ||
""" | ||
Returns the top element of the stack without removing it. | ||
|
||
Returns: | ||
int: The top element of the stack. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.top() | ||
2 | ||
""" | ||
if self.queue1: | ||
return self.queue1[0] | ||
return None | ||
|
||
def size(self) -> int: | ||
""" | ||
Returns the current size (number of elements) of the stack. | ||
|
||
Returns: | ||
int: The size of the stack. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.size() | ||
0 | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.push(3) | ||
>>> stack.size() | ||
3 | ||
>>> stack.pop() | ||
>>> stack.size() | ||
2 | ||
""" | ||
return len(self.queue1) | ||
|
||
def is_empty(self) -> bool: | ||
""" | ||
Checks if the stack is empty. | ||
|
||
Returns: | ||
bool: True if the stack is empty, False otherwise. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.is_empty() | ||
True | ||
>>> stack.push(1) | ||
>>> stack.is_empty() | ||
False | ||
""" | ||
return len(self.queue1) == 0 | ||
|
||
def peek(self) -> int: | ||
""" | ||
Returns the top element of the stack without removing it. | ||
|
||
Returns: | ||
int: The top element of the stack. | ||
|
||
Example: | ||
>>> stack = StackUsingQueues() | ||
>>> stack.push(1) | ||
>>> stack.push(2) | ||
>>> stack.peek() | ||
2 | ||
""" | ||
if self.queue1: | ||
return self.queue1[0] | ||
return None | ||
|
||
if __name__ == "__main__": | ||
stack = StackUsingQueues() | ||
|
||
# Push some elements onto the stack | ||
stack.push(1) | ||
stack.push(2) | ||
stack.push(3) | ||
|
||
print("Stack size:", stack.size()) | ||
print("Top element:", stack.top()) | ||
|
||
# Pop elements from the stack | ||
stack.pop() | ||
print("Top element after pop:", stack.top()) | ||
|
||
stack.pop() | ||
print("Top element after another pop:", stack.top()) | ||
|
||
print("Is stack empty?", stack.is_empty()) | ||
print("Peek at the top element:", stack.peek()) | ||
print("Final stack size:", stack.size()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide return type hint for the function:
__init__
. If the function does not return a value, please provide the type hint as:def function() -> None: