From 6211b712c038cd0739ca4a42706ee10adcc2af3b Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 21:43:44 -0700 Subject: [PATCH 01/10] [ADD] tests for all_permutations --- backtracking/all_permutations.py | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index ff8a53e0dd0e..0d99020ef923 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -7,8 +7,41 @@ """ from __future__ import annotations +from itertools import permutations + + +def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: + """ + Default permutation output is list of tuples + >>> permutation_tuple_to_list([1,2]) + [[1, 2], [2, 1]] + >>> permutation_tuple_to_list(['a', 'b']) + [['a', 'b'], ['b', 'a']] + """ + return [list(output_tuple) for output_tuple in permutations(arr)] + def generate_all_permutations(sequence: list[int | str]) -> None: + """ + >>> generate_all_permutations([]) + [[]] + >>> generate_all_permutations([1]) + [[1]] + >>> generate_all_permutations([1, 2]) + [[1, 2], [2, 1]] + >>> generate_all_permutations([1, 2, 3]) + [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]] + >>> generate_all_permutations([-1, 2]) + [[-1, 2], [2, -1]] + >>> generate_all_permutations([1, -2]) + [[1, -2], [-2, 1]] + >>> generate_all_permutations(['a', 'b']) + [['a', 'b'], ['b', 'a']] + >>> from itertools import permutations + >>> test_arr = [num for num in range(0, 6)] + >>> all(generate_all_permutations(test_arr) == list(permutations[test_arr])) + True + """ create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) @@ -49,3 +82,8 @@ def create_state_space_tree( sequence_2: list[int | str] = ["A", "B", "C"] generate_all_permutations(sequence_2) + +if __name__ == "__main__": + from doctest import testmod + + testmod() \ No newline at end of file From c52ff51eccedacd614781e6766e86d509f0391fe Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 04:46:30 +0000 Subject: [PATCH 02/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/all_permutations.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 0d99020ef923..317a1ceca30f 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -86,4 +86,4 @@ def create_state_space_tree( if __name__ == "__main__": from doctest import testmod - testmod() \ No newline at end of file + testmod() From d3ee66c3ca4f16bf81b88d2cfb9dd6ff8601a2e2 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 22:12:12 -0700 Subject: [PATCH 03/10] test output for function --- backtracking/all_permutations.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 317a1ceca30f..1ce5cb3d8365 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -23,10 +23,6 @@ def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: def generate_all_permutations(sequence: list[int | str]) -> None: """ - >>> generate_all_permutations([]) - [[]] - >>> generate_all_permutations([1]) - [[1]] >>> generate_all_permutations([1, 2]) [[1, 2], [2, 1]] >>> generate_all_permutations([1, 2, 3]) From 4e87f2cfea35f95446e140bd6fa1ccc3290a2080 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 22:50:33 -0700 Subject: [PATCH 04/10] [FIX] generate_all_permutations had no output --- backtracking/all_permutations.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 1ce5cb3d8365..406450518b7b 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -23,6 +23,10 @@ def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: def generate_all_permutations(sequence: list[int | str]) -> None: """ + >>> generate_all_permutations([]) + [[]] + >>> generate_all_permutations([1]) + [[1]] >>> generate_all_permutations([1, 2]) [[1, 2], [2, 1]] >>> generate_all_permutations([1, 2, 3]) @@ -35,10 +39,12 @@ def generate_all_permutations(sequence: list[int | str]) -> None: [['a', 'b'], ['b', 'a']] >>> from itertools import permutations >>> test_arr = [num for num in range(0, 6)] - >>> all(generate_all_permutations(test_arr) == list(permutations[test_arr])) + >>> generate_all_permutations(test_arr) == permutation_tuple_to_list(test_arr) True """ - create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) + output: list[list[int]] = [] + create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))], output) + return output def create_state_space_tree( @@ -46,6 +52,7 @@ def create_state_space_tree( current_sequence: list[int | str], index: int, index_used: list[int], + output: list[list[int]], ) -> None: """ Creates a state space tree to iterate through each branch using DFS. @@ -54,16 +61,20 @@ def create_state_space_tree( """ if index == len(sequence): - print(current_sequence) + # Copying b/c popping current_sequence is part of this recursive algo. + # Can't have pointer to that arr + current_sequence_copy = [num for num in current_sequence] + output.append(current_sequence_copy) return for i in range(len(sequence)): if not index_used[i]: current_sequence.append(sequence[i]) index_used[i] = True - create_state_space_tree(sequence, current_sequence, index + 1, index_used) + create_state_space_tree(sequence, current_sequence, index + 1, index_used, output) current_sequence.pop() index_used[i] = False + return output """ From 1491f2f5dd1d0b13ffd35c8d4b8b44ac732910e9 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 05:51:47 +0000 Subject: [PATCH 05/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/all_permutations.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 406450518b7b..ee30c5b9acbc 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -61,9 +61,9 @@ def create_state_space_tree( """ if index == len(sequence): - # Copying b/c popping current_sequence is part of this recursive algo. + # Copying b/c popping current_sequence is part of this recursive algo. # Can't have pointer to that arr - current_sequence_copy = [num for num in current_sequence] + current_sequence_copy = [num for num in current_sequence] output.append(current_sequence_copy) return @@ -71,7 +71,9 @@ def create_state_space_tree( if not index_used[i]: current_sequence.append(sequence[i]) index_used[i] = True - create_state_space_tree(sequence, current_sequence, index + 1, index_used, output) + create_state_space_tree( + sequence, current_sequence, index + 1, index_used, output + ) current_sequence.pop() index_used[i] = False return output From 0fef0e70867584763ee9d2c3a5552aacbb43cf5d Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 23:06:06 -0700 Subject: [PATCH 06/10] linter fixes --- backtracking/all_permutations.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index ee30c5b9acbc..9a7155a2a952 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -63,7 +63,7 @@ def create_state_space_tree( if index == len(sequence): # Copying b/c popping current_sequence is part of this recursive algo. # Can't have pointer to that arr - current_sequence_copy = [num for num in current_sequence] + current_sequence_copy = list(current_sequence) output.append(current_sequence_copy) return @@ -71,9 +71,12 @@ def create_state_space_tree( if not index_used[i]: current_sequence.append(sequence[i]) index_used[i] = True - create_state_space_tree( - sequence, current_sequence, index + 1, index_used, output - ) + # function spaced like this b/c linter 88 char limit + create_state_space_tree(sequence, + current_sequence, + index + 1, + index_used, + output) current_sequence.pop() index_used[i] = False return output From c534e470c96b0f3c3ca08ff6ce7213bf75f7b19f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 06:06:50 +0000 Subject: [PATCH 07/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/all_permutations.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 9a7155a2a952..86bb24daaa02 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -72,11 +72,9 @@ def create_state_space_tree( current_sequence.append(sequence[i]) index_used[i] = True # function spaced like this b/c linter 88 char limit - create_state_space_tree(sequence, - current_sequence, - index + 1, - index_used, - output) + create_state_space_tree( + sequence, current_sequence, index + 1, index_used, output + ) current_sequence.pop() index_used[i] = False return output From 3ff12f5d1a79404dd5d7ec523a34467356f6a9e3 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 23:21:12 -0700 Subject: [PATCH 08/10] linter fixes 2 --- backtracking/all_permutations.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 86bb24daaa02..a80dd2f5c328 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -21,7 +21,7 @@ def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: return [list(output_tuple) for output_tuple in permutations(arr)] -def generate_all_permutations(sequence: list[int | str]) -> None: +def generate_all_permutations(sequence: list[int | str]) -> list[list[int]]: """ >>> generate_all_permutations([]) [[]] @@ -52,7 +52,7 @@ def create_state_space_tree( current_sequence: list[int | str], index: int, index_used: list[int], - output: list[list[int]], + output: list[list[int | str]], ) -> None: """ Creates a state space tree to iterate through each branch using DFS. @@ -72,12 +72,13 @@ def create_state_space_tree( current_sequence.append(sequence[i]) index_used[i] = True # function spaced like this b/c linter 88 char limit - create_state_space_tree( - sequence, current_sequence, index + 1, index_used, output - ) + create_state_space_tree(sequence, + current_sequence, + index + 1, + index_used, + output) current_sequence.pop() index_used[i] = False - return output """ From 07ca166aae326b88c66a915dd53494634d250461 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 18 Oct 2023 06:21:51 +0000 Subject: [PATCH 09/10] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/all_permutations.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index a80dd2f5c328..a2e064c91c5c 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -72,11 +72,9 @@ def create_state_space_tree( current_sequence.append(sequence[i]) index_used[i] = True # function spaced like this b/c linter 88 char limit - create_state_space_tree(sequence, - current_sequence, - index + 1, - index_used, - output) + create_state_space_tree( + sequence, current_sequence, index + 1, index_used, output + ) current_sequence.pop() index_used[i] = False From efae81b0cee9525a72192d7a135e7324e9620728 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Tue, 17 Oct 2023 23:32:46 -0700 Subject: [PATCH 10/10] linter fix 3 --- backtracking/all_permutations.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index a2e064c91c5c..b66edd767270 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -10,7 +10,7 @@ from itertools import permutations -def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: +def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int | str]]: """ Default permutation output is list of tuples >>> permutation_tuple_to_list([1,2]) @@ -21,7 +21,7 @@ def permutation_tuple_to_list(arr: list[int | str]) -> list[list[int]]: return [list(output_tuple) for output_tuple in permutations(arr)] -def generate_all_permutations(sequence: list[int | str]) -> list[list[int]]: +def generate_all_permutations(sequence: list[int | str]) -> list[list[int | str]]: """ >>> generate_all_permutations([]) [[]] @@ -42,7 +42,7 @@ def generate_all_permutations(sequence: list[int | str]) -> list[list[int]]: >>> generate_all_permutations(test_arr) == permutation_tuple_to_list(test_arr) True """ - output: list[list[int]] = [] + output: list[list[int | str]] = [] create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))], output) return output