Skip to content

Commit 61d15e3

Browse files
moving max_sequence_length to model, length check + doctest, cli block now checks length
1 parent e8d2495 commit 61d15e3

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

backtracking/all_permutations.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,18 @@
88

99
from __future__ import annotations
1010

11+
MAX_SEQUENCE_LENGTH = 8
12+
1113

1214
def generate_all_permutations(sequence: list[int | str]) -> None:
15+
"""
16+
>>> generate_all_permutations([1] * 9)
17+
Traceback (most recent call last):
18+
...
19+
ValueError: Input sequence too long (max 8 elements).
20+
"""
21+
if len(sequence) > MAX_SEQUENCE_LENGTH:
22+
raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).")
1323
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])
1424

1525

@@ -78,11 +88,11 @@ def create_state_space_tree(
7888
remove the comment to take an input from the user
7989
8090
print("Enter the elements")
81-
MAX_SEQUENCE_LENGTH = 8
82-
user_input = list(map(int, input().split()))
83-
if len(user_input) > MAX_SEQUENCE_LENGTH:
91+
raw = input().split()
92+
if len(raw) > MAX_SEQUENCE_LENGTH:
8493
raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).")
85-
sequence = user_input
94+
sequence: list[int | str] = raw
95+
generate_all_permutations(sequence)
8696
"""
8797

8898
sequence: list[int | str] = [3, 1, 2, 4]

0 commit comments

Comments
 (0)