Skip to content

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
merged 7 commits into from
Apr 8, 2023

Conversation

amirsoroush
Copy link
Contributor

Queue using two Stacks:

LQJBQ

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".


  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Apr 7, 2023
@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Apr 7, 2023
@amirsoroush
Copy link
Contributor Author

Apparently ruff is not happy with my commit. It says:

Error: computer_vision/lenet_pytorch.py:9:8: PLR0402 Use `from torch import nn` in lieu of alias
Error: Process completed with exit code 1.

Unfortunately I have no idea about it. I didn't import anything from torch.

@CaedenPH
Copy link
Contributor

CaedenPH commented Apr 7, 2023

Apparently ruff is not happy with my commit. It says:

Error: computer_vision/lenet_pytorch.py:9:8: PLR0402 Use `from torch import nn` in lieu of alias
Error: Process completed with exit code 1.

Unfortunately I have no idea about it. I didn't import anything from torch.

Seems like you didn't touch that file so someone must've merged a commit that failed ruff

@amirsoroush
Copy link
Contributor Author

@CaedenPH I guess the failure is because of the merged commit which is currently requested to be reverted here.

@algorithms-keeper algorithms-keeper bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Apr 8, 2023
@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Apr 8, 2023
@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Apr 8, 2023
@cclauss
Copy link
Member

cclauss commented Apr 8, 2023

For readability, please move the "magic" functions like .__len__() and .str()before.get()and.put()`

@amirsoroush amirsoroush requested a review from cclauss April 8, 2023 09:20
Copy link
Member

@cclauss cclauss left a 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.

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Apr 8, 2023
@cclauss cclauss merged commit 5cb0a00 into TheAlgorithms:master Apr 8, 2023
@amirsoroush
Copy link
Contributor Author

@cclauss Thank you for your advice and also for introducing me to ruff linter.

For .size(), "result == expected is True", "implementing __str__", I wanna say that I wrote them because they've been implemented in other files. I tried to be consistent with those but I'm totally agree with you. I'll also fix those in the future.

@amirsoroush amirsoroush deleted the Queue_with_two_stacks branch April 8, 2023 14:04
@cclauss
Copy link
Member

cclauss commented Apr 8, 2023

Thanks for your contribution. Our codebase is not (yet) perfect so please submit pull requests for any existing algorithms that can be improved.

tianyizheng02 pushed a commit to tianyizheng02/Python that referenced this pull request May 29, 2023
* 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]>
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* 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]>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants