From e8d24952781b2d25d833dffe1b3890aa90bf38b3 Mon Sep 17 00:00:00 2001 From: orbisai0security Date: Tue, 28 Apr 2026 10:34:17 +0000 Subject: [PATCH 1/6] fix: V-002 security vulnerability Automated security fix generated by Orbis Security AI --- backtracking/all_permutations.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index f376e6fa0945..4a61b562a721 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -78,7 +78,11 @@ def create_state_space_tree( remove the comment to take an input from the user print("Enter the elements") -sequence = list(map(int, input().split())) +MAX_SEQUENCE_LENGTH = 8 +user_input = list(map(int, input().split())) +if len(user_input) > MAX_SEQUENCE_LENGTH: + raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") +sequence = user_input """ sequence: list[int | str] = [3, 1, 2, 4] From 61d15e379c26c3125db1702ded47dfc6fa2cccd5 Mon Sep 17 00:00:00 2001 From: OrbisAI Security Date: Sat, 2 May 2026 12:54:55 +0530 Subject: [PATCH 2/6] moving max_sequence_length to model, length check + doctest, cli block now checks length --- backtracking/all_permutations.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 4a61b562a721..821898989543 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -8,8 +8,18 @@ from __future__ import annotations +MAX_SEQUENCE_LENGTH = 8 + def generate_all_permutations(sequence: list[int | str]) -> None: + """ + >>> generate_all_permutations([1] * 9) + Traceback (most recent call last): + ... + ValueError: Input sequence too long (max 8 elements). + """ + if len(sequence) > MAX_SEQUENCE_LENGTH: + raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) @@ -78,11 +88,11 @@ def create_state_space_tree( remove the comment to take an input from the user print("Enter the elements") -MAX_SEQUENCE_LENGTH = 8 -user_input = list(map(int, input().split())) -if len(user_input) > MAX_SEQUENCE_LENGTH: +raw = input().split() +if len(raw) > MAX_SEQUENCE_LENGTH: raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") -sequence = user_input +sequence: list[int | str] = raw +generate_all_permutations(sequence) """ sequence: list[int | str] = [3, 1, 2, 4] From 1640999e8102717b39a2d14caab2bb39d55b9c1c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 2 May 2026 07:25:22 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- backtracking/all_permutations.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 821898989543..565c263ea283 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -19,7 +19,9 @@ def generate_all_permutations(sequence: list[int | str]) -> None: ValueError: Input sequence too long (max 8 elements). """ if len(sequence) > MAX_SEQUENCE_LENGTH: - raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") + raise ValueError( + f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements)." + ) create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))]) From 4154181b189ec28ad588b94a7bbd2b17f385508b Mon Sep 17 00:00:00 2001 From: orbisai0security Date: Mon, 4 May 2026 06:32:20 +0000 Subject: [PATCH 4/6] Apply code changes: @orbisai0security can you address code review comm... --- backtracking/all_permutations.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index 565c263ea283..dbdc82bb7d24 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -93,7 +93,13 @@ def create_state_space_tree( raw = input().split() if len(raw) > MAX_SEQUENCE_LENGTH: raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") -sequence: list[int | str] = raw +# Try to convert each token to int; keep as str if conversion is not possible +sequence: list[int | str] = [] +for token in raw: + try: + sequence.append(int(token)) + except ValueError: + sequence.append(token) generate_all_permutations(sequence) """ From 3cfeb576e9ebe7cef400f26476f2279e5337e021 Mon Sep 17 00:00:00 2001 From: orbisai0security Date: Wed, 13 May 2026 08:25:33 +0000 Subject: [PATCH 5/6] Address review feedback (3 comments) --- backtracking/all_permutations.py | 69 +++++++++++++++----------------- 1 file changed, 32 insertions(+), 37 deletions(-) diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index dbdc82bb7d24..07223393b413 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -13,6 +13,18 @@ def generate_all_permutations(sequence: list[int | str]) -> None: """ + Generate and print all permutations of the given sequence. + + Raises ValueError if the sequence exceeds MAX_SEQUENCE_LENGTH elements + to prevent excessive CPU/memory usage (permutation count is O(n!)). + + >>> 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] * 9) Traceback (most recent call last): ... @@ -36,54 +48,39 @@ def create_state_space_tree( We know that each state has exactly len(sequence) - index children. It terminates when it reaches the end of the given sequence. - :param sequence: The input sequence for which permutations are generated. - :param current_sequence: The current permutation being built. - :param index: The current index in the sequence. - :param index_used: list to track which elements are used in permutation. - - Example 1: - >>> sequence = [1, 2, 3] - >>> current_sequence = [] - >>> index_used = [False, False, False] - >>> create_state_space_tree(sequence, current_sequence, 0, index_used) + >>> create_state_space_tree([1, 2, 3], [], 0, [0, 0, 0]) [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1] [3, 1, 2] [3, 2, 1] - - Example 2: - >>> sequence = ["A", "B", "C"] - >>> current_sequence = [] - >>> index_used = [False, False, False] - >>> create_state_space_tree(sequence, current_sequence, 0, index_used) - ['A', 'B', 'C'] - ['A', 'C', 'B'] - ['B', 'A', 'C'] - ['B', 'C', 'A'] - ['C', 'A', 'B'] - ['C', 'B', 'A'] - - Example 3: - >>> sequence = [1] - >>> current_sequence = [] - >>> index_used = [False] - >>> create_state_space_tree(sequence, current_sequence, 0, index_used) - [1] + >>> create_state_space_tree(["a", "b", "c"], [], 0, [0, 0, 0]) + ['a', 'b', 'c'] + ['a', 'c', 'b'] + ['b', 'a', 'c'] + ['b', 'c', 'a'] + ['c', 'a', 'b'] + ['c', 'b', 'a'] + >>> create_state_space_tree([2, 2, 2], [], 0, [0, 0, 0]) + [2, 2, 2] + [2, 2, 2] + [2, 2, 2] + [2, 2, 2] + [2, 2, 2] + [2, 2, 2] """ - if index == len(sequence): print(current_sequence) return for i in range(len(sequence)): - if not index_used[i]: + if index_used[i] == 0: current_sequence.append(sequence[i]) - index_used[i] = True + index_used[i] = 1 create_state_space_tree(sequence, current_sequence, index + 1, index_used) current_sequence.pop() - index_used[i] = False + index_used[i] = 0 """ @@ -93,7 +90,8 @@ def create_state_space_tree( raw = input().split() if len(raw) > MAX_SEQUENCE_LENGTH: raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).") -# Try to convert each token to int; keep as str if conversion is not possible +# Try to convert each token to int; keep as str if conversion is not possible. +# This supports both integer and string elements, matching the function's type hints. sequence: list[int | str] = [] for token in raw: try: @@ -105,6 +103,3 @@ def create_state_space_tree( sequence: list[int | str] = [3, 1, 2, 4] generate_all_permutations(sequence) - -sequence_2: list[int | str] = ["A", "B", "C"] -generate_all_permutations(sequence_2) From 0adba8fcd40f7221e139e518d454b292680e5a73 Mon Sep 17 00:00:00 2001 From: orbisai0security Date: Wed, 13 May 2026 09:54:02 +0000 Subject: [PATCH 6/6] Address review feedback (3 comments) --- 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 07223393b413..4aa1015c75cb 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -44,7 +44,7 @@ def create_state_space_tree( index_used: list[int], ) -> None: """ - Creates a state space tree to iterate through each branch using DFS. + Generate a state space tree for the given sequence. We know that each state has exactly len(sequence) - index children. It terminates when it reaches the end of the given sequence.