mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
Compare commits
2 Commits
04fd5c1b5e
...
fceacf977f
Author | SHA1 | Date | |
---|---|---|---|
|
fceacf977f | ||
|
c7aeaa3fd8 |
@ -16,7 +16,7 @@ repos:
|
|||||||
- id: auto-walrus
|
- id: auto-walrus
|
||||||
|
|
||||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||||
rev: v0.0.284
|
rev: v0.0.285
|
||||||
hooks:
|
hooks:
|
||||||
- id: ruff
|
- id: ruff
|
||||||
|
|
||||||
@ -46,12 +46,12 @@ repos:
|
|||||||
pass_filenames: false
|
pass_filenames: false
|
||||||
|
|
||||||
- repo: https://github.com/abravalheri/validate-pyproject
|
- repo: https://github.com/abravalheri/validate-pyproject
|
||||||
rev: v0.13
|
rev: v0.14
|
||||||
hooks:
|
hooks:
|
||||||
- id: validate-pyproject
|
- id: validate-pyproject
|
||||||
|
|
||||||
- repo: https://github.com/pre-commit/mirrors-mypy
|
- repo: https://github.com/pre-commit/mirrors-mypy
|
||||||
rev: v1.5.0
|
rev: v1.5.1
|
||||||
hooks:
|
hooks:
|
||||||
- id: mypy
|
- id: mypy
|
||||||
args:
|
args:
|
||||||
|
@ -155,6 +155,7 @@
|
|||||||
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
|
* [Hexadecimal To Decimal](conversions/hexadecimal_to_decimal.py)
|
||||||
* [Length Conversion](conversions/length_conversion.py)
|
* [Length Conversion](conversions/length_conversion.py)
|
||||||
* [Molecular Chemistry](conversions/molecular_chemistry.py)
|
* [Molecular Chemistry](conversions/molecular_chemistry.py)
|
||||||
|
* [Octal To Binary](conversions/octal_to_binary.py)
|
||||||
* [Octal To Decimal](conversions/octal_to_decimal.py)
|
* [Octal To Decimal](conversions/octal_to_decimal.py)
|
||||||
* [Prefix Conversions](conversions/prefix_conversions.py)
|
* [Prefix Conversions](conversions/prefix_conversions.py)
|
||||||
* [Prefix Conversions String](conversions/prefix_conversions_string.py)
|
* [Prefix Conversions String](conversions/prefix_conversions_string.py)
|
||||||
|
@ -1,17 +1,16 @@
|
|||||||
def permute(nums: list[int]) -> list[list[int]]:
|
def permute_recursive(nums: list[int]) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return all permutations.
|
Return all permutations.
|
||||||
>>> from itertools import permutations
|
|
||||||
>>> numbers= [1,2,3]
|
>>> permute_recursive([1, 2, 3])
|
||||||
>>> all(list(nums) in permute(numbers) for nums in permutations(numbers))
|
[[3, 2, 1], [2, 3, 1], [1, 3, 2], [3, 1, 2], [2, 1, 3], [1, 2, 3]]
|
||||||
True
|
|
||||||
"""
|
"""
|
||||||
result = []
|
result: list[list[int]] = []
|
||||||
if len(nums) == 1:
|
if len(nums) == 0:
|
||||||
return [nums.copy()]
|
return [[]]
|
||||||
for _ in range(len(nums)):
|
for _ in range(len(nums)):
|
||||||
n = nums.pop(0)
|
n = nums.pop(0)
|
||||||
permutations = permute(nums)
|
permutations = permute_recursive(nums)
|
||||||
for perm in permutations:
|
for perm in permutations:
|
||||||
perm.append(n)
|
perm.append(n)
|
||||||
result.extend(permutations)
|
result.extend(permutations)
|
||||||
@ -19,15 +18,15 @@ def permute(nums: list[int]) -> list[list[int]]:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def permute2(nums):
|
def permute_backtrack(nums: list[int]) -> list[list[int]]:
|
||||||
"""
|
"""
|
||||||
Return all permutations of the given list.
|
Return all permutations of the given list.
|
||||||
|
|
||||||
>>> permute2([1, 2, 3])
|
>>> permute_backtrack([1, 2, 3])
|
||||||
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]
|
[[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 2, 1], [3, 1, 2]]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def backtrack(start):
|
def backtrack(start: int) -> None:
|
||||||
if start == len(nums) - 1:
|
if start == len(nums) - 1:
|
||||||
output.append(nums[:])
|
output.append(nums[:])
|
||||||
else:
|
else:
|
||||||
@ -36,7 +35,7 @@ def permute2(nums):
|
|||||||
backtrack(start + 1)
|
backtrack(start + 1)
|
||||||
nums[start], nums[i] = nums[i], nums[start] # backtrack
|
nums[start], nums[i] = nums[i], nums[start] # backtrack
|
||||||
|
|
||||||
output = []
|
output: list[list[int]] = []
|
||||||
backtrack(0)
|
backtrack(0)
|
||||||
return output
|
return output
|
||||||
|
|
||||||
@ -44,7 +43,6 @@ def permute2(nums):
|
|||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import doctest
|
import doctest
|
||||||
|
|
||||||
# use res to print the data in permute2 function
|
res = permute_backtrack([1, 2, 3])
|
||||||
res = permute2([1, 2, 3])
|
|
||||||
print(res)
|
print(res)
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user