prompt_list helper
This commit is contained in:
+33
-25
@@ -30,6 +30,32 @@ def prompt_choice(message: str, choices: set[int]) -> int:
|
||||
return value
|
||||
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():
|
||||
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("Done.")
|
||||
|
||||
|
||||
def run_variable_xor_cli():
|
||||
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`.")
|
||||
|
||||
steps = prompt_int("How many steps? (1–100): ", 1, 100)
|
||||
target_reg = prompt_int("Target register (0–7): ", 0, 7)
|
||||
|
||||
while True:
|
||||
raw = input("Bits to XOR (0–7, separated by spaces): ").strip()
|
||||
if not raw:
|
||||
print("Please enter at least one bit index.")
|
||||
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
|
||||
bits_to_xor = prompt_list(
|
||||
"Bits to XOR (0–7, separated by spaces): ",
|
||||
item_name="bit",
|
||||
min_value=0,
|
||||
max_value=7,
|
||||
)
|
||||
|
||||
print("-" * 50)
|
||||
run_variable_xor(steps, target_reg, bits_to_xor)
|
||||
|
||||
Reference in New Issue
Block a user