-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
Heaps algorithm #2475
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
Heaps algorithm #2475
Conversation
[[]] | ||
>>> heaps([0]) | ||
[[0]] | ||
>>> heaps([-1, 1]) | ||
[[-1, 1], [1, -1]] | ||
>>> heaps([1, 2, 3]) | ||
[[1, 2, 3], [2, 1, 3], [3, 1, 2], [1, 3, 2], [2, 3, 1], [3, 2, 1]] |
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.
Instead of (or in addition to) manual doctest results, please test this against https://docs.python.org/3/library/itertools.html#itertools.permutations to ensure they both deliver identical results.
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.
Done. Added one comparison between my algo and itertools permutations. Also outputed my permutations as list of tuples instead of list of lists to match itertools.
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.
Why the use of set()
? Perhaps you should be comparing against
https://docs.python.org/3/library/itertools.html#itertools.combinations or https://docs.python.org/3/library/itertools.html#itertools.combinations_with_replacement
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.
I use set()
since i have to compare the result of a generator (itertools) vs mine (list of tuple), as well as the order of tuples might also be different from one version to the other but can still be correct. Maybe you see another "cleaner" way of doing this ?
I don't think comparing with combinations is a good idea, the Heap's algorithm outputs permutations (order of elements matters).
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.
tuple(permutations(data))
and if that does not work sorted(permutations(data))
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.
Used sorted()
but had to used it on both permutations()
and heaps()
.
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.
Updated with your comment relative to the iterative version.
Hey @grochedix, TravisCI finished with status TravisBuddy Request Identifier: 9d04df70-01b6-11eb-9be6-0d9634a5fbe7 |
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
* heaps_algorithm: new algo * typo * correction doctest * doctests: compare with itertools.permutations * doctest: sorted instead of set * doctest * doctest * rebuild
* heaps_algorithm: new algo * typo * correction doctest * doctests: compare with itertools.permutations * doctest: sorted instead of set * doctest * doctest * rebuild
Describe your change:
Added the Heap's algorithm under the "divide and conquer" folder. It outputs all possible permutations from a list of elements.
Checklist:
Fixes: #{$ISSUE_NO}
.