broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    classes, levels, para = x
    n = len(classes)
    match = {}
    stack = []
    for i, c in enumerate(classes):
        if c in ('LRI', 'RLI', 'FSI'):
            stack.append(i)
        elif c == 'PDI' and stack:
            match[stack.pop()] = i
    runs = []
    for i in range(n):
        if runs and levels[i] == levels[runs[-1][-1]]:
            runs[-1].append(i)
        else:
            runs.append([i])
    start_of = {r[0]: r for r in runs}
    seqs = []
    used = set()
    for r in runs:
        if r[0] in used:
            continue
        seq = list(r)
        used.add(r[0])
        while True:
            tail = seq[-1]
            if classes[tail] not in ('LRI', 'RLI', 'FSI') or tail not in match:
                break
            nxt = start_of.get(match[tail])
            if nxt is None:
                break
            seq.extend(nxt)
            used.add(nxt[0])
        seqs.append(seq)
    out = []
    for seq in seqs:
        first, last = seq[0], seq[-1]
        lv = levels[first]
        prev = levels[first - 1] if first > 0 else para
        sos = 'R' if max(lv, prev) % 2 else 'L'
        if classes[last] == 'PDI':
            nxt_lv = para
        else:
            nxt_lv = levels[last + 1] if last + 1 < n else para
        eos = 'R' if max(levels[last], nxt_lv) % 2 else 'L'
        out.append([seq, sos, eos])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: eos after isolate initiator', [['EN', 'EN', 'LRI', 'ON', 'ON', 'ON', 'L', 'RLI', 'EN'], [2, 2, 1, 2, 2, 2, 2, 2, 3], 1], [[[0, 1], 'L', 'L'], [[2], 'L', 'R'], [[3, 4, 5, 6, 7], 'L', 'L'], [[8], 'R', 'R']]), ('regression: eos after isolate initiator', [['R', 'L', 'PDI', 'FSI', 'ON'], [1, 2, 1, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4], 'L', 'L']]), ('regression: eos after isolate initiator', [['PDI', 'ON', 'PDI', 'RLI', 'ON', 'EN', 'ON', 'FSI', 'L', 'R', 'EN', 'ON'], [1, 1, 1, 1, 3, 3, 3, 3, 4, 4, 4, 4], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'R', 'R'], [[8, 9, 10, 11], 'L', 'L']]), ('regression: eos after isolate initiator', [['EN', 'LRI', 'R', 'LRI', 'R'], [2, 1, 2, 2, 5], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3], 'L', 'L'], [[4], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['LRI', 'R', 'R', 'ON', 'ON', 'R', 'PDI', 'PDI', 'EN', 'EN', 'L', 'FSI', 'EN'], [0, 3, 3, 2, 2, 3, 0, 0, 0, 0, 0, 0, 2], 0], [[[0, 6, 7, 8, 9, 10, 11], 'L', 'L'], [[1, 2], 'R', 'R'], [[3, 4], 'R', 'R'], [[5], 'R', 'R'], [[12], 'L', 'L']]), ('control layout', [['PDI', 'R', 'LRI', 'EN', 'ON', 'R', 'R', 'EN'], [0, 0, 0, 2, 2, 3, 2, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7], 'R', 'L']])], [('regression: eos after isolate initiator', [['EN', 'L', 'R', 'L', 'PDI', 'R', 'PDI', 'R', 'EN', 'ON'], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4], 'R', 'R'], [[5], 'R', 'R'], [[6, 7, 8, 9], 'R', 'L']]), ('regression: eos after isolate initiator', [['EN', 'FSI', 'FSI', 'L', 'PDI', 'R', 'R', 'PDI', 'EN', 'RLI', 'L', 'PDI', 'PDI'], [1, 1, 2, 4, 2, 2, 3, 1, 2, 1, 3, 1, 1], 1], [[[0, 1, 7], 'R', 'L'], [[2, 4, 5], 'L', 'R'], [[3], 'L', 'L'], [[6], 'R', 'R'], [[8], 'L', 'L'], [[9, 11, 12], 'L', 'R'], [[10], 'R', 'R']]), ('regression: eos after isolate initiator', [['EN', 'PDI', 'RLI', 'L', 'LRI', 'LRI', 'L', 'R', 'L'], [0, 0, 0, 1, 1, 2, 4, 4, 4], 0], [[[0, 1, 2], 'L', 'L'], [[3, 4], 'R', 'R'], [[5], 'L', 'L'], [[6, 7, 8], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'ON', 'RLI'], [1, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['PDI', 'L', 'PDI', 'L', 'LRI', 'ON', 'FSI', 'EN', 'R', 'PDI', 'L', 'PDI', 'EN'], [0, 0, 0, 0, 0, 2, 2, 4, 5, 2, 2, 0, 0], 0], [[[0, 1, 2, 3, 4, 11, 12], 'L', 'L'], [[5, 6, 9, 10], 'L', 'L'], [[7], 'L', 'R'], [[8], 'R', 'R']]), ('control layout', [['L', 'ON', 'EN', 'ON', 'EN', 'EN', 'PDI', 'ON', 'RLI'], [0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'L', 'L']])], [('regression: eos after isolate initiator', [['R', 'L', 'PDI', 'FSI', 'ON'], [1, 2, 1, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'RLI', 'EN', 'EN', 'PDI'], [1, 2, 2, 2, 2, 3, 4, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4, 7], 'L', 'L'], [[5], 'R', 'L'], [[6], 'L', 'L']]), ('regression: eos after isolate initiator', [['L', 'ON', 'ON', 'PDI', 'FSI', 'ON', 'R', 'ON', 'R'], [2, 1, 1, 1, 1, 2, 3, 2, 3], 1], [[[0], 'L', 'L'], [[1, 2, 3, 4], 'L', 'R'], [[5], 'L', 'R'], [[6], 'R', 'R'], [[7], 'R', 'R'], [[8], 'R', 'R']]), ('regression: eos after isolate initiator', [['ON', 'LRI', 'RLI'], [1, 1, 2], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['FSI', 'EN', 'ON', 'L', 'RLI', 'ON', 'PDI'], [0, 2, 2, 2, 2, 3, 2], 0], [[[0], 'L', 'L'], [[1, 2, 3, 4, 6], 'L', 'L'], [[5], 'R', 'R']]), ('control layout', [['LRI', 'PDI', 'ON', 'L', 'EN', 'L', 'R', 'RLI'], [1, 1, 1, 2, 1, 2, 1, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7], 'L', 'R']])], [('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'R'], [1, 2, 2, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'L', 'L']]), ('regression: eos after isolate initiator', [['L', 'LRI', 'LRI', 'LRI', 'EN', 'PDI', 'R', 'FSI', 'EN', 'R', 'ON'], [2, 1, 2, 4, 6, 4, 4, 4, 6, 7, 6], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2], 'L', 'L'], [[3, 5, 6, 7], 'L', 'L'], [[4], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L']]), ('regression: eos after isolate initiator', [['R', 'R', 'ON', 'LRI', 'LRI', 'RLI', 'ON', 'L', 'R'], [1, 1, 1, 1, 2, 4, 5, 5, 5], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7, 8], 'R', 'R']]), ('regression: eos after isolate initiator', [['PDI', 'RLI', 'ON'], [0, 0, 1], 0], [[[0, 1], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'EN', 'ON', 'PDI', 'LRI', 'PDI', 'FSI', 'L', 'R', 'RLI'], [0, 0, 0, 0, 0, 0, 0, 2, 3, 2], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L'], [[7], 'L', 'R'], [[8], 'R', 'R'], [[9], 'R', 'L']]), ('control layout', [['EN', 'R', 'ON', 'EN'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']])], [('regression: eos after isolate initiator', [['LRI', 'PDI', 'EN', 'EN', 'L', 'EN', 'PDI', 'FSI', 'ON', 'R', 'R', 'R', 'FSI'], [1, 1, 2, 1, 2, 1, 1, 1, 2, 3, 3, 3, 2], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R'], [[8], 'L', 'R'], [[9, 10, 11], 'R', 'R'], [[12], 'R', 'L']]), ('regression: eos after isolate initiator', [['EN', 'EN', 'PDI', 'R', 'PDI'], [0, 0, 0, 1, 0], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'L']]), ('regression: eos after isolate initiator', [['ON', 'FSI', 'FSI', 'FSI', 'R', 'PDI', 'FSI', 'EN', 'L', 'ON', 'ON', 'EN', 'L', 'L'], [1, 1, 2, 4, 6, 4, 4, 6, 6, 6, 6, 6, 6, 6], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L'], [[3, 5, 6], 'L', 'L'], [[4], 'L', 'L'], [[7, 8, 9, 10, 11, 12, 13], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'R'], [1, 2, 2, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'L', 'L']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('control layout', [['R', 'LRI', 'LRI', 'R', 'PDI', 'ON', 'PDI'], [1, 0, 2, 5, 2, 2, 0], 0], [[[0], 'R', 'R'], [[1, 6], 'R', 'L'], [[2, 4, 5], 'L', 'L'], [[3], 'R', 'R']]), ('control layout', [['PDI', 'R', 'FSI'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    classes, levels, para = x
    n = len(classes)
    match = {}
    stack = []
    for i, c in enumerate(classes):
        if c in ('LRI', 'RLI', 'FSI'):
            stack.append(i)
        elif c == 'PDI' and stack:
            match[stack.pop()] = i
    runs = []
    for i in range(n):
        if runs and levels[i] == levels[runs[-1][-1]]:
            runs[-1].append(i)
        else:
            runs.append([i])
    start_of = {r[0]: r for r in runs}
    seqs = []
    used = set()
    for r in runs:
        if r[0] in used:
            continue
        seq = list(r)
        used.add(r[0])
        while True:
            tail = seq[-1]
            if classes[tail] not in ('LRI', 'RLI', 'FSI') or tail not in match:
                break
            nxt = start_of.get(match[tail])
            if nxt is None:
                break
            seq.extend(nxt)
            used.add(nxt[0])
        seqs.append(seq)
    out = []
    for seq in seqs:
        first, last = seq[0], seq[-1]
        lv = levels[first]
        prev = levels[first - 1] if first > 0 else para
        sos = 'R' if max(lv, prev) % 2 else 'L'
        if classes[last] in ('LRI', 'RLI', 'FSI') and last in match:
            nxt_lv = para
        else:
            nxt_lv = levels[last + 1] if last + 1 < n else para
        eos = 'R' if max(levels[last], nxt_lv) % 2 else 'L'
        out.append([seq, sos, eos])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: eos after isolate initiator', [['EN', 'EN', 'LRI', 'ON', 'ON', 'ON', 'L', 'RLI', 'EN'], [2, 2, 1, 2, 2, 2, 2, 2, 3], 1], [[[0, 1], 'L', 'L'], [[2], 'L', 'R'], [[3, 4, 5, 6, 7], 'L', 'L'], [[8], 'R', 'R']]), ('regression: eos after isolate initiator', [['R', 'L', 'PDI', 'FSI', 'ON'], [1, 2, 1, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4], 'L', 'L']]), ('regression: eos after isolate initiator', [['PDI', 'ON', 'PDI', 'RLI', 'ON', 'EN', 'ON', 'FSI', 'L', 'R', 'EN', 'ON'], [1, 1, 1, 1, 3, 3, 3, 3, 4, 4, 4, 4], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'R', 'R'], [[8, 9, 10, 11], 'L', 'L']]), ('regression: eos after isolate initiator', [['EN', 'LRI', 'R', 'LRI', 'R'], [2, 1, 2, 2, 5], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3], 'L', 'L'], [[4], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['LRI', 'R', 'R', 'ON', 'ON', 'R', 'PDI', 'PDI', 'EN', 'EN', 'L', 'FSI', 'EN'], [0, 3, 3, 2, 2, 3, 0, 0, 0, 0, 0, 0, 2], 0], [[[0, 6, 7, 8, 9, 10, 11], 'L', 'L'], [[1, 2], 'R', 'R'], [[3, 4], 'R', 'R'], [[5], 'R', 'R'], [[12], 'L', 'L']]), ('control layout', [['PDI', 'R', 'LRI', 'EN', 'ON', 'R', 'R', 'EN'], [0, 0, 0, 2, 2, 3, 2, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7], 'R', 'L']])], [('regression: eos after isolate initiator', [['EN', 'L', 'R', 'L', 'PDI', 'R', 'PDI', 'R', 'EN', 'ON'], [0, 0, 1, 0, 0, 1, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4], 'R', 'R'], [[5], 'R', 'R'], [[6, 7, 8, 9], 'R', 'L']]), ('regression: eos after isolate initiator', [['EN', 'FSI', 'FSI', 'L', 'PDI', 'R', 'R', 'PDI', 'EN', 'RLI', 'L', 'PDI', 'PDI'], [1, 1, 2, 4, 2, 2, 3, 1, 2, 1, 3, 1, 1], 1], [[[0, 1, 7], 'R', 'L'], [[2, 4, 5], 'L', 'R'], [[3], 'L', 'L'], [[6], 'R', 'R'], [[8], 'L', 'L'], [[9, 11, 12], 'L', 'R'], [[10], 'R', 'R']]), ('regression: eos after isolate initiator', [['EN', 'PDI', 'RLI', 'L', 'LRI', 'LRI', 'L', 'R', 'L'], [0, 0, 0, 1, 1, 2, 4, 4, 4], 0], [[[0, 1, 2], 'L', 'L'], [[3, 4], 'R', 'R'], [[5], 'L', 'L'], [[6, 7, 8], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'ON', 'RLI'], [1, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['PDI', 'L', 'PDI', 'L', 'LRI', 'ON', 'FSI', 'EN', 'R', 'PDI', 'L', 'PDI', 'EN'], [0, 0, 0, 0, 0, 2, 2, 4, 5, 2, 2, 0, 0], 0], [[[0, 1, 2, 3, 4, 11, 12], 'L', 'L'], [[5, 6, 9, 10], 'L', 'L'], [[7], 'L', 'R'], [[8], 'R', 'R']]), ('control layout', [['L', 'ON', 'EN', 'ON', 'EN', 'EN', 'PDI', 'ON', 'RLI'], [0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'L', 'L']])], [('regression: eos after isolate initiator', [['R', 'L', 'PDI', 'FSI', 'ON'], [1, 2, 1, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'RLI', 'EN', 'EN', 'PDI'], [1, 2, 2, 2, 2, 3, 4, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4, 7], 'L', 'L'], [[5], 'R', 'L'], [[6], 'L', 'L']]), ('regression: eos after isolate initiator', [['L', 'ON', 'ON', 'PDI', 'FSI', 'ON', 'R', 'ON', 'R'], [2, 1, 1, 1, 1, 2, 3, 2, 3], 1], [[[0], 'L', 'L'], [[1, 2, 3, 4], 'L', 'R'], [[5], 'L', 'R'], [[6], 'R', 'R'], [[7], 'R', 'R'], [[8], 'R', 'R']]), ('regression: eos after isolate initiator', [['ON', 'LRI', 'RLI'], [1, 1, 2], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['FSI', 'EN', 'ON', 'L', 'RLI', 'ON', 'PDI'], [0, 2, 2, 2, 2, 3, 2], 0], [[[0], 'L', 'L'], [[1, 2, 3, 4, 6], 'L', 'L'], [[5], 'R', 'R']]), ('control layout', [['LRI', 'PDI', 'ON', 'L', 'EN', 'L', 'R', 'RLI'], [1, 1, 1, 2, 1, 2, 1, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7], 'L', 'R']])], [('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'R'], [1, 2, 2, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'L', 'L']]), ('regression: eos after isolate initiator', [['L', 'LRI', 'LRI', 'LRI', 'EN', 'PDI', 'R', 'FSI', 'EN', 'R', 'ON'], [2, 1, 2, 4, 6, 4, 4, 4, 6, 7, 6], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2], 'L', 'L'], [[3, 5, 6, 7], 'L', 'L'], [[4], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L']]), ('regression: eos after isolate initiator', [['R', 'R', 'ON', 'LRI', 'LRI', 'RLI', 'ON', 'L', 'R'], [1, 1, 1, 1, 2, 4, 5, 5, 5], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7, 8], 'R', 'R']]), ('regression: eos after isolate initiator', [['PDI', 'RLI', 'ON'], [0, 0, 1], 0], [[[0, 1], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'EN', 'ON', 'PDI', 'LRI', 'PDI', 'FSI', 'L', 'R', 'RLI'], [0, 0, 0, 0, 0, 0, 0, 2, 3, 2], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L'], [[7], 'L', 'R'], [[8], 'R', 'R'], [[9], 'R', 'L']]), ('control layout', [['EN', 'R', 'ON', 'EN'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']])], [('regression: eos after isolate initiator', [['LRI', 'PDI', 'EN', 'EN', 'L', 'EN', 'PDI', 'FSI', 'ON', 'R', 'R', 'R', 'FSI'], [1, 1, 2, 1, 2, 1, 1, 1, 2, 3, 3, 3, 2], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R'], [[8], 'L', 'R'], [[9, 10, 11], 'R', 'R'], [[12], 'R', 'L']]), ('regression: eos after isolate initiator', [['EN', 'EN', 'PDI', 'R', 'PDI'], [0, 0, 0, 1, 0], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'L']]), ('regression: eos after isolate initiator', [['ON', 'FSI', 'FSI', 'FSI', 'R', 'PDI', 'FSI', 'EN', 'L', 'ON', 'ON', 'EN', 'L', 'L'], [1, 1, 2, 4, 6, 4, 4, 6, 6, 6, 6, 6, 6, 6], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L'], [[3, 5, 6], 'L', 'L'], [[4], 'L', 'L'], [[7, 8, 9, 10, 11, 12, 13], 'L', 'L']]), ('regression: eos after isolate initiator', [['LRI', 'L', 'ON', 'ON', 'R'], [1, 2, 2, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'L', 'L']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('control layout', [['R', 'LRI', 'LRI', 'R', 'PDI', 'ON', 'PDI'], [1, 0, 2, 5, 2, 2, 0], 0], [[[0], 'R', 'R'], [[1, 6], 'R', 'L'], [[2, 4, 5], 'L', 'L'], [[3], 'R', 'R']]), ('control layout', [['PDI', 'R', 'FSI'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Sequences ending in an unmatched initiator take eos from the isolate content.

Input [classes, levels, paragraph level]. Split into maximal level runs; chain a run ending in a matched isolate initiator (LRI/RLI/FSI) with the run starting at its matching PDI (stack matching; unmatched PDI ignored). For each sequence sos = parity of max(level of first char, level of preceding char or paragraph level); eos = parity of max(level of last char, level of following char or paragraph level), using the paragraph level when the last char is an isolate initiator. Return [[indices, sos, eos]].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    classes, levels, para = x
    n = len(classes)
    match = {}
    stack = []
    for i, c in enumerate(classes):
        if c in ('LRI', 'RLI', 'FSI'):
            stack.append(i)
        elif c == 'PDI' and stack:
            match[stack.pop()] = i
    runs = []
    for i in range(n):
        if runs and levels[i] == levels[runs[-1][-1]]:
            runs[-1].append(i)
        else:
            runs.append([i])
    start_of = {r[0]: r for r in runs}
    seqs = []
    used = set()
    for r in runs:
        if r[0] in used:
            continue
        seq = list(r)
        used.add(r[0])
        while True:
            tail = seq[-1]
            if classes[tail] not in ('LRI', 'RLI', 'FSI') or tail not in match:
                break
            nxt = start_of.get(match[tail])
            if nxt is None:
                break
            seq.extend(nxt)
            used.add(nxt[0])
        seqs.append(seq)
    out = []
    for seq in seqs:
        first, last = seq[0], seq[-1]
        lv = levels[first]
        prev = levels[first - 1] if first > 0 else para
        sos = 'R' if max(lv, prev) % 2 else 'L'
        if classes[last] in ('LRI', 'RLI', 'FSI'):
            nxt_lv = para
        else:
            nxt_lv = levels[last + 1] if last + 1 < n else para
        eos = 'R' if nxt_lv % 2 else 'L'
        out.append([seq, sos, eos])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('regression: eos level comparison', [['L', 'L', 'EN'], [2, 2, 2], 1], [[[0, 1, 2], 'L', 'L']]), ('regression: eos level comparison', [['EN', 'L', 'EN', 'PDI', 'EN'], [1, 2, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4], 'L', 'R']]), ('regression: eos level comparison', [['R', 'EN', 'PDI', 'R', 'ON', 'R'], [0, 0, 0, 1, 0, 1], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'R'], [[5], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'PDI', 'L', 'LRI', 'LRI', 'EN', 'EN', 'L'], [0, 0, 0, 0, 2, 4, 4, 4], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'L']]), ('control layout', [['EN', 'EN', 'RLI', 'PDI', 'R', 'EN', 'PDI', 'ON', 'EN', 'ON', 'L', 'ON'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 'L', 'L']])], [('regression: eos level comparison', [['R', 'R', 'RLI', 'L', 'PDI', 'ON', 'L', 'EN', 'L', 'EN', 'L'], [0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2, 4, 5, 6, 7, 8, 9, 10], 'R', 'L'], [[3], 'L', 'L']]), ('regression: eos level comparison', [['FSI', 'ON', 'EN', 'R', 'PDI', 'L', 'L', 'EN'], [0, 2, 2, 3, 0, 0, 0, 0], 0], [[[0, 4, 5, 6, 7], 'L', 'L'], [[1, 2], 'L', 'R'], [[3], 'R', 'R']]), ('regression: eos level comparison', [['LRI', 'R', 'PDI', 'EN', 'L', 'EN', 'EN', 'L', 'LRI', 'PDI', 'ON', 'R', 'L'], [1, 3, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1], 1], [[[0, 2], 'R', 'L'], [[1], 'R', 'R'], [[3], 'L', 'L'], [[4, 5], 'L', 'L'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11, 12], 'L', 'R']]), ('regression: eos level comparison', [['L', 'ON', 'PDI', 'L', 'R', 'PDI', 'EN', 'ON', 'ON', 'LRI', 'L', 'ON', 'PDI', 'FSI'], [2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'L'], [[4, 5], 'L', 'L'], [[6], 'L', 'L'], [[7, 8, 9, 12, 13], 'L', 'R'], [[10, 11], 'L', 'L']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('control layout', [['PDI', 'PDI', 'L', 'ON', 'ON', 'L', 'ON', 'ON', 'EN', 'PDI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'L', 'L']]), ('control layout', [['R', 'L', 'PDI'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']])], [('regression: eos level comparison', [['LRI', 'LRI', 'EN', 'PDI', 'EN', 'R', 'R', 'R', 'L', 'PDI', 'EN'], [1, 2, 4, 2, 2, 2, 3, 3, 2, 1, 2], 1], [[[0, 9], 'R', 'L'], [[1, 3, 4, 5], 'L', 'R'], [[2], 'L', 'L'], [[6, 7], 'R', 'R'], [[8], 'R', 'L'], [[10], 'L', 'L']]), ('regression: eos level comparison', [['RLI', 'RLI', 'R', 'PDI', 'EN', 'R', 'ON', 'PDI', 'L', 'L', 'EN', 'L', 'L'], [0, 1, 3, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], 0], [[[0, 7, 8, 9, 10, 11, 12], 'L', 'L'], [[1, 3, 4, 5, 6], 'R', 'R'], [[2], 'R', 'R']]), ('regression: eos level comparison', [['EN', 'EN', 'PDI', 'R', 'PDI'], [0, 0, 0, 1, 0], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'L']]), ('regression: eos level comparison', [['R', 'EN', 'R', 'FSI', 'ON', 'R', 'PDI', 'PDI', 'PDI', 'ON', 'RLI', 'ON', 'EN', 'R'], [1, 0, 1, 0, 2, 2, 0, 0, 0, 0, 0, 1, 1, 1], 0], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2], 'R', 'R'], [[3, 6, 7, 8, 9, 10], 'R', 'L'], [[4, 5], 'L', 'L'], [[11, 12, 13], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'R', 'ON', 'R', 'ON', 'L', 'EN', 'L', 'PDI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'L', 'L']]), ('control layout', [['EN', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])], [('regression: eos level comparison', [['EN', 'L', 'ON', 'PDI', 'R', 'R', 'LRI'], [1, 2, 1, 1, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4, 5, 6], 'L', 'R']]), ('regression: eos level comparison', [['PDI', 'EN', 'ON', 'EN', 'ON', 'ON', 'RLI', 'R', 'ON', 'EN', 'PDI', 'L'], [1, 2, 1, 2, 1, 1, 1, 3, 3, 4, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2], 'L', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 10], 'L', 'L'], [[7, 8], 'R', 'L'], [[9], 'L', 'L'], [[11], 'L', 'L']]), ('regression: eos level comparison', [['FSI', 'EN', 'PDI', 'L', 'R', 'ON', 'ON', 'LRI'], [1, 2, 1, 2, 1, 1, 1, 1], 1], [[[0, 2], 'R', 'L'], [[1], 'L', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7], 'L', 'R']]), ('regression: eos level comparison', [['R', 'R', 'L', 'L', 'ON'], [1, 1, 2, 1, 1], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3, 4], 'L', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['R', 'R', 'EN', 'L', 'ON', 'ON', 'RLI', 'PDI', 'RLI'], [1, 1, 1, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'R', 'R']]), ('control layout', [['RLI', 'RLI', 'ON', 'RLI', 'L', 'PDI', 'R'], [1, 3, 5, 5, 7, 5, 5], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2, 3, 5, 6], 'R', 'R'], [[4], 'R', 'R']])], [('regression: eos level comparison', [['L', 'ON', 'EN', 'R'], [0, 0, 0, 1], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R']]), ('regression: eos level comparison', [['ON', 'ON', 'ON', 'RLI', 'EN', 'L'], [1, 1, 1, 1, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'R']]), ('regression: eos level comparison', [['ON', 'LRI', 'RLI', 'R', 'L', 'RLI', 'ON', 'EN'], [1, 1, 2, 3, 3, 3, 5, 6], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L'], [[3, 4, 5], 'R', 'R'], [[6], 'R', 'L'], [[7], 'L', 'L']]), ('regression: eos level comparison', [['RLI', 'RLI', 'L', 'L'], [0, 1, 3, 4], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2], 'R', 'L'], [[3], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['L', 'ON', 'EN', 'R', 'EN', 'L', 'FSI'], [0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L']]), ('control layout', [['PDI', 'PDI', 'ON', 'L', 'FSI'], [0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4], 'L', 'L']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    classes, levels, para = x
    n = len(classes)
    match = {}
    stack = []
    for i, c in enumerate(classes):
        if c in ('LRI', 'RLI', 'FSI'):
            stack.append(i)
        elif c == 'PDI' and stack:
            match[stack.pop()] = i
    runs = []
    for i in range(n):
        if runs and levels[i] == levels[runs[-1][-1]]:
            runs[-1].append(i)
        else:
            runs.append([i])
    start_of = {r[0]: r for r in runs}
    seqs = []
    used = set()
    for r in runs:
        if r[0] in used:
            continue
        seq = list(r)
        used.add(r[0])
        while True:
            tail = seq[-1]
            if classes[tail] not in ('LRI', 'RLI', 'FSI') or tail not in match:
                break
            nxt = start_of.get(match[tail])
            if nxt is None:
                break
            seq.extend(nxt)
            used.add(nxt[0])
        seqs.append(seq)
    out = []
    for seq in seqs:
        first, last = seq[0], seq[-1]
        lv = levels[first]
        prev = levels[first - 1] if first > 0 else para
        sos = 'R' if max(lv, prev) % 2 else 'L'
        if classes[last] in ('LRI', 'RLI', 'FSI'):
            nxt_lv = para
        else:
            nxt_lv = levels[last + 1] if last + 1 < n else para
        eos = 'R' if levels[last] % 2 else 'L'
        out.append([seq, sos, eos])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('regression: eos level comparison', [['L', 'L', 'EN'], [2, 2, 2], 1], [[[0, 1, 2], 'L', 'L']]), ('regression: eos level comparison', [['EN', 'L', 'EN', 'PDI', 'EN'], [1, 2, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4], 'L', 'R']]), ('regression: eos level comparison', [['R', 'EN', 'PDI', 'R', 'ON', 'R'], [0, 0, 0, 1, 0, 1], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'R'], [[5], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'PDI', 'L', 'LRI', 'LRI', 'EN', 'EN', 'L'], [0, 0, 0, 0, 2, 4, 4, 4], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'L']]), ('control layout', [['EN', 'EN', 'RLI', 'PDI', 'R', 'EN', 'PDI', 'ON', 'EN', 'ON', 'L', 'ON'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 'L', 'L']])], [('regression: eos level comparison', [['R', 'R', 'RLI', 'L', 'PDI', 'ON', 'L', 'EN', 'L', 'EN', 'L'], [0, 1, 0, 2, 0, 0, 0, 0, 0, 0, 0], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2, 4, 5, 6, 7, 8, 9, 10], 'R', 'L'], [[3], 'L', 'L']]), ('regression: eos level comparison', [['FSI', 'ON', 'EN', 'R', 'PDI', 'L', 'L', 'EN'], [0, 2, 2, 3, 0, 0, 0, 0], 0], [[[0, 4, 5, 6, 7], 'L', 'L'], [[1, 2], 'L', 'R'], [[3], 'R', 'R']]), ('regression: eos level comparison', [['LRI', 'R', 'PDI', 'EN', 'L', 'EN', 'EN', 'L', 'LRI', 'PDI', 'ON', 'R', 'L'], [1, 3, 1, 2, 1, 1, 2, 2, 1, 1, 1, 1, 1], 1], [[[0, 2], 'R', 'L'], [[1], 'R', 'R'], [[3], 'L', 'L'], [[4, 5], 'L', 'L'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11, 12], 'L', 'R']]), ('regression: eos level comparison', [['L', 'ON', 'PDI', 'L', 'R', 'PDI', 'EN', 'ON', 'ON', 'LRI', 'L', 'ON', 'PDI', 'FSI'], [2, 1, 1, 2, 1, 1, 2, 1, 1, 1, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'L'], [[4, 5], 'L', 'L'], [[6], 'L', 'L'], [[7, 8, 9, 12, 13], 'L', 'R'], [[10, 11], 'L', 'L']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('control layout', [['PDI', 'PDI', 'L', 'ON', 'ON', 'L', 'ON', 'ON', 'EN', 'PDI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'L', 'L']]), ('control layout', [['R', 'L', 'PDI'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']])], [('regression: eos level comparison', [['LRI', 'LRI', 'EN', 'PDI', 'EN', 'R', 'R', 'R', 'L', 'PDI', 'EN'], [1, 2, 4, 2, 2, 2, 3, 3, 2, 1, 2], 1], [[[0, 9], 'R', 'L'], [[1, 3, 4, 5], 'L', 'R'], [[2], 'L', 'L'], [[6, 7], 'R', 'R'], [[8], 'R', 'L'], [[10], 'L', 'L']]), ('regression: eos level comparison', [['RLI', 'RLI', 'R', 'PDI', 'EN', 'R', 'ON', 'PDI', 'L', 'L', 'EN', 'L', 'L'], [0, 1, 3, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0], 0], [[[0, 7, 8, 9, 10, 11, 12], 'L', 'L'], [[1, 3, 4, 5, 6], 'R', 'R'], [[2], 'R', 'R']]), ('regression: eos level comparison', [['EN', 'EN', 'PDI', 'R', 'PDI'], [0, 0, 0, 1, 0], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4], 'R', 'L']]), ('regression: eos level comparison', [['R', 'EN', 'R', 'FSI', 'ON', 'R', 'PDI', 'PDI', 'PDI', 'ON', 'RLI', 'ON', 'EN', 'R'], [1, 0, 1, 0, 2, 2, 0, 0, 0, 0, 0, 1, 1, 1], 0], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2], 'R', 'R'], [[3, 6, 7, 8, 9, 10], 'R', 'L'], [[4, 5], 'L', 'L'], [[11, 12, 13], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'R', 'ON', 'R', 'ON', 'L', 'EN', 'L', 'PDI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'L', 'L']]), ('control layout', [['EN', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])], [('regression: eos level comparison', [['EN', 'L', 'ON', 'PDI', 'R', 'R', 'LRI'], [1, 2, 1, 1, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4, 5, 6], 'L', 'R']]), ('regression: eos level comparison', [['PDI', 'EN', 'ON', 'EN', 'ON', 'ON', 'RLI', 'R', 'ON', 'EN', 'PDI', 'L'], [1, 2, 1, 2, 1, 1, 1, 3, 3, 4, 1, 2], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2], 'L', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 10], 'L', 'L'], [[7, 8], 'R', 'L'], [[9], 'L', 'L'], [[11], 'L', 'L']]), ('regression: eos level comparison', [['FSI', 'EN', 'PDI', 'L', 'R', 'ON', 'ON', 'LRI'], [1, 2, 1, 2, 1, 1, 1, 1], 1], [[[0, 2], 'R', 'L'], [[1], 'L', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7], 'L', 'R']]), ('regression: eos level comparison', [['R', 'R', 'L', 'L', 'ON'], [1, 1, 2, 1, 1], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3, 4], 'L', 'R']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['R', 'R', 'EN', 'L', 'ON', 'ON', 'RLI', 'PDI', 'RLI'], [1, 1, 1, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'R', 'R']]), ('control layout', [['RLI', 'RLI', 'ON', 'RLI', 'L', 'PDI', 'R'], [1, 3, 5, 5, 7, 5, 5], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2, 3, 5, 6], 'R', 'R'], [[4], 'R', 'R']])], [('regression: eos level comparison', [['L', 'ON', 'EN', 'R'], [0, 0, 0, 1], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R']]), ('regression: eos level comparison', [['ON', 'ON', 'ON', 'RLI', 'EN', 'L'], [1, 1, 1, 1, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'R']]), ('regression: eos level comparison', [['ON', 'LRI', 'RLI', 'R', 'L', 'RLI', 'ON', 'EN'], [1, 1, 2, 3, 3, 3, 5, 6], 1], [[[0, 1], 'R', 'R'], [[2], 'L', 'L'], [[3, 4, 5], 'R', 'R'], [[6], 'R', 'L'], [[7], 'L', 'L']]), ('regression: eos level comparison', [['RLI', 'RLI', 'L', 'L'], [0, 1, 3, 4], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2], 'R', 'L'], [[3], 'L', 'L']]), ('isolate joins outer run', [['L', 'RLI', 'R', 'PDI', 'L'], [0, 0, 1, 0, 0], 0], [[[0, 1, 3, 4], 'L', 'L'], [[2], 'R', 'R']]), ('nested isolates', [['R', 'LRI', 'L', 'RLI', 'R', 'PDI', 'PDI', 'R'], [1, 1, 2, 2, 3, 2, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'R'], [[2, 3, 5], 'L', 'L'], [[4], 'R', 'R']]), ('control layout', [['L', 'ON', 'EN', 'R', 'EN', 'L', 'FSI'], [0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L']]), ('control layout', [['PDI', 'PDI', 'ON', 'L', 'FSI'], [0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4], 'L', 'L']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Sequence end types follow the next character only.

Input [classes, levels, paragraph level]. Split into maximal level runs; chain a run ending in a matched isolate initiator (LRI/RLI/FSI) with the run starting at its matching PDI (stack matching; unmatched PDI ignored). For each sequence sos = parity of max(level of first char, level of preceding char or paragraph level); eos = parity of max(level of last char, level of following char or paragraph level), using the paragraph level when the last char is an isolate initiator. Return [[indices, sos, eos]].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] = i - runs[-1][0]
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run length accumulation', [2, 2, 1, 1, 1, 1, 1, 0, 0, 0], [[2, 5, 'rtl'], [0, 2, 'ltr'], [7, 3, 'ltr']]), ('regression: run length accumulation', [4, 3, 3, 2, 2, 4], [[1, 2, 'rtl'], [0, 1, 'ltr'], [3, 2, 'ltr'], [5, 1, 'ltr']]), ('regression: run length accumulation', [1, 1, 2, 4, 4, 4, 2, 2, 2, 2, 4, 4], [[2, 1, 'ltr'], [3, 3, 'ltr'], [6, 4, 'ltr'], [10, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [3], [[0, 1, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']]), ('control layout', [0], [[0, 1, 'ltr']])], [('regression: run length accumulation', [2, 2, 2, 3, 3, 3, 3, 2], [[0, 3, 'ltr'], [3, 4, 'rtl'], [7, 1, 'ltr']]), ('regression: run length accumulation', [2, 2, 1], [[2, 1, 'rtl'], [0, 2, 'ltr']]), ('regression: run length accumulation', [1, 1, 1, 1, 1, 1, 1], [[0, 7, 'rtl']]), ('regression: run length accumulation', [0, 0, 0], [[0, 3, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1], [[0, 1, 'rtl']]), ('control layout', [3], [[0, 1, 'rtl']])], [('regression: run length accumulation', [1, 1], [[0, 2, 'rtl']]), ('regression: run length accumulation', [1, 1, 3, 1, 1, 1, 1, 2, 2], [[7, 2, 'ltr'], [3, 4, 'rtl'], [2, 1, 'rtl'], [0, 2, 'rtl']]), ('regression: run length accumulation', [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [[0, 6, 'ltr'], [6, 4, 'rtl']]), ('regression: run length accumulation', [2, 2, 2, 2, 1, 1], [[4, 2, 'rtl'], [0, 4, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']])], [('regression: run length accumulation', [3, 3, 3, 0, 0, 0, 0, 0, 0], [[0, 3, 'rtl'], [3, 6, 'ltr']]), ('regression: run length accumulation', [1, 1, 1], [[0, 3, 'rtl']]), ('regression: run length accumulation', [0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1], [[0, 2, 'ltr'], [3, 8, 'rtl'], [2, 1, 'rtl']]), ('regression: run length accumulation', [2, 2, 0, 0, 0, 0, 1, 1, 0], [[0, 2, 'ltr'], [2, 4, 'ltr'], [6, 2, 'rtl'], [8, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0], [[0, 1, 'ltr']]), ('control layout', [1], [[0, 1, 'rtl']])], [('regression: run length accumulation', [2, 2, 1, 1, 1, 1, 1], [[2, 5, 'rtl'], [0, 2, 'ltr']]), ('regression: run length accumulation', [2, 2, 2, 2, 1, 1, 1, 1, 3, 3, 1, 2], [[11, 1, 'ltr'], [10, 1, 'rtl'], [8, 2, 'rtl'], [4, 4, 'rtl'], [0, 4, 'ltr']]), ('regression: run length accumulation', [1, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [[1, 2, 'rtl'], [0, 1, 'rtl'], [3, 9, 'ltr']]), ('regression: run length accumulation', [2, 4, 4, 1, 1, 1, 1, 2], [[7, 1, 'ltr'], [3, 4, 'rtl'], [0, 1, 'ltr'], [1, 2, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']]), ('control layout', [3], [[0, 1, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] = i
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run length accumulation', [2, 2, 1, 1, 1, 1, 1, 0, 0, 0], [[2, 5, 'rtl'], [0, 2, 'ltr'], [7, 3, 'ltr']]), ('regression: run length accumulation', [4, 3, 3, 2, 2, 4], [[1, 2, 'rtl'], [0, 1, 'ltr'], [3, 2, 'ltr'], [5, 1, 'ltr']]), ('regression: run length accumulation', [1, 1, 2, 4, 4, 4, 2, 2, 2, 2, 4, 4], [[2, 1, 'ltr'], [3, 3, 'ltr'], [6, 4, 'ltr'], [10, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [3], [[0, 1, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']]), ('control layout', [0], [[0, 1, 'ltr']])], [('regression: run length accumulation', [2, 2, 2, 3, 3, 3, 3, 2], [[0, 3, 'ltr'], [3, 4, 'rtl'], [7, 1, 'ltr']]), ('regression: run length accumulation', [2, 2, 1], [[2, 1, 'rtl'], [0, 2, 'ltr']]), ('regression: run length accumulation', [1, 1, 1, 1, 1, 1, 1], [[0, 7, 'rtl']]), ('regression: run length accumulation', [0, 0, 0], [[0, 3, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1], [[0, 1, 'rtl']]), ('control layout', [3], [[0, 1, 'rtl']])], [('regression: run length accumulation', [1, 1], [[0, 2, 'rtl']]), ('regression: run length accumulation', [1, 1, 3, 1, 1, 1, 1, 2, 2], [[7, 2, 'ltr'], [3, 4, 'rtl'], [2, 1, 'rtl'], [0, 2, 'rtl']]), ('regression: run length accumulation', [0, 0, 0, 0, 0, 0, 1, 1, 1, 1], [[0, 6, 'ltr'], [6, 4, 'rtl']]), ('regression: run length accumulation', [2, 2, 2, 2, 1, 1], [[4, 2, 'rtl'], [0, 4, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']])], [('regression: run length accumulation', [3, 3, 3, 0, 0, 0, 0, 0, 0], [[0, 3, 'rtl'], [3, 6, 'ltr']]), ('regression: run length accumulation', [1, 1, 1], [[0, 3, 'rtl']]), ('regression: run length accumulation', [0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1], [[0, 2, 'ltr'], [3, 8, 'rtl'], [2, 1, 'rtl']]), ('regression: run length accumulation', [2, 2, 0, 0, 0, 0, 1, 1, 0], [[0, 2, 'ltr'], [2, 4, 'ltr'], [6, 2, 'rtl'], [8, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0], [[0, 1, 'ltr']]), ('control layout', [1], [[0, 1, 'rtl']])], [('regression: run length accumulation', [2, 2, 1, 1, 1, 1, 1], [[2, 5, 'rtl'], [0, 2, 'ltr']]), ('regression: run length accumulation', [2, 2, 2, 2, 1, 1, 1, 1, 3, 3, 1, 2], [[11, 1, 'ltr'], [10, 1, 'rtl'], [8, 2, 'rtl'], [4, 4, 'rtl'], [0, 4, 'ltr']]), ('regression: run length accumulation', [1, 3, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0], [[1, 2, 'rtl'], [0, 1, 'rtl'], [3, 9, 'ltr']]), ('regression: run length accumulation', [2, 4, 4, 1, 1, 1, 1, 2], [[7, 1, 'ltr'], [3, 4, 'rtl'], [0, 1, 'ltr'], [1, 2, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2], [[0, 1, 'ltr']]), ('control layout', [3], [[0, 1, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Runs report lengths one short, dropping their last glyph.

Input resolved levels of a line. Group maximal equal-level runs [logical start, length, level]; reorder whole runs from the highest level down to the lowest odd level by reversing sequences of runs at or above the level. Return [[start, length, "rtl"|"ltr"]] in visual order.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv > 0 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run direction label', [1, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1], [[7, 5, 'rtl'], [1, 3, 'rtl'], [4, 3, 'ltr'], [0, 1, 'rtl']]), ('partial-repair probe', [3, 3, 3, 1], [[3, 1, 'rtl'], [0, 3, 'rtl']]), ('regression: run direction label', [3, 3, 2, 4, 4, 2, 4], [[0, 2, 'rtl'], [2, 1, 'ltr'], [3, 2, 'ltr'], [5, 1, 'ltr'], [6, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1, 1, 0, 0], [[0, 4, 'rtl'], [4, 2, 'ltr']]), ('control layout', [0, 0, 0, 0, 0], [[0, 5, 'ltr']]), ('control layout', [0, 0, 1, 1, 1], [[0, 2, 'ltr'], [2, 3, 'rtl']])], [('regression: run direction label', [2, 2, 3, 3, 3, 3, 0], [[0, 2, 'ltr'], [2, 4, 'rtl'], [6, 1, 'ltr']]), ('regression: run direction label', [3, 3, 3, 3, 1, 1, 1, 1, 2], [[8, 1, 'ltr'], [4, 4, 'rtl'], [0, 4, 'rtl']]), ('regression: run direction label', [0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 2], [[0, 6, 'ltr'], [6, 4, 'rtl'], [10, 1, 'ltr']]), ('regression: run direction label', [2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1], [[10, 2, 'rtl'], [0, 1, 'ltr'], [1, 9, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1], [[0, 2, 'rtl']]), ('control layout', [1, 1], [[0, 2, 'rtl']])], [('regression: run direction label', [2, 2, 2, 2, 1, 1, 1, 1, 3, 3], [[8, 2, 'rtl'], [4, 4, 'rtl'], [0, 4, 'ltr']]), ('regression: run direction label', [3, 3, 3, 1, 1, 3, 3, 3, 1, 1, 1, 2], [[11, 1, 'ltr'], [8, 3, 'rtl'], [5, 3, 'rtl'], [3, 2, 'rtl'], [0, 3, 'rtl']]), ('regression: run direction label', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('regression: run direction label', [2, 2, 1, 1, 1, 2, 2, 3, 3, 3, 0], [[5, 2, 'ltr'], [7, 3, 'rtl'], [2, 3, 'rtl'], [0, 2, 'ltr'], [10, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1], [[0, 8, 'rtl']]), ('control layout', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']])], [('regression: run direction label', [2, 2, 2, 2, 2, 2, 4, 1, 3, 3, 3, 3], [[8, 4, 'rtl'], [7, 1, 'rtl'], [0, 6, 'ltr'], [6, 1, 'ltr']]), ('regression: run direction label', [2, 2, 2, 3, 3, 3, 1], [[6, 1, 'rtl'], [0, 3, 'ltr'], [3, 3, 'rtl']]), ('regression: run direction label', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('partial-repair probe', [0, 0, 0, 0, 3], [[0, 4, 'ltr'], [4, 1, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1], [[0, 8, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [[0, 11, 'rtl']])], [('regression: run direction label', [4, 4, 4, 4, 1], [[4, 1, 'rtl'], [0, 4, 'ltr']]), ('regression: run direction label', [1, 3, 4], [[2, 1, 'ltr'], [1, 1, 'rtl'], [0, 1, 'rtl']]), ('regression: run direction label', [2, 1, 1, 1, 1, 3, 3, 3, 3, 1, 4], [[10, 1, 'ltr'], [9, 1, 'rtl'], [5, 4, 'rtl'], [1, 4, 'rtl'], [0, 1, 'ltr']]), ('partial-repair probe', [3, 3], [[0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1], [[0, 6, 'rtl']]), ('control layout', [1, 1], [[0, 2, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv == 1 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run direction label', [1, 3, 3, 3, 2, 2, 2, 1, 1, 1, 1, 1], [[7, 5, 'rtl'], [1, 3, 'rtl'], [4, 3, 'ltr'], [0, 1, 'rtl']]), ('partial-repair probe', [3, 3, 3, 1], [[3, 1, 'rtl'], [0, 3, 'rtl']]), ('regression: run direction label', [3, 3, 2, 4, 4, 2, 4], [[0, 2, 'rtl'], [2, 1, 'ltr'], [3, 2, 'ltr'], [5, 1, 'ltr'], [6, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1, 1, 0, 0], [[0, 4, 'rtl'], [4, 2, 'ltr']]), ('control layout', [0, 0, 0, 0, 0], [[0, 5, 'ltr']]), ('control layout', [0, 0, 1, 1, 1], [[0, 2, 'ltr'], [2, 3, 'rtl']])], [('regression: run direction label', [2, 2, 3, 3, 3, 3, 0], [[0, 2, 'ltr'], [2, 4, 'rtl'], [6, 1, 'ltr']]), ('regression: run direction label', [3, 3, 3, 3, 1, 1, 1, 1, 2], [[8, 1, 'ltr'], [4, 4, 'rtl'], [0, 4, 'rtl']]), ('regression: run direction label', [0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 2], [[0, 6, 'ltr'], [6, 4, 'rtl'], [10, 1, 'ltr']]), ('regression: run direction label', [2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 1, 1], [[10, 2, 'rtl'], [0, 1, 'ltr'], [1, 9, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1], [[0, 2, 'rtl']]), ('control layout', [1, 1], [[0, 2, 'rtl']])], [('regression: run direction label', [2, 2, 2, 2, 1, 1, 1, 1, 3, 3], [[8, 2, 'rtl'], [4, 4, 'rtl'], [0, 4, 'ltr']]), ('regression: run direction label', [3, 3, 3, 1, 1, 3, 3, 3, 1, 1, 1, 2], [[11, 1, 'ltr'], [8, 3, 'rtl'], [5, 3, 'rtl'], [3, 2, 'rtl'], [0, 3, 'rtl']]), ('regression: run direction label', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('regression: run direction label', [2, 2, 1, 1, 1, 2, 2, 3, 3, 3, 0], [[5, 2, 'ltr'], [7, 3, 'rtl'], [2, 3, 'rtl'], [0, 2, 'ltr'], [10, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1], [[0, 8, 'rtl']]), ('control layout', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']])], [('regression: run direction label', [2, 2, 2, 2, 2, 2, 4, 1, 3, 3, 3, 3], [[8, 4, 'rtl'], [7, 1, 'rtl'], [0, 6, 'ltr'], [6, 1, 'ltr']]), ('regression: run direction label', [2, 2, 2, 3, 3, 3, 1], [[6, 1, 'rtl'], [0, 3, 'ltr'], [3, 3, 'rtl']]), ('regression: run direction label', [2, 3], [[0, 1, 'ltr'], [1, 1, 'rtl']]), ('partial-repair probe', [0, 0, 0, 0, 3], [[0, 4, 'ltr'], [4, 1, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1], [[0, 8, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [[0, 11, 'rtl']])], [('regression: run direction label', [4, 4, 4, 4, 1], [[4, 1, 'rtl'], [0, 4, 'ltr']]), ('regression: run direction label', [1, 3, 4], [[2, 1, 'ltr'], [1, 1, 'rtl'], [0, 1, 'rtl']]), ('regression: run direction label', [2, 1, 1, 1, 1, 3, 3, 3, 3, 1, 4], [[10, 1, 'ltr'], [9, 1, 'rtl'], [5, 4, 'rtl'], [1, 4, 'rtl'], [0, 1, 'ltr']]), ('partial-repair probe', [3, 3], [[0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1], [[0, 6, 'rtl']]), ('control layout', [1, 1], [[0, 2, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Level-2 runs are shaped right-to-left.

Input resolved levels of a line. Group maximal equal-level runs [logical start, length, level]; reorder whole runs from the highest level down to the lowest odd level by reversing sequences of runs at or above the level. Return [[start, length, "rtl"|"ltr"]] in visual order.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd), -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: lowest odd pass', [0, 0, 0, 0, 1, 3], [[0, 4, 'ltr'], [5, 1, 'rtl'], [4, 1, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 3, 3, 3, 0, 0, 1, 1, 0], [[0, 3, 'ltr'], [3, 3, 'rtl'], [6, 2, 'ltr'], [8, 2, 'rtl'], [10, 1, 'ltr']]), ('regression: lowest odd pass', [0, 1, 1, 1, 1, 1, 2, 2], [[0, 1, 'ltr'], [6, 2, 'ltr'], [1, 5, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 0, 0], [[0, 4, 'ltr']]), ('control layout', [4, 4, 4, 4], [[0, 4, 'ltr']]), ('control layout', [2], [[0, 1, 'ltr']])], [('regression: lowest odd pass', [1, 1, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2], [[3, 5, 'rtl'], [8, 4, 'ltr'], [0, 3, 'rtl']]), ('regression: lowest odd pass', [1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 3], [[0, 3, 'rtl'], [3, 4, 'ltr'], [7, 4, 'ltr'], [11, 1, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 2, 1, 1, 1], [[4, 3, 'rtl'], [0, 4, 'ltr']]), ('partial-repair probe', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 0, 0, 0, 2, 2, 2, 0], [[0, 5, 'ltr'], [5, 3, 'ltr'], [8, 1, 'ltr']]), ('control layout', [0, 0], [[0, 2, 'ltr']])], [('regression: lowest odd pass', [3, 3, 3, 4, 4, 4, 2, 2, 2, 2, 1], [[10, 1, 'rtl'], [3, 3, 'ltr'], [0, 3, 'rtl'], [6, 4, 'ltr']]), ('regression: lowest odd pass', [1, 2, 2], [[1, 2, 'ltr'], [0, 1, 'rtl']]), ('regression: lowest odd pass', [3, 3, 3, 1, 1, 1, 1], [[3, 4, 'rtl'], [0, 3, 'rtl']]), ('partial-repair probe', [1, 1, 0, 0, 0, 0, 3], [[0, 2, 'rtl'], [2, 4, 'ltr'], [6, 1, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1], [[0, 7, 'rtl']]), ('control layout', [4, 4, 2, 4], [[0, 2, 'ltr'], [2, 1, 'ltr'], [3, 1, 'ltr']])], [('regression: lowest odd pass', [0, 1, 1, 1, 1, 3], [[0, 1, 'ltr'], [5, 1, 'rtl'], [1, 4, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 1, 1], [[3, 2, 'rtl'], [0, 3, 'ltr']]), ('regression: lowest odd pass', [1, 1, 1, 1, 3, 0, 0, 0, 1], [[4, 1, 'rtl'], [0, 4, 'rtl'], [5, 3, 'ltr'], [8, 1, 'rtl']]), ('regression: lowest odd pass', [3, 3, 3, 1, 1, 3, 3], [[5, 2, 'rtl'], [3, 2, 'rtl'], [0, 3, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2, 2, 2, 2], [[0, 4, 'ltr']]), ('control layout', [2, 2, 2], [[0, 3, 'ltr']])], [('regression: lowest odd pass', [2, 2, 1, 1, 1], [[2, 3, 'rtl'], [0, 2, 'ltr']]), ('regression: lowest odd pass', [2, 1, 1, 1, 1, 3, 3, 3, 3, 1, 4], [[10, 1, 'ltr'], [9, 1, 'rtl'], [5, 4, 'rtl'], [1, 4, 'rtl'], [0, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('partial-repair probe', [0, 0, 0, 1, 1], [[0, 3, 'ltr'], [3, 2, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [3, 3, 3, 3], [[0, 4, 'rtl']]), ('control layout', [1, 1, 1, 1], [[0, 4, 'rtl']]), ('control layout', [4, 4, 4, 4, 4, 4, 4, 2, 2], [[0, 7, 'ltr'], [7, 2, 'ltr']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 2, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: lowest odd pass', [0, 0, 0, 0, 1, 3], [[0, 4, 'ltr'], [5, 1, 'rtl'], [4, 1, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 3, 3, 3, 0, 0, 1, 1, 0], [[0, 3, 'ltr'], [3, 3, 'rtl'], [6, 2, 'ltr'], [8, 2, 'rtl'], [10, 1, 'ltr']]), ('regression: lowest odd pass', [0, 1, 1, 1, 1, 1, 2, 2], [[0, 1, 'ltr'], [6, 2, 'ltr'], [1, 5, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 0, 0], [[0, 4, 'ltr']]), ('control layout', [4, 4, 4, 4], [[0, 4, 'ltr']]), ('control layout', [2], [[0, 1, 'ltr']])], [('regression: lowest odd pass', [1, 1, 1, 3, 3, 3, 3, 3, 2, 2, 2, 2], [[3, 5, 'rtl'], [8, 4, 'ltr'], [0, 3, 'rtl']]), ('regression: lowest odd pass', [1, 1, 1, 0, 0, 0, 0, 2, 2, 2, 2, 3], [[0, 3, 'rtl'], [3, 4, 'ltr'], [7, 4, 'ltr'], [11, 1, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 2, 1, 1, 1], [[4, 3, 'rtl'], [0, 4, 'ltr']]), ('partial-repair probe', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 0, 0, 0, 2, 2, 2, 0], [[0, 5, 'ltr'], [5, 3, 'ltr'], [8, 1, 'ltr']]), ('control layout', [0, 0], [[0, 2, 'ltr']])], [('regression: lowest odd pass', [3, 3, 3, 4, 4, 4, 2, 2, 2, 2, 1], [[10, 1, 'rtl'], [3, 3, 'ltr'], [0, 3, 'rtl'], [6, 4, 'ltr']]), ('regression: lowest odd pass', [1, 2, 2], [[1, 2, 'ltr'], [0, 1, 'rtl']]), ('regression: lowest odd pass', [3, 3, 3, 1, 1, 1, 1], [[3, 4, 'rtl'], [0, 3, 'rtl']]), ('partial-repair probe', [1, 1, 0, 0, 0, 0, 3], [[0, 2, 'rtl'], [2, 4, 'ltr'], [6, 1, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 1, 1, 1], [[0, 7, 'rtl']]), ('control layout', [4, 4, 2, 4], [[0, 2, 'ltr'], [2, 1, 'ltr'], [3, 1, 'ltr']])], [('regression: lowest odd pass', [0, 1, 1, 1, 1, 3], [[0, 1, 'ltr'], [5, 1, 'rtl'], [1, 4, 'rtl']]), ('regression: lowest odd pass', [2, 2, 2, 1, 1], [[3, 2, 'rtl'], [0, 3, 'ltr']]), ('regression: lowest odd pass', [1, 1, 1, 1, 3, 0, 0, 0, 1], [[4, 1, 'rtl'], [0, 4, 'rtl'], [5, 3, 'ltr'], [8, 1, 'rtl']]), ('regression: lowest odd pass', [3, 3, 3, 1, 1, 3, 3], [[5, 2, 'rtl'], [3, 2, 'rtl'], [0, 3, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [2, 2, 2, 2], [[0, 4, 'ltr']]), ('control layout', [2, 2, 2], [[0, 3, 'ltr']])], [('regression: lowest odd pass', [2, 2, 1, 1, 1], [[2, 3, 'rtl'], [0, 2, 'ltr']]), ('regression: lowest odd pass', [2, 1, 1, 1, 1, 3, 3, 3, 3, 1, 4], [[10, 1, 'ltr'], [9, 1, 'rtl'], [5, 4, 'rtl'], [1, 4, 'rtl'], [0, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('partial-repair probe', [0, 0, 0, 1, 1], [[0, 3, 'ltr'], [3, 2, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [3, 3, 3, 3], [[0, 4, 'rtl']]), ('control layout', [1, 1, 1, 1], [[0, 4, 'rtl']]), ('control layout', [4, 4, 4, 4, 4, 4, 4, 2, 2], [[0, 7, 'ltr'], [7, 2, 'ltr']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Runs at the lowest odd level stay in logical order.

Input resolved levels of a line. Group maximal equal-level runs [logical start, length, level]; reorder whole runs from the highest level down to the lowest odd level by reversing sequences of runs at or above the level. Return [[start, length, "rtl"|"ltr"]] in visual order.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 1, 4, 2, 2], [[4, 1, 'ltr'], [5, 2, 'ltr'], [0, 4, 'rtl']]), ('regression: run sequence reversal', [3, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2], [[8, 3, 'ltr'], [6, 2, 'rtl'], [0, 1, 'rtl'], [1, 5, 'ltr']]), ('regression: run sequence reversal', [4, 3, 4, 1, 1, 1, 1], [[3, 4, 'rtl'], [2, 1, 'ltr'], [1, 1, 'rtl'], [0, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [3, 3, 2, 2, 2, 2, 2], [[0, 2, 'rtl'], [2, 5, 'ltr']]), ('control layout', [0, 0, 0, 0, 0], [[0, 5, 'ltr']]), ('control layout', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']])], [('regression: run sequence reversal', [2, 1, 1], [[1, 2, 'rtl'], [0, 1, 'ltr']]), ('regression: run sequence reversal', [1, 1, 3], [[2, 1, 'rtl'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 3, 3, 3, 2, 2, 2, 0, 2, 2, 2, 1], [[1, 3, 'rtl'], [4, 3, 'ltr'], [0, 1, 'rtl'], [7, 1, 'ltr'], [11, 1, 'rtl'], [8, 3, 'ltr']]), ('regression: run sequence reversal', [4, 4, 4, 4, 2, 1, 1, 1, 1, 1, 1, 2], [[11, 1, 'ltr'], [5, 6, 'rtl'], [0, 4, 'ltr'], [4, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1], [[0, 4, 'rtl'], [4, 4, 'ltr'], [8, 3, 'rtl']]), ('control layout', [2, 2], [[0, 2, 'ltr']])], [('regression: run sequence reversal', [1, 1, 2, 2, 2, 2, 1, 1, 1, 1], [[6, 4, 'rtl'], [2, 4, 'ltr'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 2, 2, 2, 2, 2, 1], [[8, 1, 'rtl'], [3, 5, 'ltr'], [0, 3, 'rtl']]), ('partial-repair probe', [1, 1, 1, 1, 0, 0, 0, 3, 2, 2], [[0, 4, 'rtl'], [4, 3, 'ltr'], [7, 1, 'rtl'], [8, 2, 'ltr']]), ('regression: run sequence reversal', [2, 4, 4, 1, 2, 2], [[4, 2, 'ltr'], [3, 1, 'rtl'], [0, 1, 'ltr'], [1, 2, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 0, 0, 0], [[0, 2, 'rtl'], [2, 3, 'ltr']]), ('control layout', [2, 2, 3, 3, 3], [[0, 2, 'ltr'], [2, 3, 'rtl']])], [('regression: run sequence reversal', [3, 3, 3, 1], [[3, 1, 'rtl'], [0, 3, 'rtl']]), ('regression: run sequence reversal', [1, 2, 1, 1, 1, 1, 2, 2, 1, 1], [[8, 2, 'rtl'], [6, 2, 'ltr'], [2, 4, 'rtl'], [1, 1, 'ltr'], [0, 1, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 4, 4, 3, 4, 4], [[6, 2, 'ltr'], [5, 1, 'rtl'], [3, 2, 'ltr'], [0, 3, 'rtl']]), ('regression: run sequence reversal', [2, 4, 4, 4, 4, 1, 1], [[5, 2, 'rtl'], [0, 1, 'ltr'], [1, 4, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1], [[0, 3, 'rtl']]), ('control layout', [2, 2, 2, 2, 2], [[0, 5, 'ltr']])], [('regression: run sequence reversal', [1, 1, 1, 1, 1, 2, 2, 2, 2, 0], [[5, 4, 'ltr'], [0, 5, 'rtl'], [9, 1, 'ltr']]), ('regression: run sequence reversal', [1, 1, 1, 1, 3, 0, 0, 0, 1], [[4, 1, 'rtl'], [0, 4, 'rtl'], [5, 3, 'ltr'], [8, 1, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 1, 1, 3, 3, 3, 2, 1, 1], [[9, 2, 'rtl'], [5, 3, 'rtl'], [8, 1, 'ltr'], [0, 5, 'rtl']]), ('regression: run sequence reversal', [0, 0, 0, 0, 2, 2, 3, 3, 3, 3, 3, 1], [[0, 4, 'ltr'], [11, 1, 'rtl'], [4, 2, 'ltr'], [6, 5, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [[0, 2, 'ltr'], [2, 1, 'rtl'], [3, 9, 'ltr']]), ('control layout', [3, 3], [[0, 2, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    levels = x
    n = len(levels)
    runs = []
    for i in range(n):
        if runs and runs[-1][2] == levels[i]:
            runs[-1][1] += 1
        else:
            runs.append([i, 1, levels[i]])
    if not runs:
        return []
    odd = [r[2] for r in runs if r[2] % 2]
    if odd:
        for lev in range(max(r[2] for r in runs), min(odd) - 1, -1):
            out, i = [], 0
            while i < len(runs):
                if runs[i][2] >= lev:
                    j = i
                    while j < len(runs) and runs[j][2] >= lev:
                        j += 1
                    out.extend(runs[i:j][::-1] if lev % 2 else runs[i:j])
                    i = j
                else:
                    out.append(runs[i])
                    i += 1
            runs = out
    return [[s, ln, 'rtl' if lv % 2 else 'ltr'] for s, ln, lv in runs]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 1, 4, 2, 2], [[4, 1, 'ltr'], [5, 2, 'ltr'], [0, 4, 'rtl']]), ('regression: run sequence reversal', [3, 2, 2, 2, 2, 2, 1, 1, 2, 2, 2], [[8, 3, 'ltr'], [6, 2, 'rtl'], [0, 1, 'rtl'], [1, 5, 'ltr']]), ('regression: run sequence reversal', [4, 3, 4, 1, 1, 1, 1], [[3, 4, 'rtl'], [2, 1, 'ltr'], [1, 1, 'rtl'], [0, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [3, 3, 2, 2, 2, 2, 2], [[0, 2, 'rtl'], [2, 5, 'ltr']]), ('control layout', [0, 0, 0, 0, 0], [[0, 5, 'ltr']]), ('control layout', [1, 1, 0], [[0, 2, 'rtl'], [2, 1, 'ltr']])], [('regression: run sequence reversal', [2, 1, 1], [[1, 2, 'rtl'], [0, 1, 'ltr']]), ('regression: run sequence reversal', [1, 1, 3], [[2, 1, 'rtl'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 3, 3, 3, 2, 2, 2, 0, 2, 2, 2, 1], [[1, 3, 'rtl'], [4, 3, 'ltr'], [0, 1, 'rtl'], [7, 1, 'ltr'], [11, 1, 'rtl'], [8, 3, 'ltr']]), ('regression: run sequence reversal', [4, 4, 4, 4, 2, 1, 1, 1, 1, 1, 1, 2], [[11, 1, 'ltr'], [5, 6, 'rtl'], [0, 4, 'ltr'], [4, 1, 'ltr']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('control layout', [1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1], [[0, 4, 'rtl'], [4, 4, 'ltr'], [8, 3, 'rtl']]), ('control layout', [2, 2], [[0, 2, 'ltr']])], [('regression: run sequence reversal', [1, 1, 2, 2, 2, 2, 1, 1, 1, 1], [[6, 4, 'rtl'], [2, 4, 'ltr'], [0, 2, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 2, 2, 2, 2, 2, 1], [[8, 1, 'rtl'], [3, 5, 'ltr'], [0, 3, 'rtl']]), ('partial-repair probe', [1, 1, 1, 1, 0, 0, 0, 3, 2, 2], [[0, 4, 'rtl'], [4, 3, 'ltr'], [7, 1, 'rtl'], [8, 2, 'ltr']]), ('regression: run sequence reversal', [2, 4, 4, 1, 2, 2], [[4, 2, 'ltr'], [3, 1, 'rtl'], [0, 1, 'ltr'], [1, 2, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 0, 0, 0], [[0, 2, 'rtl'], [2, 3, 'ltr']]), ('control layout', [2, 2, 3, 3, 3], [[0, 2, 'ltr'], [2, 3, 'rtl']])], [('regression: run sequence reversal', [3, 3, 3, 1], [[3, 1, 'rtl'], [0, 3, 'rtl']]), ('regression: run sequence reversal', [1, 2, 1, 1, 1, 1, 2, 2, 1, 1], [[8, 2, 'rtl'], [6, 2, 'ltr'], [2, 4, 'rtl'], [1, 1, 'ltr'], [0, 1, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 4, 4, 3, 4, 4], [[6, 2, 'ltr'], [5, 1, 'rtl'], [3, 2, 'ltr'], [0, 3, 'rtl']]), ('regression: run sequence reversal', [2, 4, 4, 4, 4, 1, 1], [[5, 2, 'rtl'], [0, 1, 'ltr'], [1, 4, 'ltr']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [1, 1, 1], [[0, 3, 'rtl']]), ('control layout', [2, 2, 2, 2, 2], [[0, 5, 'ltr']])], [('regression: run sequence reversal', [1, 1, 1, 1, 1, 2, 2, 2, 2, 0], [[5, 4, 'ltr'], [0, 5, 'rtl'], [9, 1, 'ltr']]), ('regression: run sequence reversal', [1, 1, 1, 1, 3, 0, 0, 0, 1], [[4, 1, 'rtl'], [0, 4, 'rtl'], [5, 3, 'ltr'], [8, 1, 'rtl']]), ('regression: run sequence reversal', [1, 1, 1, 1, 1, 3, 3, 3, 2, 1, 1], [[9, 2, 'rtl'], [5, 3, 'rtl'], [8, 1, 'ltr'], [0, 5, 'rtl']]), ('regression: run sequence reversal', [0, 0, 0, 0, 2, 2, 3, 3, 3, 3, 3, 1], [[0, 4, 'ltr'], [11, 1, 'rtl'], [4, 2, 'ltr'], [6, 5, 'rtl']]), ('LTR number inside RTL', [1, 1, 2, 2, 1], [[4, 1, 'rtl'], [2, 2, 'ltr'], [0, 2, 'rtl']]), ('lowest odd is three', [2, 3, 2], [[0, 1, 'ltr'], [1, 1, 'rtl'], [2, 1, 'ltr']]), ('control layout', [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], [[0, 2, 'ltr'], [2, 1, 'rtl'], [3, 9, 'ltr']]), ('control layout', [3, 3], [[0, 2, 'rtl']])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Visual runs come out in logical order.

Input resolved levels of a line. Group maximal equal-level runs [logical start, length, level]; reorder whole runs from the highest level down to the lowest odd level by reversing sequences of runs at or above the level. Return [[start, length, "rtl"|"ltr"]] in visual order.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    text, default = x
    paras = []
    cur = ''
    i = 0
    while i < len(text):
        ch = text[i]
        if ch == '\n' and text[i + 1:i + 2] == '\r':
            cur += '\r\n'
            paras.append(cur)
            cur = ''
            i += 2
            continue
        cur += ch
        if ch in '\n\r\u2029':
            paras.append(cur)
            cur = ''
        i += 1
    if cur:
        paras.append(cur)
    out = []
    for p in paras:
        level = default
        for ch in p:
            if ch.isalpha():
                level = 1 if ch.isupper() else 0
                break
        out.append([p, level])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('regression: CRLF pairing', ['b \r\n1\n,\ncbb\n\n!\n\n', 0], [['b \r\n', 0], ['1\n', 0], [',\n', 0], ['cbb\n', 0], ['\n', 0], ['!\n', 0], ['\n', 0]]), ('regression: CRLF pairing', ['a  \r\n1', 1], [['a  \r\n', 0], ['1', 1]]), ('regression: CRLF pairing', ['!c\r\nXc..\u2029ZbZ', 0], [['!c\r\n', 0], ['Xc..\u2029', 1], ['ZbZ', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['\r,cb\r\u2029a\n.Z\r', 1], [['\r', 1], [',cb\r', 0], ['\u2029', 1], ['a\n', 0], ['.Z\r', 1]]), ('control layout', ['\nY\n', 0], [['\n', 0], ['Y\n', 1]])], [('regression: CRLF pairing', ['.\r\nb\n\r\nYc\nb\r\r\nb\nYY', 0], [['.\r\n', 0], ['b\n', 0], ['\r\n', 0], ['Yc\n', 1], ['b\r', 0], ['\r\n', 0], ['b\n', 0], ['YY', 1]]), ('regression: CRLF pairing', ['Z\n1\r\n!2X', 0], [['Z\n', 1], ['1\r\n', 0], ['!2X', 1]]), ('regression: CRLF pairing', ['aYYX1.b\r\n.', 1], [['aYYX1.b\r\n', 0], ['.', 1]]), ('regression: CRLF pairing', ['!c\n\n\r\naY', 1], [['!c\n', 0], ['\n', 1], ['\r\n', 1], ['aY', 0]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', [',bXX!Z!2b.\u2029\nc', 1], [[',bXX!Z!2b.\u2029', 0], ['\n', 1], ['c', 0]]), ('control layout', ['2\nZ\nX\nX', 1], [['2\n', 1], ['Z\n', 1], ['X\n', 1], ['X', 1]])], [('regression: CRLF pairing', ['\r\n1,X1 \r\n.b,', 1], [['\r\n', 1], ['1,X1 \r\n', 1], ['.b,', 0]]), ('regression: CRLF pairing', ['b  \r\nc2,\u202911\na,', 1], [['b  \r\n', 0], ['c2,\u2029', 0], ['11\n', 1], ['a,', 0]]), ('regression: CRLF pairing', ['!Ya\nb,\n!,\u2029\r\n1cYca', 0], [['!Ya\n', 1], ['b,\n', 0], ['!,\u2029', 0], ['\r\n', 0], ['1cYca', 0]]), ('regression: CRLF pairing', ['!c\n\n\r\naY', 1], [['!c\n', 0], ['\n', 1], ['\r\n', 1], ['aY', 0]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['\n!\r.aca1X,Yc ', 0], [['\n', 0], ['!\r', 0], ['.aca1X,Yc ', 0]]), ('control layout', ['Xac!\nZ2\u2029aYa.\r,Z!', 0], [['Xac!\n', 1], ['Z2\u2029', 1], ['aYa.\r', 0], [',Z!', 1]])], [('regression: CRLF pairing', ['a\rY,!c,2 \nc\nXb\r\nc', 1], [['a\r', 0], ['Y,!c,2 \n', 1], ['c\n', 0], ['Xb\r\n', 1], ['c', 0]]), ('regression: CRLF pairing', ['Y\n\r\nX1ZYXZ1\n', 1], [['Y\n', 1], ['\r\n', 1], ['X1ZYXZ1\n', 1]]), ('regression: CRLF pairing', ['a!Y\r\naZZ. XXY \n\n\n', 1], [['a!Y\r\n', 0], ['aZZ. XXY \n', 0], ['\n', 1], ['\n', 1]]), ('regression: CRLF pairing', ['aa\r\naa\nY.cb1b', 1], [['aa\r\n', 0], ['aa\n', 0], ['Y.cb1b', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['cZ!', 0], [['cZ!', 0]]), ('control layout', ['1 a!.\u2029\nc1X 1YZa1', 1], [['1 a!.\u2029', 0], ['\n', 1], ['c1X 1YZa1', 0]])], [('regression: CRLF pairing', ['\ra\u2029\r\n11Z\r\n2,Xc 1\n', 1], [['\r', 1], ['a\u2029', 0], ['\r\n', 1], ['11Z\r\n', 1], ['2,Xc 1\n', 1]]), ('regression: CRLF pairing', ['Ya\nb c,,,a1\r\n\r\n\n\u2029\n', 0], [['Ya\n', 1], ['b c,,,a1\r\n', 0], ['\r\n', 0], ['\n', 0], ['\u2029', 0], ['\n', 0]]), ('regression: CRLF pairing', ['\r\n.! 12\u2029\n', 1], [['\r\n', 1], ['.! 12\u2029', 1], ['\n', 1]]), ('regression: CRLF pairing', ['Y1Y2!\r\r\n c', 0], [['Y1Y2!\r', 1], ['\r\n', 0], [' c', 0]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['\n ..Y\n.,2\nY', 1], [['\n', 1], [' ..Y\n', 1], ['.,2\n', 1], ['Y', 1]]), ('control layout', ['\n\n1abb\u2029X', 0], [['\n', 0], ['\n', 0], ['1abb\u2029', 0], ['X', 1]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    text, default = x
    paras = []
    cur = ''
    i = 0
    while i < len(text):
        ch = text[i]
        if ch == '\r' and text[i + 1:i + 2] == '\r':
            cur += '\r\n'
            paras.append(cur)
            cur = ''
            i += 2
            continue
        cur += ch
        if ch in '\n\r\u2029':
            paras.append(cur)
            cur = ''
        i += 1
    if cur:
        paras.append(cur)
    out = []
    for p in paras:
        level = default
        for ch in p:
            if ch.isalpha():
                level = 1 if ch.isupper() else 0
                break
        out.append([p, level])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('regression: CRLF pairing', ['b \r\n1\n,\ncbb\n\n!\n\n', 0], [['b \r\n', 0], ['1\n', 0], [',\n', 0], ['cbb\n', 0], ['\n', 0], ['!\n', 0], ['\n', 0]]), ('regression: CRLF pairing', ['a  \r\n1', 1], [['a  \r\n', 0], ['1', 1]]), ('regression: CRLF pairing', ['!c\r\nXc..\u2029ZbZ', 0], [['!c\r\n', 0], ['Xc..\u2029', 1], ['ZbZ', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['\r,cb\r\u2029a\n.Z\r', 1], [['\r', 1], [',cb\r', 0], ['\u2029', 1], ['a\n', 0], ['.Z\r', 1]]), ('control layout', ['\nY\n', 0], [['\n', 0], ['Y\n', 1]])], [('regression: CRLF pairing', ['.\r\nb\n\r\nYc\nb\r\r\nb\nYY', 0], [['.\r\n', 0], ['b\n', 0], ['\r\n', 0], ['Yc\n', 1], ['b\r', 0], ['\r\n', 0], ['b\n', 0], ['YY', 1]]), ('regression: CRLF pairing', ['Z\n1\r\n!2X', 0], [['Z\n', 1], ['1\r\n', 0], ['!2X', 1]]), ('regression: CRLF pairing', ['aYYX1.b\r\n.', 1], [['aYYX1.b\r\n', 0], ['.', 1]]), ('regression: CRLF pairing', ['!c\n\n\r\naY', 1], [['!c\n', 0], ['\n', 1], ['\r\n', 1], ['aY', 0]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', [',bXX!Z!2b.\u2029\nc', 1], [[',bXX!Z!2b.\u2029', 0], ['\n', 1], ['c', 0]]), ('control layout', ['2\nZ\nX\nX', 1], [['2\n', 1], ['Z\n', 1], ['X\n', 1], ['X', 1]])], [('regression: CRLF pairing', ['\r\n1,X1 \r\n.b,', 1], [['\r\n', 1], ['1,X1 \r\n', 1], ['.b,', 0]]), ('regression: CRLF pairing', ['b  \r\nc2,\u202911\na,', 1], [['b  \r\n', 0], ['c2,\u2029', 0], ['11\n', 1], ['a,', 0]]), ('regression: CRLF pairing', ['!Ya\nb,\n!,\u2029\r\n1cYca', 0], [['!Ya\n', 1], ['b,\n', 0], ['!,\u2029', 0], ['\r\n', 0], ['1cYca', 0]]), ('regression: CRLF pairing', ['!c\n\n\r\naY', 1], [['!c\n', 0], ['\n', 1], ['\r\n', 1], ['aY', 0]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['\n!\r.aca1X,Yc ', 0], [['\n', 0], ['!\r', 0], ['.aca1X,Yc ', 0]]), ('control layout', ['Xac!\nZ2\u2029aYa.\r,Z!', 0], [['Xac!\n', 1], ['Z2\u2029', 1], ['aYa.\r', 0], [',Z!', 1]])], [('regression: CRLF pairing', ['a\rY,!c,2 \nc\nXb\r\nc', 1], [['a\r', 0], ['Y,!c,2 \n', 1], ['c\n', 0], ['Xb\r\n', 1], ['c', 0]]), ('regression: CRLF pairing', ['Y\n\r\nX1ZYXZ1\n', 1], [['Y\n', 1], ['\r\n', 1], ['X1ZYXZ1\n', 1]]), ('regression: CRLF pairing', ['a!Y\r\naZZ. XXY \n\n\n', 1], [['a!Y\r\n', 0], ['aZZ. XXY \n', 0], ['\n', 1], ['\n', 1]]), ('regression: CRLF pairing', ['aa\r\naa\nY.cb1b', 1], [['aa\r\n', 0], ['aa\n', 0], ['Y.cb1b', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['cZ!', 0], [['cZ!', 0]]), ('control layout', ['1 a!.\u2029\nc1X 1YZa1', 1], [['1 a!.\u2029', 0], ['\n', 1], ['c1X 1YZa1', 0]])], [('regression: CRLF pairing', ['\ra\u2029\r\n11Z\r\n2,Xc 1\n', 1], [['\r', 1], ['a\u2029', 0], ['\r\n', 1], ['11Z\r\n', 1], ['2,Xc 1\n', 1]]), ('regression: CRLF pairing', ['Ya\nb c,,,a1\r\n\r\n\n\u2029\n', 0], [['Ya\n', 1], ['b c,,,a1\r\n', 0], ['\r\n', 0], ['\n', 0], ['\u2029', 0], ['\n', 0]]), ('regression: CRLF pairing', ['\r\n.! 12\u2029\n', 1], [['\r\n', 1], ['.! 12\u2029', 1], ['\n', 1]]), ('regression: CRLF pairing', ['Y1Y2!\r\r\n c', 0], [['Y1Y2!\r', 1], ['\r\n', 0], [' c', 0]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['\n ..Y\n.,2\nY', 1], [['\n', 1], [' ..Y\n', 1], ['.,2\n', 1], ['Y', 1]]), ('control layout', ['\n\n1abb\u2029X', 0], [['\n', 0], ['\n', 0], ['1abb\u2029', 0], ['X', 1]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Windows line endings create an extra empty paragraph.

Input [text, default level]. Paragraph separators (\n, \r, U+2029; \r\n counts once) end a paragraph and stay with it. A trailing non-empty remainder is a paragraph. Each paragraph takes level 1 if its first letter is uppercase (stand-in for RTL), 0 if lowercase, else the default. Return [[paragraph, level]].

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    text, default = x
    paras = []
    cur = ''
    i = 0
    while i < len(text):
        ch = text[i]
        if ch == '\r' and text[i + 1:i + 2] == '\n':
            cur += '\r\n'
            paras.append(cur)
            cur = ''
            i += 2
            continue
        if ch in '\n\r\u2029':
            paras.append(cur)
            cur = ''
        cur += ch
        i += 1
    if cur:
        paras.append(cur)
    out = []
    for p in paras:
        level = default
        for ch in p:
            if ch.isalpha():
                level = 1 if ch.isupper() else 0
                break
        out.append([p, level])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('regression: separator ownership', [' Y,1Y\n\r\n', 1], [[' Y,1Y\n', 1], ['\r\n', 1]]), ('regression: separator ownership', ['.a1 \r2a2\n', 0], [['.a1 \r', 0], ['2a2\n', 0]]), ('regression: separator ownership', ['bX YXX\nX', 1], [['bX YXX\n', 0], ['X', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['Z,b\r\nZ1a,,Y2211', 0], [['Z,b\r\n', 1], ['Z1a,,Y2211', 1]]), ('control layout', ['Yb.', 0], [['Yb.', 1]])], [('regression: separator ownership', [' 1c!b.Y2\u2029\n\u2029\nY.b\n', 1], [[' 1c!b.Y2\u2029', 0], ['\n', 1], ['\u2029', 1], ['\n', 1], ['Y.b\n', 1]]), ('regression: separator ownership', ['\nc2Z\r\n', 0], [['\n', 0], ['c2Z\r\n', 0]]), ('regression: separator ownership', ['XbZ,.Y c\rYb', 0], [['XbZ,.Y c\r', 1], ['Yb', 1]]), ('regression: separator ownership', ['bb\n.Z. ', 1], [['bb\n', 0], ['.Z. ', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['\r\n1,X1 \r\n.b,', 1], [['\r\n', 1], ['1,X1 \r\n', 1], ['.b,', 0]]), ('control layout', ['b!X', 0], [['b!X', 0]])], [('regression: separator ownership', ['\n!, \r,1\nZ', 1], [['\n', 1], ['!, \r', 1], [',1\n', 1], ['Z', 1]]), ('regression: separator ownership', ['.,bZ\nbXY', 1], [['.,bZ\n', 0], ['bXY', 0]]), ('regression: separator ownership', ['1 \n\u2029cX\r\n1\n\n\n.c', 0], [['1 \n', 0], ['\u2029', 0], ['cX\r\n', 0], ['1\n', 0], ['\n', 0], ['\n', 0], ['.c', 0]]), ('regression: separator ownership', ['..Z\r\na\n\r\na!Ya b', 1], [['..Z\r\n', 1], ['a\n', 0], ['\r\n', 1], ['a!Ya b', 0]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['aabY!.! XZa .Yc', 0], [['aabY!.! XZa .Yc', 0]]), ('control layout', ['a! \r\nY.a1', 1], [['a! \r\n', 0], ['Y.a1', 1]])], [('regression: separator ownership', ['Ya\nb c,,,a1\r\n\r\n\n\u2029\n', 0], [['Ya\n', 1], ['b c,,,a1\r\n', 0], ['\r\n', 0], ['\n', 0], ['\u2029', 0], ['\n', 0]]), ('regression: separator ownership', ['21!b!YcZ\r\n\n.Y Y!', 1], [['21!b!YcZ\r\n', 0], ['\n', 1], ['.Y Y!', 1]]), ('regression: separator ownership', ['a\r\n\nY1X111,2Z', 1], [['a\r\n', 0], ['\n', 1], ['Y1X111,2Z', 1]]), ('regression: separator ownership', ['Z\u2029\r\n Z', 0], [['Z\u2029', 1], ['\r\n', 0], [' Z', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', [',Yb!', 0], [[',Yb!', 1]]), ('control layout', ['a  \r\n1', 1], [['a  \r\n', 0], ['1', 1]])], [('regression: separator ownership', [' ZZ\nXb', 0], [[' ZZ\n', 1], ['Xb', 1]]), ('regression: separator ownership', ['c1\u2029\nb2', 1], [['c1\u2029', 0], ['\n', 1], ['b2', 0]]), ('regression: separator ownership', ['c\rbX\r\n', 0], [['c\r', 0], ['bX\r\n', 0]]), ('regression: separator ownership', [',Y. X a,\r\n1c1,\n2', 1], [[',Y. X a,\r\n', 1], ['1c1,\n', 0], ['2', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['12.,cZX', 0], [['12.,cZX', 0]]), ('control layout', ['a,1.2Z', 1], [['a,1.2Z', 0]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

source:
"""Failure Map reference implementation. Python standard library only."""
import json

N = 1
observations = []
def solve(x):
    text, default = x
    paras = []
    cur = ''
    i = 0
    while i < len(text):
        ch = text[i]
        if ch == '\r' and text[i + 1:i + 2] == '\n':
            cur += '\r\n'
            paras.append(cur)
            cur = ''
            i += 2
            continue
        if ch in '\n\r\u2029':
            paras.append(cur)
            cur = ''
        else:
            cur += ch
        i += 1
    if cur:
        paras.append(cur)
    out = []
    for p in paras:
        level = default
        for ch in p:
            if ch.isalpha():
                level = 1 if ch.isupper() else 0
                break
        out.append([p, level])
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('regression: separator ownership', [' Y,1Y\n\r\n', 1], [[' Y,1Y\n', 1], ['\r\n', 1]]), ('regression: separator ownership', ['.a1 \r2a2\n', 0], [['.a1 \r', 0], ['2a2\n', 0]]), ('regression: separator ownership', ['bX YXX\nX', 1], [['bX YXX\n', 0], ['X', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['Z,b\r\nZ1a,,Y2211', 0], [['Z,b\r\n', 1], ['Z1a,,Y2211', 1]]), ('control layout', ['Yb.', 0], [['Yb.', 1]])], [('regression: separator ownership', [' 1c!b.Y2\u2029\n\u2029\nY.b\n', 1], [[' 1c!b.Y2\u2029', 0], ['\n', 1], ['\u2029', 1], ['\n', 1], ['Y.b\n', 1]]), ('regression: separator ownership', ['\nc2Z\r\n', 0], [['\n', 0], ['c2Z\r\n', 0]]), ('regression: separator ownership', ['XbZ,.Y c\rYb', 0], [['XbZ,.Y c\r', 1], ['Yb', 1]]), ('regression: separator ownership', ['bb\n.Z. ', 1], [['bb\n', 0], ['.Z. ', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['\r\n1,X1 \r\n.b,', 1], [['\r\n', 1], ['1,X1 \r\n', 1], ['.b,', 0]]), ('control layout', ['b!X', 0], [['b!X', 0]])], [('regression: separator ownership', ['\n!, \r,1\nZ', 1], [['\n', 1], ['!, \r', 1], [',1\n', 1], ['Z', 1]]), ('regression: separator ownership', ['.,bZ\nbXY', 1], [['.,bZ\n', 0], ['bXY', 0]]), ('regression: separator ownership', ['1 \n\u2029cX\r\n1\n\n\n.c', 0], [['1 \n', 0], ['\u2029', 0], ['cX\r\n', 0], ['1\n', 0], ['\n', 0], ['\n', 0], ['.c', 0]]), ('regression: separator ownership', ['..Z\r\na\n\r\na!Ya b', 1], [['..Z\r\n', 1], ['a\n', 0], ['\r\n', 1], ['a!Ya b', 0]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('control layout', ['aabY!.! XZa .Yc', 0], [['aabY!.! XZa .Yc', 0]]), ('control layout', ['a! \r\nY.a1', 1], [['a! \r\n', 0], ['Y.a1', 1]])], [('regression: separator ownership', ['Ya\nb c,,,a1\r\n\r\n\n\u2029\n', 0], [['Ya\n', 1], ['b c,,,a1\r\n', 0], ['\r\n', 0], ['\n', 0], ['\u2029', 0], ['\n', 0]]), ('regression: separator ownership', ['21!b!YcZ\r\n\n.Y Y!', 1], [['21!b!YcZ\r\n', 0], ['\n', 1], ['.Y Y!', 1]]), ('regression: separator ownership', ['a\r\n\nY1X111,2Z', 1], [['a\r\n', 0], ['\n', 1], ['Y1X111,2Z', 1]]), ('regression: separator ownership', ['Z\u2029\r\n Z', 0], [['Z\u2029', 1], ['\r\n', 0], [' Z', 1]]), ('separator only paragraph', ['a\n\nZ', 1], [['a\n', 0], ['\n', 1], ['Z', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', [',Yb!', 0], [[',Yb!', 1]]), ('control layout', ['a  \r\n1', 1], [['a  \r\n', 0], ['1', 1]])], [('regression: separator ownership', [' ZZ\nXb', 0], [[' ZZ\n', 1], ['Xb', 1]]), ('regression: separator ownership', ['c1\u2029\nb2', 1], [['c1\u2029', 0], ['\n', 1], ['b2', 0]]), ('regression: separator ownership', ['c\rbX\r\n', 0], [['c\r', 0], ['bX\r\n', 0]]), ('regression: separator ownership', [',Y. X a,\r\n1c1,\n2', 1], [[',Y. X a,\r\n', 1], ['1c1,\n', 0], ['2', 1]]), ('CRLF is one separator', ['ab\r\nXY', 0], [['ab\r\n', 0], ['XY', 1]]), ('digits then letter', ['12 X\n', 0], [['12 X\n', 1]]), ('control layout', ['12.,cZX', 0], [['12.,cZX', 0]]), ('control layout', ['a,1.2Z', 1], [['a,1.2Z', 0]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Paragraph separators appear at the start of the following paragraph.

Input [text, default level]. Paragraph separators (\n, \r, U+2029; \r\n counts once) end a paragraph and stay with it. A trailing non-empty remainder is a paragraph. Each paragraph takes level 1 if its first letter is uppercase (stand-in for RTL), 0 if lowercase, else the default. Return [[paragraph, level]].

split: open-access