diff --git a/backtracking/all_permutations.py b/backtracking/all_permutations.py index f376e6fa0945..565c263ea283 100644 --- a/backtracking/all_permutations.py +++ b/backtracking/all_permutations.py @@ -8,8 +8,20 @@ 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,7 +90,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())) +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 +generate_all_permutations(sequence) """ sequence: list[int | str] = [3, 1, 2, 4]