From 841ca3d660701594dea1d80e2213fb7149d424aa Mon Sep 17 00:00:00 2001 From: UmairWaseef <117443651+UmairWaseef@users.noreply.github.com> Date: Wed, 30 Oct 2024 11:32:15 +0530 Subject: [PATCH 1/2] Add combinations function for array inputs --- data_structures/arrays/new_combination.py | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 data_structures/arrays/new_combination.py diff --git a/data_structures/arrays/new_combination.py b/data_structures/arrays/new_combination.py new file mode 100644 index 000000000000..4b347fa142b8 --- /dev/null +++ b/data_structures/arrays/new_combination.py @@ -0,0 +1,64 @@ +from itertools import combinations + +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.copy()) + 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 + +def get_combinations(nums: list[int], r: int) -> list[list[int]]: + """ + Return all combinations of the given list of length r. + + >>> get_combinations([1, 2, 3], 2) + [[1, 2], [1, 3], [2, 3]] + """ + return [list(comb) for comb in combinations(nums, r)] + + +if __name__ == "__main__": + import doctest + + # Test permutations + result_permute = permute_backtrack([1, 2, 3]) + print("Permutations:", result_permute) + + # Test combinations + result_combinations = get_combinations([1, 2, 3], 2) + print("Combinations of length 2:", result_combinations) + + # Run doctests + doctest.testmod() From 606f962196c3395f4f060af805749e7d0c13c049 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 30 Oct 2024 06:04:00 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- data_structures/arrays/new_combination.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/data_structures/arrays/new_combination.py b/data_structures/arrays/new_combination.py index 4b347fa142b8..d083acf7594f 100644 --- a/data_structures/arrays/new_combination.py +++ b/data_structures/arrays/new_combination.py @@ -1,5 +1,6 @@ from itertools import combinations + def permute_recursive(nums: list[int]) -> list[list[int]]: """ Return all permutations. @@ -19,6 +20,7 @@ def permute_recursive(nums: list[int]) -> list[list[int]]: nums.append(n) return result + def permute_backtrack(nums: list[int]) -> list[list[int]]: """ Return all permutations of the given list. @@ -26,6 +28,7 @@ def permute_backtrack(nums: list[int]) -> list[list[int]]: >>> 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[:]) @@ -39,6 +42,7 @@ def backtrack(start: int) -> None: backtrack(0) return output + def get_combinations(nums: list[int], r: int) -> list[list[int]]: """ Return all combinations of the given list of length r.