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
6 changes: 5 additions & 1 deletion backtracking/all_permutations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
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 moving MAX_SEQUENCE_LENGTH to the module level instead of defining it inside this input block. That makes the limit easier to find, reuse, and update later.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@orbisai0security can you address code review comments?

user_input = list(map(int, input().split()))
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This still only supports integer input from the user, while the function type hints allow both int and str. Consider documenting that interactive input only accepts integers, or update the parsing logic to support strings too.

if len(user_input) > MAX_SEQUENCE_LENGTH:
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The length check happens after converting all input values into a list. For very large input, this still loads everything into memory first. Consider checking the split input length before mapping to integers.

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.

sequence = user_input
"""

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