-
-
Notifications
You must be signed in to change notification settings - Fork 47k
added algo for finding permutations of an array #7614
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
Changes from all commits
128020b
0fe013e
a7d03f8
3f5d5cb
9258074
09e8147
f36374b
3def3fb
84a7e46
7cbdfff
677af80
1d8ead9
5396075
d78301c
38b690f
3638192
e73d2ca
6f82262
a604334
a1c3cd0
9cad4b8
b192a96
b490971
bb480be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,26 @@ | ||||||||
def permute(nums: list[int]) -> list[list[int]]: | ||||||||
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
rajansh87 marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
""" | ||||||||
Return all permutations. | ||||||||
|
||||||||
>>> from itertools import permutations | ||||||||
>>> numbers= [1,2,3] | ||||||||
>>> all(list(nums) in permute(numbers) for nums in permutations(numbers)) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||
True | ||||||||
""" | ||||||||
result = [] | ||||||||
if len(nums) == 1: | ||||||||
return [nums.copy()] | ||||||||
for _ in range(len(nums)): | ||||||||
n = nums.pop(0) | ||||||||
Comment on lines
+13
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think you just want to loop over each element in the array? |
||||||||
permutations = permute(nums) | ||||||||
for perm in permutations: | ||||||||
perm.append(n) | ||||||||
result.extend(permutations) | ||||||||
nums.append(n) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're mutating the input in-place! That is a very bad idea. The function is returning the result, so why are you mutating the input? |
||||||||
return result | ||||||||
|
||||||||
|
||||||||
if __name__ == "__main__": | ||||||||
import doctest | ||||||||
|
||||||||
doctest.testmod() |
Uh oh!
There was an error while loading. Please reload this page.