prompt_list helper

This commit is contained in:
sam.hadow committed 2026-05-04 11:00:29 +02:00
1 parent 1187b9ea25
commit 381e00c584
1 file changed
+33 -25
+33 -25
View File
@@ -30,6 +30,32 @@ def prompt_choice(message: str, choices: set[int]) -> int:
return value return value
print(f"Value must be one of: {choices_str}") print(f"Value must be one of: {choices_str}")
def prompt_list(prompt: str, item_name: str, min_value: int, max_value: int):
while True:
raw = input(prompt).strip()
if not raw:
print(f"Please enter at least one {item_name}.")
continue
try:
values = [int(x) for x in raw.split()]
except ValueError:
print("Please enter only numbers separated by spaces.")
continue
if any(v < min_value or v > max_value for v in values):
print(f"Each {item_name} must be between {min_value} and {max_value}.")
continue
seen = set()
values = [v for v in values if not (v in seen or seen.add(v))]
if not values:
print(f"Please enter at least one valid {item_name}.")
continue
return values
def run_classic_cli(): def run_classic_cli():
print("\nR registers are indexed 0–7; bits within each register are 0–7.") print("\nR registers are indexed 0–7; bits within each register are 0–7.")
@@ -74,37 +100,19 @@ def run_exhaustive_cli():
print("\n" + "=" * 50) print("\n" + "=" * 50)
print("Done.") print("Done.")
def run_variable_xor_cli(): def run_variable_xor_cli():
print("\nR registers are indexed 0–7; bits within each register are 0–7.") print("\nR registers are indexed 0–7; bits within each register are 0–7.")
print("Enter the bit positions to XOR, e.g. `0 1`.") print("Enter the bit positions to XOR, e.g. `0 1`.")
steps = prompt_int("How many steps? (1–100): ", 1, 100) steps = prompt_int("How many steps? (1–100): ", 1, 100)
target_reg = prompt_int("Target register (0–7): ", 0, 7) target_reg = prompt_int("Target register (0–7): ", 0, 7)
bits_to_xor = prompt_list(
while True: "Bits to XOR (0–7, separated by spaces): ",
raw = input("Bits to XOR (0–7, separated by spaces): ").strip() item_name="bit",
if not raw: min_value=0,
print("Please enter at least one bit index.") max_value=7,
continue )
try:
bits_to_xor = [int(x) for x in raw.split()]
except ValueError:
print("Please enter only numbers separated by spaces.")
continue
if any(bit < 0 or bit > 7 for bit in bits_to_xor):
print("Each bit must be between 0 and 7.")
continue
seen = set()
bits_to_xor = [b for b in bits_to_xor if not (b in seen or seen.add(b))]
if not bits_to_xor:
print("Please enter at least one valid bit.")
continue
break
print("-" * 50) print("-" * 50)
run_variable_xor(steps, target_reg, bits_to_xor) run_variable_xor(steps, target_reg, bits_to_xor)