X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?a=blobdiff_plain;f=rpl.py;h=b848afa756a6188a60d29a446bdd51d1e455b74a;hb=cd3329fc206bacfd90a8e2cbe364244359568733;hp=f826fc4fd5fbe33eed919e8d0f5d80220047b73b;hpb=d7eeacf1eab237bbbe67d3e44b90b57fd1445667;p=picoclvr.git diff --git a/rpl.py b/rpl.py index f826fc4..b848afa 100755 --- a/rpl.py +++ b/rpl.py @@ -75,9 +75,9 @@ def generate( result_stack = rpl_exec(prog, stack) if len(result_stack) == 0: no_empty_stack = False - result = result + [""] + stack + [""] + result_stack + result = result + [""] + stack + [""] + result_stack - result = result + [""] + prog + result = result + [""] + prog result = result + [""] if no_empty_stack and ( @@ -103,10 +103,12 @@ def next_marker(seq, tokens, start=0): def decompose(seq): io = [] k = 0 - while seq[k] == "": - o = next_marker(seq, [""], start=k + 1) - e = next_marker(seq, ["", ""], start=o) - if o is None or e is None: + 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 e is None: raise ValueError( "Missing input/output markers (should be correct in the prompt)" ) @@ -121,18 +123,24 @@ def decompose(seq): k = e - if seq[k] == "": + if seq[k] == "": e = next_marker(seq, [""], start=k) if e is None: prog = [] else: prog = seq[k + 1 : e] else: - raise ValueError("Missing (it should be in the prompt)") + 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) @@ -152,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))