Skip to content

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

Merged
merged 24 commits into from
Oct 29, 2022
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
128020b
Add files via upload
rajansh87 Oct 25, 2022
0fe013e
Delete permutations.cpython-310.pyc
rajansh87 Oct 25, 2022
a7d03f8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2022
3f5d5cb
Update permutations.py
rajansh87 Oct 25, 2022
9258074
Update permutations.py
rajansh87 Oct 25, 2022
09e8147
Add files via upload
rajansh87 Oct 25, 2022
f36374b
Delete permutations.py
rajansh87 Oct 25, 2022
3def3fb
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2022
84a7e46
Update permutations.py
rajansh87 Oct 25, 2022
7cbdfff
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2022
677af80
Update permutations.py
rajansh87 Oct 25, 2022
1d8ead9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2022
5396075
Update permutations.py
rajansh87 Oct 25, 2022
d78301c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 25, 2022
38b690f
Update data_structures/arrays/permutations.py
rajansh87 Oct 26, 2022
3638192
Update permutations.py
rajansh87 Oct 26, 2022
e73d2ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
6f82262
Update permutations.py
rajansh87 Oct 26, 2022
a604334
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
a1c3cd0
Update data_structures/arrays/permutations.py
rajansh87 Oct 26, 2022
9cad4b8
Update permutations.py
rajansh87 Oct 26, 2022
b192a96
Update permutations.py
rajansh87 Oct 26, 2022
b490971
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Oct 26, 2022
bb480be
Update permutations.py
cclauss Oct 29, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions data_structures/arrays/permutations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
def permute(nums: list[int]) -> list[list[int]]:
"""
Return all permutations.

>>> from itertools import permutations
>>> numbers= [1,2,3]
>>> all(list(nums) in permute(numbers) for nums in permutations(numbers))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add more tests like when the input is an empty list, list with 1 number, multiple numbers, etc. Test out the algorithm as a black box and white box.

True
"""
result = []
if len(nums) == 1:
return [nums.copy()]
for _ in range(len(nums)):
n = nums.pop(0)
Comment on lines +13 to +14
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for _ in range(len(nums)):
n = nums.pop(0)
for n in nums:

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)
Copy link
Member

Choose a reason for hiding this comment

The 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()