X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=rpl.py;h=b51edefcce07b40e01a984f5e263dc944bc01342;hb=b59fca62aa31de18a3e0cd0bb54e395d4b1254ae;hp=8d31efe439c40b91cb80b7a8167f8f41ae31b9df;hpb=cd5e4647e105a10012d687169d49bec0343e274f;p=picoclvr.git diff --git a/rpl.py b/rpl.py index 8d31efe..b51edef 100755 --- a/rpl.py +++ b/rpl.py @@ -105,15 +105,21 @@ def decompose(seq): k = 0 while seq[k] == "": o = next_marker(seq, [""], start=k + 1) + if o is None: + raise ValueError("Missing output markers (should be correct in the prompt)") e = next_marker(seq, ["", ""], start=o) - if o is None or e is None: - raise ValueError("Invalid input/output") + if e is None: + raise ValueError( + "Missing input/output markers (should be correct in the prompt)" + ) try: io.append( ([int(x) for x in seq[k + 1 : o]], [int(x) for x in seq[o + 1 : e]]) ) except ValueError: - raise ValueError("Invalid input/output") + raise ValueError( + "Invalid input/output value (should be correct in the prompt)" + ) k = e @@ -123,9 +129,18 @@ def decompose(seq): prog = [] else: prog = seq[k + 1 : e] + else: + raise ValueError("Missing (it should be in the prompt)") + return prog, io +def stack_distance(target_stack, result_stack): + return abs(len(result_stack) - len(target_stack)) + sum( + [0 if x == y else 1 for x, y in zip(result_stack, target_stack)] + ) + + def compute_nb_errors(seq): prog, io = decompose(seq) @@ -145,9 +160,7 @@ def compute_nb_errors(seq): for start_stack, target_stack in io: result_stack = rpl_exec(prog, start_stack) nb_total += len(target_stack) - e = abs(len(result_stack) - len(target_stack)) + sum( - [0 if x == y else 1 for x, y in zip(result_stack, target_stack)] - ) + e = stack_distance(target_stack, result_stack) nb_errors += e stacks.append((start_stack, target_stack, result_stack, e == 0))