Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion backtracking/all_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)."

Check failure on line 23 in backtracking/all_permutations.py

View workflow job for this annotation

GitHub Actions / ruff

ruff (EM102)

backtracking/all_permutations.py:23:13: EM102 Exception must not use an f-string literal, assign to variable first help: Assign to variable; remove f-string literal
)
create_state_space_tree(sequence, [], 0, [0 for i in range(len(sequence))])


Expand Down Expand Up @@ -78,7 +90,11 @@
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).")
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a test or doctest for the new length limit so this behavior is verified automatically and does not regress later.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

sequence: list[int | str] = raw
generate_all_permutations(sequence)
"""

sequence: list[int | str] = [3, 1, 2, 4]
Expand Down
Loading