-
-
Notifications
You must be signed in to change notification settings - Fork 46.7k
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
Merged
Merged
Heaps algorithm #2475
Changes from 13 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
1f99120
Merge remote-tracking branch 'upstream/master'
grochedix b5769f7
Merge remote-tracking branch 'upstream/master'
grochedix 3708397
Merge remote-tracking branch 'upstream/master'
grochedix 4cfb62f
Merge branch 'master' of https://github.com/TheAlgorithms/Python
grochedix 86b62a9
Merge remote-tracking branch 'upstream/master'
grochedix 28a56c8
Merge remote-tracking branch 'upstream/master'
grochedix fb2b5ba
Merge remote-tracking branch 'upstream/master'
grochedix d8934d5
Merge remote-tracking branch 'upstream/master'
grochedix 8a575f7
Merge remote-tracking branch 'upstream/master'
grochedix e8539c6
Merge remote-tracking branch 'upstream/master'
grochedix 4efe882
heaps_algorithm: new algo
grochedix d8f8fb3
typo
grochedix 662aa3d
correction doctest
grochedix a6adcb1
doctests: compare with itertools.permutations
grochedix d613539
doctest: sorted instead of set
grochedix 32add91
doctest
grochedix 4785a1d
doctest
grochedix f751cd3
rebuild
grochedix 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,50 @@ | ||
""" | ||
Heap's algorithm returns the list of all permutations possible from a list. | ||
It minimizes movement by generating each permutation from the previous one | ||
by swapping only two elements. | ||
More information: | ||
https://en.wikipedia.org/wiki/Heap%27s_algorithm. | ||
""" | ||
|
||
|
||
def heaps(arr: list) -> list: | ||
""" | ||
Pure python implementation of the Heap's algorithm, | ||
returning all permutations of a list. | ||
>>> heaps([]) | ||
[[]] | ||
>>> 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]] | ||
""" | ||
|
||
if len(arr) <= 1: | ||
return [arr] | ||
|
||
res = [] | ||
|
||
def generate(k: int, arr: list): | ||
if k == 1: | ||
res.append(arr[:]) | ||
return | ||
|
||
generate(k - 1, arr) | ||
|
||
for i in range(k - 1): | ||
if k % 2 == 0: # k is even | ||
arr[i], arr[k - 1] = arr[k - 1], arr[i] | ||
else: # k is odd | ||
arr[0], arr[k - 1] = arr[k - 1], arr[0] | ||
generate(k - 1, arr) | ||
|
||
generate(len(arr), arr) | ||
return res | ||
|
||
|
||
if __name__ == "__main__": | ||
user_input = input("Enter numbers separated by a comma:\n").strip() | ||
arr = [int(item) for item in user_input.split(",")] | ||
print(heaps(arr)) |
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.
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 againsthttps://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 worksorted(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 bothpermutations()
andheaps()
.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.