-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
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
Queue implementation using two Stacks #8617
Conversation
Apparently ruff is not happy with my commit. It says:
Unfortunately I have no idea about it. I didn't import anything from |
Seems like you didn't touch that file so someone must've merged a commit that failed ruff |
For readability, please move the "magic" functions like |
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.
LGTM
A few changes:
- If you can use something immediately without naming it then do so to keep doctests short
- Test the result, not the result == expected is True
- Use repr() instead of str() in tests unless essential.
- Favor tuples over lists when possible because they take less RAM.
- We do not need a .size()` method if we have defined len.
- The filename should match the class that is inside it.
@cclauss Thank you for your advice and also for introducing me to For |
Thanks for your contribution. Our codebase is not (yet) perfect so please submit pull requests for any existing algorithms that can be improved. |
* Queue implementation using two Stacks * fix typo in queue/queue_on_two_stacks.py * add 'iterable' to queue_on_two_stacks initializer * make queue_on_two_stacks.py generic class * fix ruff-UP007 in queue_on_two_stacks.py * enhance readability in queue_on_two_stacks.py * Create queue_by_two_stacks.py --------- Co-authored-by: Christian Clauss <[email protected]>
* Queue implementation using two Stacks * fix typo in queue/queue_on_two_stacks.py * add 'iterable' to queue_on_two_stacks initializer * make queue_on_two_stacks.py generic class * fix ruff-UP007 in queue_on_two_stacks.py * enhance readability in queue_on_two_stacks.py * Create queue_by_two_stacks.py --------- Co-authored-by: Christian Clauss <[email protected]>
Queue using two Stacks:
Here is the implementation of Queue using two stacks. It has O(1) time complexity for en-queuing and Amortized O(1) for de-queuing. The worst case time complexity for de-queuing is O(n) though. The rationale behind this and its use-cases are mentioned here and here.
I also included doctest for methods. It's formatted using "Black" and checked by "Mypy".
Checklist:
Fixes: #{$ISSUE_NO}
.