-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Queue implementation using two Stacks #8617
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
cclauss
merged 7 commits into
TheAlgorithms:master
from
amirsoroush:Queue_with_two_stacks
Apr 8, 2023
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
49ce661
Queue implementation using two Stacks
amirsoroush 37bd353
fix typo in queue/queue_on_two_stacks.py
amirsoroush 1da0297
add 'iterable' to queue_on_two_stacks initializer
amirsoroush 100ec02
make queue_on_two_stacks.py generic class
amirsoroush d9accd5
fix ruff-UP007 in queue_on_two_stacks.py
amirsoroush 953660b
enhance readability in queue_on_two_stacks.py
amirsoroush a8eb743
Create queue_by_two_stacks.py
cclauss 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 hidden or 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,123 @@ | ||
"""Queue implementation using two stacks""" | ||
|
||
from typing import Any | ||
|
||
|
||
class Queue: | ||
def __init__(self) -> None: | ||
amirsoroush marked this conversation as resolved.
Show resolved
Hide resolved
|
||
self._stack1: list[Any] = [] | ||
self._stack2: list[Any] = [] | ||
|
||
def put(self, item: Any) -> None: | ||
""" | ||
Put `item` into the Queue | ||
|
||
>>> queue = Queue() | ||
>>> queue.put(10) | ||
>>> queue.put(20) | ||
>>> len(queue) == 2 | ||
True | ||
>>> str(queue) | ||
'<[10, 20]>' | ||
""" | ||
|
||
self._stack1.append(item) | ||
|
||
def get(self) -> Any: | ||
""" | ||
Get `item` from the Queue | ||
|
||
>>> queue = Queue() | ||
>>> for i in (10, 20, 30): | ||
... queue.put(i) | ||
>>> len(queue) | ||
3 | ||
>>> queue.get() | ||
10 | ||
>>> queue.get() | ||
20 | ||
>>> len(queue) == 1 | ||
True | ||
>>> queue.get() | ||
30 | ||
>>> queue.get() | ||
Traceback (most recent call last): | ||
... | ||
IndexError: Queue is empty | ||
""" | ||
|
||
# To reduce number of attribute look-ups in `while` loop. | ||
stack1_pop = self._stack1.pop | ||
stack2_append = self._stack2.append | ||
|
||
if not self._stack2: | ||
while self._stack1: | ||
stack2_append(stack1_pop()) | ||
|
||
if not self._stack2: | ||
raise IndexError("Queue is empty") | ||
return self._stack2.pop() | ||
|
||
def size(self) -> int: | ||
""" | ||
Returns the length of the Queue | ||
|
||
>>> queue = Queue() | ||
>>> queue.size() | ||
0 | ||
>>> queue.put(10) | ||
>>> queue.put(20) | ||
>>> queue.size() | ||
2 | ||
>>> queue.get() | ||
10 | ||
>>> queue.size() == 1 | ||
True | ||
""" | ||
|
||
return len(self) | ||
|
||
def __str__(self) -> str: | ||
amirsoroush marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
>>> queue = Queue() | ||
>>> str(queue) | ||
'<[]>' | ||
>>> queue.put(10) | ||
>>> str(queue) | ||
'<[10]>' | ||
>>> queue.put(20) | ||
>>> str(queue) | ||
'<[10, 20]>' | ||
amirsoroush marked this conversation as resolved.
Show resolved
Hide resolved
|
||
>>> queue.get() | ||
10 | ||
>>> queue.put(30) | ||
>>> str(queue) == '<[20, 30]>' | ||
True | ||
""" | ||
|
||
return f"<{self._stack2[::-1] + self._stack1}>" | ||
|
||
def __len__(self) -> int: | ||
""" | ||
>>> queue = Queue() | ||
>>> for i in range(1, 11): | ||
... queue.put(i) | ||
... | ||
>>> len(queue) == 10 | ||
True | ||
>>> for i in range(2): | ||
... queue.get() | ||
... | ||
1 | ||
2 | ||
>>> len(queue) == 8 | ||
True | ||
""" | ||
|
||
return len(self._stack1) + len(self._stack2) | ||
|
||
|
||
if __name__ == "__main__": | ||
from doctest import testmod | ||
|
||
testmod() |
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.
Uh oh!
There was an error while loading. Please reload this page.