Python/data_structures/arrays/permutations.py
Caeden Perelli-Harris fceacf977f
Fix type errors in permutations (#9007)
* updating DIRECTORY.md

* types(permuations): Rename permute2

* Apply suggestions from code review

Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>

* fix(permutations): Call permute_recursive

* fix(permutations): Correct permutations order

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Tianyi Zheng <tianyizheng02@gmail.com>
2023-08-22 02:33:47 -07:00

49 lines
1.3 KiB
Python

def permute_recursive(nums: list[int]) -> list[list[int]]:
"""
Return all permutations.
>>> permute_recursive([1, 2, 3])
[[3, 2, 1], [2, 3, 1], [1, 3, 2], [3, 1, 2], [2, 1, 3], [1, 2, 3]]
"""
result: list[list[int]] = []
if len(nums) == 0:
return [[]]
for _ in range(len(nums)):
n = nums.pop(0)
permutations = permute_recursive(nums)
for perm in permutations:
perm.append(n)
result.extend(permutations)
nums.append(n)
return result
def permute_backtrack(nums: list[int]) -> list[list[int]]:
"""
Return all permutations of the given list.
>>> permute_backtrack([1, 2, 3])
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]
"""
def backtrack(start: int) -> None:
if start == len(nums) - 1:
output.append(nums[:])
else:
for i in range(start, len(nums)):
nums[start], nums[i] = nums[i], nums[start]
backtrack(start + 1)
nums[start], nums[i] = nums[i], nums[start] # backtrack
output: list[list[int]] = []
backtrack(0)
return output
if __name__ == "__main__":
import doctest
res = permute_backtrack([1, 2, 3])
print(res)
doctest.testmod()