Skip to content

Commit 3cfeb57

Browse files
Address review feedback (3 comments)
1 parent 4154181 commit 3cfeb57

1 file changed

Lines changed: 32 additions & 37 deletions

File tree

backtracking/all_permutations.py

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
def generate_all_permutations(sequence: list[int | str]) -> None:
1515
"""
16+
Generate and print all permutations of the given sequence.
17+
18+
Raises ValueError if the sequence exceeds MAX_SEQUENCE_LENGTH elements
19+
to prevent excessive CPU/memory usage (permutation count is O(n!)).
20+
21+
>>> generate_all_permutations([1, 2, 3])
22+
[1, 2, 3]
23+
[1, 3, 2]
24+
[2, 1, 3]
25+
[2, 3, 1]
26+
[3, 1, 2]
27+
[3, 2, 1]
1628
>>> generate_all_permutations([1] * 9)
1729
Traceback (most recent call last):
1830
...
@@ -36,54 +48,39 @@ def create_state_space_tree(
3648
We know that each state has exactly len(sequence) - index children.
3749
It terminates when it reaches the end of the given sequence.
3850
39-
:param sequence: The input sequence for which permutations are generated.
40-
:param current_sequence: The current permutation being built.
41-
:param index: The current index in the sequence.
42-
:param index_used: list to track which elements are used in permutation.
43-
44-
Example 1:
45-
>>> sequence = [1, 2, 3]
46-
>>> current_sequence = []
47-
>>> index_used = [False, False, False]
48-
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
51+
>>> create_state_space_tree([1, 2, 3], [], 0, [0, 0, 0])
4952
[1, 2, 3]
5053
[1, 3, 2]
5154
[2, 1, 3]
5255
[2, 3, 1]
5356
[3, 1, 2]
5457
[3, 2, 1]
55-
56-
Example 2:
57-
>>> sequence = ["A", "B", "C"]
58-
>>> current_sequence = []
59-
>>> index_used = [False, False, False]
60-
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
61-
['A', 'B', 'C']
62-
['A', 'C', 'B']
63-
['B', 'A', 'C']
64-
['B', 'C', 'A']
65-
['C', 'A', 'B']
66-
['C', 'B', 'A']
67-
68-
Example 3:
69-
>>> sequence = [1]
70-
>>> current_sequence = []
71-
>>> index_used = [False]
72-
>>> create_state_space_tree(sequence, current_sequence, 0, index_used)
73-
[1]
58+
>>> create_state_space_tree(["a", "b", "c"], [], 0, [0, 0, 0])
59+
['a', 'b', 'c']
60+
['a', 'c', 'b']
61+
['b', 'a', 'c']
62+
['b', 'c', 'a']
63+
['c', 'a', 'b']
64+
['c', 'b', 'a']
65+
>>> create_state_space_tree([2, 2, 2], [], 0, [0, 0, 0])
66+
[2, 2, 2]
67+
[2, 2, 2]
68+
[2, 2, 2]
69+
[2, 2, 2]
70+
[2, 2, 2]
71+
[2, 2, 2]
7472
"""
75-
7673
if index == len(sequence):
7774
print(current_sequence)
7875
return
7976

8077
for i in range(len(sequence)):
81-
if not index_used[i]:
78+
if index_used[i] == 0:
8279
current_sequence.append(sequence[i])
83-
index_used[i] = True
80+
index_used[i] = 1
8481
create_state_space_tree(sequence, current_sequence, index + 1, index_used)
8582
current_sequence.pop()
86-
index_used[i] = False
83+
index_used[i] = 0
8784

8885

8986
"""
@@ -93,7 +90,8 @@ def create_state_space_tree(
9390
raw = input().split()
9491
if len(raw) > MAX_SEQUENCE_LENGTH:
9592
raise ValueError(f"Input sequence too long (max {MAX_SEQUENCE_LENGTH} elements).")
96-
# Try to convert each token to int; keep as str if conversion is not possible
93+
# Try to convert each token to int; keep as str if conversion is not possible.
94+
# This supports both integer and string elements, matching the function's type hints.
9795
sequence: list[int | str] = []
9896
for token in raw:
9997
try:
@@ -105,6 +103,3 @@ def create_state_space_tree(
105103

106104
sequence: list[int | str] = [3, 1, 2, 4]
107105
generate_all_permutations(sequence)
108-
109-
sequence_2: list[int | str] = ["A", "B", "C"]
110-
generate_all_permutations(sequence_2)

0 commit comments

Comments
 (0)