Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upHeaps algorithm #2475
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]] |
cclauss
Sep 24, 2020
•
Member
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.
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.
grochedix
Sep 24, 2020
Author
Contributor
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.
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.
cclauss
Sep 24, 2020
•
Member
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
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
grochedix
Sep 24, 2020
Author
Contributor
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).
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).
cclauss
Sep 24, 2020
Member
tuple(permutations(data)) and if that does not work sorted(permutations(data))
tuple(permutations(data)) and if that does not work sorted(permutations(data))
grochedix
Sep 25, 2020
Author
Contributor
Used sorted() but had to used it on both permutations() and heaps().
Used sorted() but had to used it on both permutations() and heaps().
grochedix
Sep 28, 2020
Author
Contributor
Updated with your comment relative to the iterative version.
Updated with your comment relative to the iterative version.
|
Hey @grochedix, TravisCI finished with status TravisBuddy Request Identifier: 9d04df70-01b6-11eb-9be6-0d9634a5fbe7 |
|
LGTM |


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