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(0)] = 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 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 = [[('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']]), ('regression: PDI matching order', [['FSI', 'LRI', 'PDI', 'L', 'EN', 'L', 'ON', 'PDI', 'RLI'], [1, 2, 2, 2, 2, 2, 2, 1, 1], 1], [[[0, 7, 8], 'R', 'R'], [[1, 2, 3, 4, 5, 6], 'L', 'L']]), ('regression: PDI matching order', [['EN', 'PDI', 'RLI', 'RLI', 'RLI', 'PDI', 'R', 'LRI', 'PDI', 'ON', 'PDI', 'LRI', 'PDI'], [1, 1, 1, 3, 5, 5, 5, 5, 5, 5, 3, 3, 3], 1], [[[0, 1, 2], 'R', 'R'], [[3, 10, 11, 12], 'R', 'R'], [[4, 5, 6, 7, 8, 9], 'R', 'R']]), ('regression: PDI matching order', [['R', 'LRI', 'LRI', 'EN', 'EN', 'FSI', 'PDI', 'ON', 'PDI', 'ON', 'FSI', 'PDI', 'PDI'], [1, 1, 2, 4, 4, 4, 4, 4, 2, 2, 2, 2, 1], 1], [[[0, 1, 12], 'R', 'R'], [[2, 8, 9, 10, 11], 'L', 'L'], [[3, 4, 5, 6, 7], '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', [['EN', 'ON', 'EN', 'R', 'RLI', 'RLI'], [0, 0, 0, 0, 0, 1], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5], 'R', 'R']]), ('control layout', [['ON', 'EN', 'EN', 'PDI'], [1, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'R']])], [('regression: PDI matching order', [['ON', 'ON', 'EN', 'LRI', 'L', 'FSI', 'EN', 'PDI', 'FSI'], [0, 0, 0, 0, 2, 2, 4, 2, 2], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4, 5, 7, 8], 'L', 'L'], [[6], 'L', 'L']]), ('regression: PDI matching order', [['L', 'PDI', 'FSI', 'ON', 'LRI', 'L', 'PDI', 'EN', 'L', 'L'], [2, 1, 1, 2, 2, 4, 2, 2, 2, 2], 1], [[[0], 'L', 'L'], [[1, 2], 'L', 'R'], [[3, 4, 6, 7, 8, 9], 'L', 'L'], [[5], 'L', 'L']]), ('regression: PDI matching order', [['FSI', 'L', 'FSI', 'RLI', 'EN', 'ON', 'ON', 'R', 'PDI', 'L', 'PDI'], [0, 2, 2, 4, 6, 5, 5, 5, 4, 4, 2], 0], [[[0], 'L', 'L'], [[1, 2, 10], 'L', 'L'], [[3, 8, 9], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R']]), ('partial-repair probe', [['R', 'ON', 'ON', 'RLI', 'ON', 'ON', 'R', 'PDI', 'PDI'], [1, 1, 1, 1, 3, 3, 3, 1, 1], 1], [[[0, 1, 2, 3, 7, 8], 'R', 'R'], [[4, 5, 6], '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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['RLI', 'EN', 'ON'], [1, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'R']]), ('control layout', [['L', 'EN', 'R', 'PDI', 'EN', 'EN', 'ON', 'L', 'EN', 'RLI', 'L', 'R', 'LRI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'L', 'L'], [[10, 11, 12], 'R', 'R']])], [('regression: PDI matching order', [['PDI', 'LRI', 'EN', 'LRI', 'R', 'EN', 'ON', 'R', 'PDI', 'PDI'], [0, 0, 2, 2, 5, 4, 4, 4, 2, 0], 0], [[[0, 1, 9], 'L', 'L'], [[2, 3, 8], 'L', 'L'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: PDI matching order', [['RLI', 'LRI', 'L', 'R', 'FSI', 'L', 'EN', 'PDI', 'L', 'FSI', 'ON'], [0, 1, 2, 2, 2, 4, 4, 2, 2, 2, 4], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2, 3, 4, 7, 8, 9], 'L', 'L'], [[5, 6], 'L', 'L'], [[10], 'L', 'L']]), ('partial-repair probe', [['ON', 'ON', 'L', 'LRI', 'EN', 'PDI', 'EN', 'R', 'R', 'PDI', 'PDI', 'L'], [1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'L', 'L'], [[6], 'L', 'L'], [[7, 8, 9, 10, 11], 'L', 'R']]), ('regression: PDI matching order', [['LRI', 'R', 'ON', 'RLI', 'R', 'RLI', 'R', 'PDI', 'PDI', 'ON', 'LRI'], [0, 2, 2, 2, 3, 3, 5, 3, 2, 2, 2], 0], [[[0], 'L', 'L'], [[1, 2, 3, 8, 9, 10], 'L', 'L'], [[4, 5, 7], 'R', 'R'], [[6], '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']]), ('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', [['L', 'R', 'LRI', 'PDI'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('control layout', [['ON', 'EN', 'EN', 'PDI'], [1, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'R']])], [('regression: PDI matching order', [['RLI', 'FSI', 'FSI', 'R', 'L', 'PDI', 'ON'], [0, 1, 2, 4, 4, 2, 2], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L']]), ('regression: PDI matching order', [['FSI', 'FSI', 'PDI', 'PDI', 'L', 'PDI'], [1, 2, 2, 1, 2, 1], 1], [[[0, 3], 'R', 'L'], [[1, 2], 'L', 'L'], [[4], 'L', 'L'], [[5], 'L', 'R']]), ('partial-repair probe', [['ON', 'EN', 'RLI', 'ON', 'R', 'PDI', 'R', 'PDI', 'ON', 'L', 'PDI', 'L', 'PDI'], [1, 1, 1, 3, 3, 1, 1, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 2, 5, 6, 7, 8], 'R', 'L'], [[3, 4], 'R', 'R'], [[9], 'L', 'L'], [[10, 11, 12], 'L', 'R']]), ('regression: PDI matching order', [['ON', 'PDI', 'EN', 'FSI', 'L', 'R', 'LRI', 'L', 'PDI', 'PDI', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1, 2, 3, 2, 4, 2, 1, 1, 2, 1], 1], [[[0, 1, 2, 3, 9, 10], 'R', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R'], [[6, 8], 'R', 'L'], [[7], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', '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']]), ('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', [['L', 'ON', 'R', 'R', 'L', 'LRI', 'PDI', 'LRI', 'RLI', 'ON', 'R', 'R'], [0, 0, 0, 1, 0, 0, 0, 0, 2, 3, 3, 3], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4, 5, 6, 7], 'R', 'L'], [[8], 'L', 'L'], [[9, 10, 11], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'PDI', 'EN'], [1, 2, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4], 'L', 'R']])], [('regression: PDI matching order', [['EN', 'RLI', 'L', 'ON', 'FSI', 'PDI', 'EN', 'L', 'PDI', 'LRI', 'ON', 'L'], [2, 1, 4, 3, 3, 3, 4, 3, 1, 1, 2, 2], 1], [[[0], 'L', 'L'], [[1, 8, 9], 'L', 'R'], [[2], 'L', 'L'], [[3, 4, 5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'R'], [[10, 11], 'L', 'L']]), ('regression: PDI matching order', [['RLI', 'RLI', 'RLI', 'L', 'PDI', 'FSI', 'PDI', 'L', 'EN'], [1, 3, 5, 7, 5, 5, 5, 5, 5], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2, 4, 5, 6, 7, 8], 'R', 'R'], [[3], 'R', 'R']]), ('partial-repair probe', [['LRI', 'ON', 'PDI', 'PDI', 'L', 'L', 'ON', 'EN', 'EN', 'R', 'LRI'], [0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0], 0], [[[0, 2, 3, 4, 5, 6, 7, 8], 'L', 'R'], [[1], 'L', 'L'], [[9], 'R', 'R'], [[10], 'R', 'L']]), ('partial-repair probe', [['EN', 'RLI', 'R', 'PDI', 'PDI', 'LRI', 'LRI', 'R', 'ON', 'FSI'], [1, 1, 3, 1, 1, 1, 2, 5, 4, 4], 1], [[[0, 1, 3, 4, 5], 'R', 'R'], [[2], 'R', 'R'], [[6], 'L', 'L'], [[7], 'R', 'R'], [[8, 9], 'R', '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', [['L', 'EN', 'ON'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']]), ('control layout', [['L', 'PDI', 'R', 'ON', 'ON', 'ON', 'ON'], [2, 1, 1, 1, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3, 4, 5, 6], 'L', 'R']])]]
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[-1]] = 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 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 = [[('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']]), ('regression: PDI matching order', [['FSI', 'LRI', 'PDI', 'L', 'EN', 'L', 'ON', 'PDI', 'RLI'], [1, 2, 2, 2, 2, 2, 2, 1, 1], 1], [[[0, 7, 8], 'R', 'R'], [[1, 2, 3, 4, 5, 6], 'L', 'L']]), ('regression: PDI matching order', [['EN', 'PDI', 'RLI', 'RLI', 'RLI', 'PDI', 'R', 'LRI', 'PDI', 'ON', 'PDI', 'LRI', 'PDI'], [1, 1, 1, 3, 5, 5, 5, 5, 5, 5, 3, 3, 3], 1], [[[0, 1, 2], 'R', 'R'], [[3, 10, 11, 12], 'R', 'R'], [[4, 5, 6, 7, 8, 9], 'R', 'R']]), ('regression: PDI matching order', [['R', 'LRI', 'LRI', 'EN', 'EN', 'FSI', 'PDI', 'ON', 'PDI', 'ON', 'FSI', 'PDI', 'PDI'], [1, 1, 2, 4, 4, 4, 4, 4, 2, 2, 2, 2, 1], 1], [[[0, 1, 12], 'R', 'R'], [[2, 8, 9, 10, 11], 'L', 'L'], [[3, 4, 5, 6, 7], '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', [['EN', 'ON', 'EN', 'R', 'RLI', 'RLI'], [0, 0, 0, 0, 0, 1], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5], 'R', 'R']]), ('control layout', [['ON', 'EN', 'EN', 'PDI'], [1, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'R']])], [('regression: PDI matching order', [['ON', 'ON', 'EN', 'LRI', 'L', 'FSI', 'EN', 'PDI', 'FSI'], [0, 0, 0, 0, 2, 2, 4, 2, 2], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4, 5, 7, 8], 'L', 'L'], [[6], 'L', 'L']]), ('regression: PDI matching order', [['L', 'PDI', 'FSI', 'ON', 'LRI', 'L', 'PDI', 'EN', 'L', 'L'], [2, 1, 1, 2, 2, 4, 2, 2, 2, 2], 1], [[[0], 'L', 'L'], [[1, 2], 'L', 'R'], [[3, 4, 6, 7, 8, 9], 'L', 'L'], [[5], 'L', 'L']]), ('regression: PDI matching order', [['FSI', 'L', 'FSI', 'RLI', 'EN', 'ON', 'ON', 'R', 'PDI', 'L', 'PDI'], [0, 2, 2, 4, 6, 5, 5, 5, 4, 4, 2], 0], [[[0], 'L', 'L'], [[1, 2, 10], 'L', 'L'], [[3, 8, 9], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R']]), ('partial-repair probe', [['R', 'ON', 'ON', 'RLI', 'ON', 'ON', 'R', 'PDI', 'PDI'], [1, 1, 1, 1, 3, 3, 3, 1, 1], 1], [[[0, 1, 2, 3, 7, 8], 'R', 'R'], [[4, 5, 6], '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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['RLI', 'EN', 'ON'], [1, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'R']]), ('control layout', [['L', 'EN', 'R', 'PDI', 'EN', 'EN', 'ON', 'L', 'EN', 'RLI', 'L', 'R', 'LRI'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 'L', 'L'], [[10, 11, 12], 'R', 'R']])], [('regression: PDI matching order', [['PDI', 'LRI', 'EN', 'LRI', 'R', 'EN', 'ON', 'R', 'PDI', 'PDI'], [0, 0, 2, 2, 5, 4, 4, 4, 2, 0], 0], [[[0, 1, 9], 'L', 'L'], [[2, 3, 8], 'L', 'L'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: PDI matching order', [['RLI', 'LRI', 'L', 'R', 'FSI', 'L', 'EN', 'PDI', 'L', 'FSI', 'ON'], [0, 1, 2, 2, 2, 4, 4, 2, 2, 2, 4], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2, 3, 4, 7, 8, 9], 'L', 'L'], [[5, 6], 'L', 'L'], [[10], 'L', 'L']]), ('partial-repair probe', [['ON', 'ON', 'L', 'LRI', 'EN', 'PDI', 'EN', 'R', 'R', 'PDI', 'PDI', 'L'], [1, 1, 2, 1, 2, 1, 2, 1, 1, 1, 1, 1], 1], [[[0, 1], 'R', 'L'], [[2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'L', 'L'], [[6], 'L', 'L'], [[7, 8, 9, 10, 11], 'L', 'R']]), ('regression: PDI matching order', [['LRI', 'R', 'ON', 'RLI', 'R', 'RLI', 'R', 'PDI', 'PDI', 'ON', 'LRI'], [0, 2, 2, 2, 3, 3, 5, 3, 2, 2, 2], 0], [[[0], 'L', 'L'], [[1, 2, 3, 8, 9, 10], 'L', 'L'], [[4, 5, 7], 'R', 'R'], [[6], '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']]), ('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', [['L', 'R', 'LRI', 'PDI'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('control layout', [['ON', 'EN', 'EN', 'PDI'], [1, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3], 'L', 'R']])], [('regression: PDI matching order', [['RLI', 'FSI', 'FSI', 'R', 'L', 'PDI', 'ON'], [0, 1, 2, 4, 4, 2, 2], 0], [[[0], 'L', 'L'], [[1], 'R', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L']]), ('regression: PDI matching order', [['FSI', 'FSI', 'PDI', 'PDI', 'L', 'PDI'], [1, 2, 2, 1, 2, 1], 1], [[[0, 3], 'R', 'L'], [[1, 2], 'L', 'L'], [[4], 'L', 'L'], [[5], 'L', 'R']]), ('partial-repair probe', [['ON', 'EN', 'RLI', 'ON', 'R', 'PDI', 'R', 'PDI', 'ON', 'L', 'PDI', 'L', 'PDI'], [1, 1, 1, 3, 3, 1, 1, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 2, 5, 6, 7, 8], 'R', 'L'], [[3, 4], 'R', 'R'], [[9], 'L', 'L'], [[10, 11, 12], 'L', 'R']]), ('regression: PDI matching order', [['ON', 'PDI', 'EN', 'FSI', 'L', 'R', 'LRI', 'L', 'PDI', 'PDI', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1, 2, 3, 2, 4, 2, 1, 1, 2, 1], 1], [[[0, 1, 2, 3, 9, 10], 'R', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R'], [[6, 8], 'R', 'L'], [[7], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', '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']]), ('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', [['L', 'ON', 'R', 'R', 'L', 'LRI', 'PDI', 'LRI', 'RLI', 'ON', 'R', 'R'], [0, 0, 0, 1, 0, 0, 0, 0, 2, 3, 3, 3], 0], [[[0, 1, 2], 'L', 'R'], [[3], 'R', 'R'], [[4, 5, 6, 7], 'R', 'L'], [[8], 'L', 'L'], [[9, 10, 11], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'PDI', 'EN'], [1, 2, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3, 4], 'L', 'R']])], [('regression: PDI matching order', [['EN', 'RLI', 'L', 'ON', 'FSI', 'PDI', 'EN', 'L', 'PDI', 'LRI', 'ON', 'L'], [2, 1, 4, 3, 3, 3, 4, 3, 1, 1, 2, 2], 1], [[[0], 'L', 'L'], [[1, 8, 9], 'L', 'R'], [[2], 'L', 'L'], [[3, 4, 5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'R'], [[10, 11], 'L', 'L']]), ('regression: PDI matching order', [['RLI', 'RLI', 'RLI', 'L', 'PDI', 'FSI', 'PDI', 'L', 'EN'], [1, 3, 5, 7, 5, 5, 5, 5, 5], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2, 4, 5, 6, 7, 8], 'R', 'R'], [[3], 'R', 'R']]), ('partial-repair probe', [['LRI', 'ON', 'PDI', 'PDI', 'L', 'L', 'ON', 'EN', 'EN', 'R', 'LRI'], [0, 2, 0, 0, 0, 0, 0, 0, 0, 1, 0], 0], [[[0, 2, 3, 4, 5, 6, 7, 8], 'L', 'R'], [[1], 'L', 'L'], [[9], 'R', 'R'], [[10], 'R', 'L']]), ('partial-repair probe', [['EN', 'RLI', 'R', 'PDI', 'PDI', 'LRI', 'LRI', 'R', 'ON', 'FSI'], [1, 1, 3, 1, 1, 1, 2, 5, 4, 4], 1], [[[0, 1, 3, 4, 5], 'R', 'R'], [[2], 'R', 'R'], [[6], 'L', 'L'], [[7], 'R', 'R'], [[8, 9], 'R', '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', [['L', 'EN', 'ON'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']]), ('control layout', [['L', 'PDI', 'R', 'ON', 'ON', 'ON', 'ON'], [2, 1, 1, 1, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3, 4, 5, 6], 'L', 'R']])]]
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:
Nested isolates join the wrong outer runs.

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') 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 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: first-strong isolate chaining', [['EN', 'LRI', 'L', 'RLI', 'PDI', 'ON', 'FSI', 'R', 'R', 'R', 'ON', 'PDI', 'R', 'LRI'], [0, 0, 2, 2, 2, 2, 2, 4, 5, 5, 4, 2, 2, 2], 0], [[[0, 1], 'L', 'L'], [[2, 3, 4, 5, 6, 11, 12, 13], 'L', 'L'], [[7], 'L', 'R'], [[8, 9], 'R', 'R'], [[10], 'R', 'L']]), ('regression: first-strong isolate chaining', [['EN', 'L', 'L', 'FSI', 'R', 'PDI', 'L', 'EN', 'ON', 'PDI', 'L', 'L', 'EN', 'ON'], [1, 2, 2, 1, 3, 1, 2, 2, 1, 1, 1, 1, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'R', 'R'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('regression: first-strong isolate chaining', [['EN', 'EN', 'PDI', 'R', 'FSI', 'L', 'LRI', 'L', 'PDI', 'FSI', 'FSI', 'PDI', 'PDI'], [0, 0, 0, 0, 0, 2, 2, 4, 2, 2, 4, 4, 2], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5, 6, 8, 9, 12], 'L', 'L'], [[7], 'L', 'L'], [[10, 11], 'L', 'L']]), ('partial-repair probe', [['L', 'FSI', 'LRI', 'ON', 'R', 'PDI', 'LRI', 'ON'], [2, 1, 2, 4, 4, 2, 2, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L'], [[7], 'L', 'L']]), ('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', [['R', 'EN', 'L', 'L', 'R', 'R', 'PDI', 'EN', 'PDI', 'L', 'PDI', 'ON', 'ON'], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'R', 'L']]), ('control layout', [['EN', 'R', 'RLI'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']])], [('regression: first-strong isolate chaining', [['FSI', 'R', 'EN', 'R', 'PDI', 'R', 'EN', 'R', 'R', 'EN', 'RLI', 'L', 'EN'], [1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 4, 4], 1], [[[0, 4, 5, 6, 7, 8], 'R', 'L'], [[1, 2, 3], 'L', 'L'], [[9], 'L', 'L'], [[10], 'L', 'R'], [[11, 12], 'L', 'L']]), ('regression: first-strong isolate chaining', [['L', 'FSI', 'EN', 'FSI', 'PDI', 'PDI', 'ON', 'ON', 'RLI', 'EN', 'RLI', 'L'], [0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 1, 4], 0], [[[0, 1, 5, 6, 7, 8], 'L', 'L'], [[2, 3, 4], 'L', 'L'], [[9], 'L', 'L'], [[10], 'L', 'R'], [[11], 'L', 'L']]), ('regression: first-strong isolate chaining', [['ON', 'PDI', 'EN', 'FSI', 'L', 'R', 'LRI', 'L', 'PDI', 'PDI', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1, 2, 3, 2, 4, 2, 1, 1, 2, 1], 1], [[[0, 1, 2, 3, 9, 10], 'R', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R'], [[6, 8], 'R', 'L'], [[7], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R']]), ('partial-repair probe', [['LRI', 'L', 'EN', 'R', 'PDI', 'EN'], [1, 2, 2, 2, 1, 2], 1], [[[0, 4], 'R', 'L'], [[1, 2, 3], 'L', 'L'], [[5], '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', [['L', 'L', 'R', 'PDI', 'PDI', 'L', 'ON', 'L', 'PDI'], [0, 0, 1, 0, 0, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5, 6, 7, 8], 'R', 'L']]), ('control layout', [['L', 'ON', 'R'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])], [('regression: first-strong isolate chaining', [['L', 'FSI', 'EN', 'PDI', 'EN', 'EN', 'ON', 'EN', 'PDI', 'L', 'FSI', 'FSI', 'ON', 'ON'], [2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 4, 4], 1], [[[0], 'L', 'L'], [[1, 3, 4], 'L', 'L'], [[2], 'L', 'L'], [[5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'L'], [[8, 9, 10], 'L', 'R'], [[11], 'L', 'L'], [[12, 13], 'L', 'L']]), ('regression: first-strong isolate chaining', [['FSI', 'L', 'FSI', 'RLI', 'EN', 'ON', 'ON', 'R', 'PDI', 'L', 'PDI'], [0, 2, 2, 4, 6, 5, 5, 5, 4, 4, 2], 0], [[[0], 'L', 'L'], [[1, 2, 10], 'L', 'L'], [[3, 8, 9], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R']]), ('partial-repair probe', [['FSI', 'RLI', 'EN', 'R', 'LRI', 'EN', 'ON', 'R', 'L', 'L', 'PDI', 'L', 'RLI'], [1, 2, 4, 3, 3, 4, 4, 4, 4, 4, 3, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'L'], [[3, 4, 10], 'L', 'L'], [[5, 6, 7, 8, 9], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R']]), ('partial-repair probe', [['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']]), ('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', 'L', 'EN', 'PDI', 'R', 'EN', 'LRI', 'PDI', 'ON', 'ON', 'FSI', 'ON'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'L', 'L'], [[11], 'L', 'L']]), ('control layout', [['RLI', 'EN', 'EN', 'L', 'ON'], [1, 3, 3, 4, 3], 1], [[[0], 'R', 'R'], [[1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4], 'L', 'R']])], [('regression: first-strong isolate chaining', [['EN', 'FSI', 'ON', 'ON', 'PDI'], [2, 1, 2, 2, 1], 1], [[[0], 'L', 'L'], [[1, 4], 'L', 'R'], [[2, 3], 'L', 'L']]), ('regression: first-strong isolate chaining', [['PDI', 'RLI', 'FSI', 'ON', 'PDI', 'EN', 'PDI', 'R', 'EN', 'L', 'L', 'R', 'L', 'PDI'], [1, 1, 3, 4, 3, 4, 1, 1, 1, 1, 2, 1, 2, 1], 1], [[[0, 1, 6, 7, 8, 9], 'R', 'L'], [[2, 4], 'R', 'L'], [[3], 'L', 'L'], [[5], 'L', 'L'], [[10], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('partial-repair probe', [['RLI', 'ON', 'LRI', 'L', 'L', 'R', 'ON', 'L', 'EN', 'PDI', 'ON'], [0, 1, 1, 2, 2, 3, 2, 2, 2, 1, 1], 0], [[[0], 'L', 'L'], [[1, 2, 9, 10], 'R', 'R'], [[3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7, 8], 'R', 'L']]), ('partial-repair probe', [['PDI', 'R', 'R', 'ON', 'L', 'L', 'LRI', 'R', 'L', 'PDI', 'PDI', 'EN', 'PDI', 'ON'], [0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13], 'L', 'L'], [[7], 'R', 'R'], [[8], 'R', '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', [['ON', 'PDI', 'RLI', 'L'], [0, 0, 0, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'L', 'L']])], [('regression: first-strong isolate chaining', [['EN', 'L', 'L', 'FSI', 'R', 'PDI', 'L', 'EN', 'ON', 'PDI', 'L', 'L', 'EN', 'ON'], [1, 2, 2, 1, 3, 1, 2, 2, 1, 1, 1, 1, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'R', 'R'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('regression: first-strong isolate chaining', [['PDI', 'R', 'PDI', 'R', 'EN', 'FSI', 'L', 'ON', 'EN', 'PDI'], [1, 1, 1, 1, 1, 1, 2, 2, 2, 1], 1], [[[0, 1, 2, 3, 4, 5, 9], 'R', 'R'], [[6, 7, 8], 'L', 'L']]), ('regression: first-strong isolate chaining', [['LRI', 'ON', 'FSI', 'R', 'RLI', 'PDI', 'RLI', 'ON', 'PDI', 'ON', 'EN', 'PDI', 'PDI'], [1, 2, 2, 5, 4, 4, 4, 5, 4, 4, 4, 2, 1], 1], [[[0, 12], 'R', 'R'], [[1, 2, 11], 'L', 'L'], [[3], 'R', 'R'], [[4, 5, 6, 8, 9, 10], 'R', 'L'], [[7], 'R', 'R']]), ('partial-repair probe', [['PDI', 'LRI', 'EN', 'LRI', 'R', 'EN', 'ON', 'R', 'PDI', 'PDI'], [0, 0, 2, 2, 5, 4, 4, 4, 2, 0], 0], [[[0, 1, 9], 'L', 'L'], [[2, 3, 8], 'L', 'L'], [[4], 'R', 'R'], [[5, 6, 7], 'R', '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', [['L', 'EN', 'PDI', 'FSI', 'L', 'R'], [0, 0, 0, 0, 2, 3], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R']]), ('control layout', [['ON', 'EN', 'ON', 'EN', 'EN', 'LRI', 'RLI', 'EN', 'EN', 'EN', 'R', 'R', 'PDI'], [1, 1, 1, 1, 2, 1, 2, 3, 4, 3, 3, 3, 2], 1], [[[0, 1, 2, 3], 'R', 'L'], [[4], 'L', 'L'], [[5], 'L', 'R'], [[6, 12], 'L', 'L'], [[7], 'R', 'L'], [[8], 'L', 'L'], [[9, 10, 11], 'L', 'R']])]]
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 ('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 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: first-strong isolate chaining', [['EN', 'LRI', 'L', 'RLI', 'PDI', 'ON', 'FSI', 'R', 'R', 'R', 'ON', 'PDI', 'R', 'LRI'], [0, 0, 2, 2, 2, 2, 2, 4, 5, 5, 4, 2, 2, 2], 0], [[[0, 1], 'L', 'L'], [[2, 3, 4, 5, 6, 11, 12, 13], 'L', 'L'], [[7], 'L', 'R'], [[8, 9], 'R', 'R'], [[10], 'R', 'L']]), ('regression: first-strong isolate chaining', [['EN', 'L', 'L', 'FSI', 'R', 'PDI', 'L', 'EN', 'ON', 'PDI', 'L', 'L', 'EN', 'ON'], [1, 2, 2, 1, 3, 1, 2, 2, 1, 1, 1, 1, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'R', 'R'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('regression: first-strong isolate chaining', [['EN', 'EN', 'PDI', 'R', 'FSI', 'L', 'LRI', 'L', 'PDI', 'FSI', 'FSI', 'PDI', 'PDI'], [0, 0, 0, 0, 0, 2, 2, 4, 2, 2, 4, 4, 2], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5, 6, 8, 9, 12], 'L', 'L'], [[7], 'L', 'L'], [[10, 11], 'L', 'L']]), ('partial-repair probe', [['L', 'FSI', 'LRI', 'ON', 'R', 'PDI', 'LRI', 'ON'], [2, 1, 2, 4, 4, 2, 2, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L'], [[7], 'L', 'L']]), ('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', [['R', 'EN', 'L', 'L', 'R', 'R', 'PDI', 'EN', 'PDI', 'L', 'PDI', 'ON', 'ON'], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'R', 'L']]), ('control layout', [['EN', 'R', 'RLI'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']])], [('regression: first-strong isolate chaining', [['FSI', 'R', 'EN', 'R', 'PDI', 'R', 'EN', 'R', 'R', 'EN', 'RLI', 'L', 'EN'], [1, 2, 2, 2, 1, 1, 1, 1, 1, 2, 1, 4, 4], 1], [[[0, 4, 5, 6, 7, 8], 'R', 'L'], [[1, 2, 3], 'L', 'L'], [[9], 'L', 'L'], [[10], 'L', 'R'], [[11, 12], 'L', 'L']]), ('regression: first-strong isolate chaining', [['L', 'FSI', 'EN', 'FSI', 'PDI', 'PDI', 'ON', 'ON', 'RLI', 'EN', 'RLI', 'L'], [0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 1, 4], 0], [[[0, 1, 5, 6, 7, 8], 'L', 'L'], [[2, 3, 4], 'L', 'L'], [[9], 'L', 'L'], [[10], 'L', 'R'], [[11], 'L', 'L']]), ('regression: first-strong isolate chaining', [['ON', 'PDI', 'EN', 'FSI', 'L', 'R', 'LRI', 'L', 'PDI', 'PDI', 'PDI', 'EN', 'PDI'], [1, 1, 1, 1, 2, 3, 2, 4, 2, 1, 1, 2, 1], 1], [[[0, 1, 2, 3, 9, 10], 'R', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R'], [[6, 8], 'R', 'L'], [[7], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R']]), ('partial-repair probe', [['LRI', 'L', 'EN', 'R', 'PDI', 'EN'], [1, 2, 2, 2, 1, 2], 1], [[[0, 4], 'R', 'L'], [[1, 2, 3], 'L', 'L'], [[5], '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', [['L', 'L', 'R', 'PDI', 'PDI', 'L', 'ON', 'L', 'PDI'], [0, 0, 1, 0, 0, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5, 6, 7, 8], 'R', 'L']]), ('control layout', [['L', 'ON', 'R'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])], [('regression: first-strong isolate chaining', [['L', 'FSI', 'EN', 'PDI', 'EN', 'EN', 'ON', 'EN', 'PDI', 'L', 'FSI', 'FSI', 'ON', 'ON'], [2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 4, 4], 1], [[[0], 'L', 'L'], [[1, 3, 4], 'L', 'L'], [[2], 'L', 'L'], [[5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'L'], [[8, 9, 10], 'L', 'R'], [[11], 'L', 'L'], [[12, 13], 'L', 'L']]), ('regression: first-strong isolate chaining', [['FSI', 'L', 'FSI', 'RLI', 'EN', 'ON', 'ON', 'R', 'PDI', 'L', 'PDI'], [0, 2, 2, 4, 6, 5, 5, 5, 4, 4, 2], 0], [[[0], 'L', 'L'], [[1, 2, 10], 'L', 'L'], [[3, 8, 9], 'L', 'L'], [[4], 'L', 'L'], [[5, 6, 7], 'L', 'R']]), ('partial-repair probe', [['FSI', 'RLI', 'EN', 'R', 'LRI', 'EN', 'ON', 'R', 'L', 'L', 'PDI', 'L', 'RLI'], [1, 2, 4, 3, 3, 4, 4, 4, 4, 4, 3, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'L'], [[3, 4, 10], 'L', 'L'], [[5, 6, 7, 8, 9], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R']]), ('partial-repair probe', [['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']]), ('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', 'L', 'EN', 'PDI', 'R', 'EN', 'LRI', 'PDI', 'ON', 'ON', 'FSI', 'ON'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 'L', 'L'], [[11], 'L', 'L']]), ('control layout', [['RLI', 'EN', 'EN', 'L', 'ON'], [1, 3, 3, 4, 3], 1], [[[0], 'R', 'R'], [[1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4], 'L', 'R']])], [('regression: first-strong isolate chaining', [['EN', 'FSI', 'ON', 'ON', 'PDI'], [2, 1, 2, 2, 1], 1], [[[0], 'L', 'L'], [[1, 4], 'L', 'R'], [[2, 3], 'L', 'L']]), ('regression: first-strong isolate chaining', [['PDI', 'RLI', 'FSI', 'ON', 'PDI', 'EN', 'PDI', 'R', 'EN', 'L', 'L', 'R', 'L', 'PDI'], [1, 1, 3, 4, 3, 4, 1, 1, 1, 1, 2, 1, 2, 1], 1], [[[0, 1, 6, 7, 8, 9], 'R', 'L'], [[2, 4], 'R', 'L'], [[3], 'L', 'L'], [[5], 'L', 'L'], [[10], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('partial-repair probe', [['RLI', 'ON', 'LRI', 'L', 'L', 'R', 'ON', 'L', 'EN', 'PDI', 'ON'], [0, 1, 1, 2, 2, 3, 2, 2, 2, 1, 1], 0], [[[0], 'L', 'L'], [[1, 2, 9, 10], 'R', 'R'], [[3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7, 8], 'R', 'L']]), ('partial-repair probe', [['PDI', 'R', 'R', 'ON', 'L', 'L', 'LRI', 'R', 'L', 'PDI', 'PDI', 'EN', 'PDI', 'ON'], [0, 0, 0, 0, 0, 0, 0, 3, 2, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 9, 10, 11, 12, 13], 'L', 'L'], [[7], 'R', 'R'], [[8], 'R', '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', [['ON', 'PDI', 'RLI', 'L'], [0, 0, 0, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'L', 'L']])], [('regression: first-strong isolate chaining', [['EN', 'L', 'L', 'FSI', 'R', 'PDI', 'L', 'EN', 'ON', 'PDI', 'L', 'L', 'EN', 'ON'], [1, 2, 2, 1, 3, 1, 2, 2, 1, 1, 1, 1, 2, 1], 1], [[[0], 'R', 'L'], [[1, 2], 'L', 'L'], [[3, 5], 'L', 'L'], [[4], 'R', 'R'], [[6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], 'L', 'L'], [[13], 'L', 'R']]), ('regression: first-strong isolate chaining', [['PDI', 'R', 'PDI', 'R', 'EN', 'FSI', 'L', 'ON', 'EN', 'PDI'], [1, 1, 1, 1, 1, 1, 2, 2, 2, 1], 1], [[[0, 1, 2, 3, 4, 5, 9], 'R', 'R'], [[6, 7, 8], 'L', 'L']]), ('regression: first-strong isolate chaining', [['LRI', 'ON', 'FSI', 'R', 'RLI', 'PDI', 'RLI', 'ON', 'PDI', 'ON', 'EN', 'PDI', 'PDI'], [1, 2, 2, 5, 4, 4, 4, 5, 4, 4, 4, 2, 1], 1], [[[0, 12], 'R', 'R'], [[1, 2, 11], 'L', 'L'], [[3], 'R', 'R'], [[4, 5, 6, 8, 9, 10], 'R', 'L'], [[7], 'R', 'R']]), ('partial-repair probe', [['PDI', 'LRI', 'EN', 'LRI', 'R', 'EN', 'ON', 'R', 'PDI', 'PDI'], [0, 0, 2, 2, 5, 4, 4, 4, 2, 0], 0], [[[0, 1, 9], 'L', 'L'], [[2, 3, 8], 'L', 'L'], [[4], 'R', 'R'], [[5, 6, 7], 'R', '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', [['L', 'EN', 'PDI', 'FSI', 'L', 'R'], [0, 0, 0, 0, 2, 3], 0], [[[0, 1, 2, 3], 'L', 'L'], [[4], 'L', 'R'], [[5], 'R', 'R']]), ('control layout', [['ON', 'EN', 'ON', 'EN', 'EN', 'LRI', 'RLI', 'EN', 'EN', 'EN', 'R', 'R', 'PDI'], [1, 1, 1, 1, 2, 1, 2, 3, 4, 3, 3, 3, 2], 1], [[[0, 1, 2, 3], 'R', 'L'], [[4], 'L', 'L'], [[5], 'L', 'R'], [[6, 12], 'L', 'L'], [[7], 'R', 'L'], [[8], 'L', 'L'], [[9, 10, 11], 'L', 'R']])]]
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:
Text around an FSI isolate is split into separate sequences.

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)
        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 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 = [[('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: chained run bookkeeping', [['L', 'FSI', 'LRI', 'ON', 'R', 'PDI', 'LRI', 'ON'], [2, 1, 2, 4, 4, 2, 2, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L'], [[7], 'L', 'L']]), ('regression: chained run bookkeeping', [['EN', 'PDI', 'FSI', 'LRI', 'L', 'L', 'LRI', 'ON', 'PDI', 'R', 'L', 'EN', 'L', 'R'], [1, 1, 1, 2, 4, 4, 4, 6, 4, 4, 4, 4, 4, 5], 1], [[[0, 1, 2], 'R', 'R'], [[3], 'L', 'L'], [[4, 5, 6, 8, 9, 10, 11, 12], 'L', 'R'], [[7], 'L', 'L'], [[13], '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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['R', 'R', 'PDI', 'PDI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']]), ('control layout', [['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']]), ('control layout', [['L', 'PDI', 'R', 'L', 'PDI', 'PDI', 'R', 'RLI'], [0, 0, 1, 0, 0, 0, 1, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5], 'R', 'R'], [[6], 'R', 'R'], [[7], 'R', 'L']])], [('regression: chained run bookkeeping', [['EN', 'FSI', 'R', 'R', 'R', 'ON', 'PDI', 'ON'], [0, 0, 3, 2, 3, 2, 0, 0], 0], [[[0, 1, 6, 7], 'L', 'L'], [[2], 'R', 'R'], [[3], 'R', 'R'], [[4], 'R', 'R'], [[5], 'R', 'L']]), ('regression: chained run bookkeeping', [['L', 'L', 'PDI', 'ON', 'L', 'LRI', 'EN', 'EN', 'R', 'R', 'R', 'PDI', 'RLI', 'FSI'], [1, 2, 1, 1, 2, 1, 2, 2, 2, 3, 2, 1, 1, 3], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'L'], [[4], 'L', 'L'], [[5, 11, 12], 'L', 'R'], [[6, 7, 8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L'], [[13], 'R', 'R']]), ('regression: chained run bookkeeping', [['RLI', 'FSI', 'RLI', 'EN', 'PDI', 'PDI', 'FSI', 'R', 'L'], [1, 3, 4, 5, 4, 3, 3, 5, 4], 1], [[[0], 'R', 'R'], [[1, 5, 6], 'R', 'R'], [[2, 4], 'L', 'L'], [[3], 'R', 'R'], [[7], 'R', 'R'], [[8], 'R', 'L']]), ('regression: chained run bookkeeping', [['PDI', 'RLI', 'L', 'L', 'EN', 'L', 'PDI', 'PDI', 'L', 'ON', 'R', 'FSI'], [1, 1, 3, 3, 4, 4, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'L'], [[2, 3], 'R', 'L'], [[4, 5], 'L', 'L'], [[8], 'L', 'L'], [[9, 10, 11], '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', [['L', 'PDI', 'L', 'EN', 'L', 'PDI'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['L', 'L', 'EN', 'R', 'R', 'PDI', 'ON', 'L', 'R', 'ON', 'EN'], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2], 1], [[[0, 1, 2, 3, 4, 5, 6], 'R', 'L'], [[7], 'L', 'L'], [[8, 9], 'L', 'L'], [[10], 'L', 'L']])], [('regression: chained run bookkeeping', [['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: chained run bookkeeping', [['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: chained run bookkeeping', [['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']]), ('regression: chained run bookkeeping', [['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']]), ('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', [['ON', 'R', 'PDI', 'ON', 'EN', 'RLI', 'L', 'LRI', 'ON'], [0, 1, 0, 0, 0, 0, 2, 1, 2], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2, 3, 4, 5], 'R', 'L'], [[6], 'L', 'L'], [[7], 'L', 'R'], [[8], 'L', 'L']]), ('control layout', [['EN', 'PDI', 'R', 'EN', 'EN', 'ON', 'FSI'], [0, 0, 1, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5, 6], 'R', 'L']])], [('regression: chained run bookkeeping', [['R', 'L', 'LRI', 'EN', 'PDI', 'PDI', 'PDI', 'R'], [1, 2, 1, 2, 1, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 4, 5, 6, 7], 'L', 'R'], [[3], 'L', 'L']]), ('regression: chained run bookkeeping', [['ON', 'FSI', 'R', 'LRI', 'R', 'FSI', 'PDI', 'PDI'], [1, 1, 2, 2, 5, 4, 4, 2], 1], [[[0, 1], 'R', 'R'], [[2, 3, 7], 'L', 'L'], [[4], 'R', 'R'], [[5, 6], 'R', 'L']]), ('regression: chained run bookkeeping', [['FSI', 'FSI', 'FSI', 'PDI', 'LRI', 'PDI', 'LRI', 'R', 'ON', 'PDI', 'FSI', 'EN', 'PDI', 'EN'], [1, 2, 4, 4, 4, 4, 4, 7, 6, 4, 4, 6, 4, 4], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2, 3, 4, 5, 6, 9, 10, 12, 13], 'L', 'L'], [[7], 'R', 'R'], [[8], 'R', 'L'], [[11], 'L', 'L']]), ('regression: chained run bookkeeping', [['L', 'LRI', 'FSI', 'L', 'RLI', 'PDI', 'L', 'ON', 'PDI', 'R', 'R'], [1, 1, 2, 4, 4, 4, 4, 4, 2, 2, 3], 1], [[[0, 1], 'R', 'R'], [[2, 8, 9], 'L', 'R'], [[3, 4, 5, 6, 7], 'L', 'L'], [[10], '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']]), ('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', [['ON', 'L', 'PDI', 'FSI', 'EN', 'RLI', 'EN'], [1, 2, 1, 1, 2, 2, 3], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4, 5], 'L', 'L'], [[6], 'R', 'R']]), ('control layout', [['ON', 'PDI', 'ON', 'ON', 'RLI', 'EN', 'R', 'R'], [0, 0, 0, 0, 0, 2, 1, 1], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7], 'L', 'R']])], [('regression: chained run bookkeeping', [['ON', 'PDI', 'L', 'PDI', 'L', 'RLI', 'EN', 'PDI', 'LRI', 'EN', 'FSI'], [0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2], 0], [[[0, 1, 2, 3, 4, 5, 7, 8], 'L', 'L'], [[6], 'L', 'L'], [[9, 10], 'L', 'L']]), ('regression: chained run bookkeeping', [['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: chained run bookkeeping', [['FSI', 'R', 'RLI', 'L', 'LRI', 'R', 'PDI', 'RLI', 'EN', 'PDI'], [1, 3, 2, 4, 3, 5, 3, 3, 5, 3], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2], 'R', 'L'], [[3], 'L', 'L'], [[4, 6, 7, 9], 'L', 'R'], [[5], 'R', 'R'], [[8], 'R', 'R']]), ('regression: chained run bookkeeping', [['RLI', 'RLI', 'L', 'RLI', 'EN', 'PDI', 'EN', 'PDI', 'PDI', 'EN', 'PDI', 'R'], [0, 1, 4, 3, 6, 3, 3, 1, 0, 0, 0, 1], 0], [[[0, 8, 9, 10], 'L', 'R'], [[1, 7], 'R', 'R'], [[2], 'L', 'L'], [[3, 5, 6], 'L', 'R'], [[4], 'L', 'L'], [[11], 'R', 'R']]), ('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', [['ON', 'EN', 'PDI', 'L'], [1, 2, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R']]), ('control layout', [['LRI', 'PDI', 'PDI', 'R', 'R', 'PDI', 'EN', 'LRI', 'EN', 'FSI'], [0, 0, 0, 1, 1, 0, 0, 0, 2, 2], 0], [[[0, 1, 2], 'L', 'R'], [[3, 4], 'R', 'R'], [[5, 6, 7], 'R', 'L'], [[8, 9], '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[-1])
        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 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 = [[('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: chained run bookkeeping', [['L', 'FSI', 'LRI', 'ON', 'R', 'PDI', 'LRI', 'ON'], [2, 1, 2, 4, 4, 2, 2, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 5, 6], 'L', 'L'], [[3, 4], 'L', 'L'], [[7], 'L', 'L']]), ('regression: chained run bookkeeping', [['EN', 'PDI', 'FSI', 'LRI', 'L', 'L', 'LRI', 'ON', 'PDI', 'R', 'L', 'EN', 'L', 'R'], [1, 1, 1, 2, 4, 4, 4, 6, 4, 4, 4, 4, 4, 5], 1], [[[0, 1, 2], 'R', 'R'], [[3], 'L', 'L'], [[4, 5, 6, 8, 9, 10, 11, 12], 'L', 'R'], [[7], 'L', 'L'], [[13], '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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['R', 'R', 'PDI', 'PDI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']]), ('control layout', [['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']]), ('control layout', [['L', 'PDI', 'R', 'L', 'PDI', 'PDI', 'R', 'RLI'], [0, 0, 1, 0, 0, 0, 1, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5], 'R', 'R'], [[6], 'R', 'R'], [[7], 'R', 'L']])], [('regression: chained run bookkeeping', [['EN', 'FSI', 'R', 'R', 'R', 'ON', 'PDI', 'ON'], [0, 0, 3, 2, 3, 2, 0, 0], 0], [[[0, 1, 6, 7], 'L', 'L'], [[2], 'R', 'R'], [[3], 'R', 'R'], [[4], 'R', 'R'], [[5], 'R', 'L']]), ('regression: chained run bookkeeping', [['L', 'L', 'PDI', 'ON', 'L', 'LRI', 'EN', 'EN', 'R', 'R', 'R', 'PDI', 'RLI', 'FSI'], [1, 2, 1, 1, 2, 1, 2, 2, 2, 3, 2, 1, 1, 3], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'L'], [[4], 'L', 'L'], [[5, 11, 12], 'L', 'R'], [[6, 7, 8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L'], [[13], 'R', 'R']]), ('regression: chained run bookkeeping', [['RLI', 'FSI', 'RLI', 'EN', 'PDI', 'PDI', 'FSI', 'R', 'L'], [1, 3, 4, 5, 4, 3, 3, 5, 4], 1], [[[0], 'R', 'R'], [[1, 5, 6], 'R', 'R'], [[2, 4], 'L', 'L'], [[3], 'R', 'R'], [[7], 'R', 'R'], [[8], 'R', 'L']]), ('regression: chained run bookkeeping', [['PDI', 'RLI', 'L', 'L', 'EN', 'L', 'PDI', 'PDI', 'L', 'ON', 'R', 'FSI'], [1, 1, 3, 3, 4, 4, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 6, 7], 'R', 'L'], [[2, 3], 'R', 'L'], [[4, 5], 'L', 'L'], [[8], 'L', 'L'], [[9, 10, 11], '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', [['L', 'PDI', 'L', 'EN', 'L', 'PDI'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['L', 'L', 'EN', 'R', 'R', 'PDI', 'ON', 'L', 'R', 'ON', 'EN'], [1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2], 1], [[[0, 1, 2, 3, 4, 5, 6], 'R', 'L'], [[7], 'L', 'L'], [[8, 9], 'L', 'L'], [[10], 'L', 'L']])], [('regression: chained run bookkeeping', [['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: chained run bookkeeping', [['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: chained run bookkeeping', [['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']]), ('regression: chained run bookkeeping', [['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']]), ('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', [['ON', 'R', 'PDI', 'ON', 'EN', 'RLI', 'L', 'LRI', 'ON'], [0, 1, 0, 0, 0, 0, 2, 1, 2], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2, 3, 4, 5], 'R', 'L'], [[6], 'L', 'L'], [[7], 'L', 'R'], [[8], 'L', 'L']]), ('control layout', [['EN', 'PDI', 'R', 'EN', 'EN', 'ON', 'FSI'], [0, 0, 1, 0, 0, 0, 0], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R'], [[3, 4, 5, 6], 'R', 'L']])], [('regression: chained run bookkeeping', [['R', 'L', 'LRI', 'EN', 'PDI', 'PDI', 'PDI', 'R'], [1, 2, 1, 2, 1, 1, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 4, 5, 6, 7], 'L', 'R'], [[3], 'L', 'L']]), ('regression: chained run bookkeeping', [['ON', 'FSI', 'R', 'LRI', 'R', 'FSI', 'PDI', 'PDI'], [1, 1, 2, 2, 5, 4, 4, 2], 1], [[[0, 1], 'R', 'R'], [[2, 3, 7], 'L', 'L'], [[4], 'R', 'R'], [[5, 6], 'R', 'L']]), ('regression: chained run bookkeeping', [['FSI', 'FSI', 'FSI', 'PDI', 'LRI', 'PDI', 'LRI', 'R', 'ON', 'PDI', 'FSI', 'EN', 'PDI', 'EN'], [1, 2, 4, 4, 4, 4, 4, 7, 6, 4, 4, 6, 4, 4], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2, 3, 4, 5, 6, 9, 10, 12, 13], 'L', 'L'], [[7], 'R', 'R'], [[8], 'R', 'L'], [[11], 'L', 'L']]), ('regression: chained run bookkeeping', [['L', 'LRI', 'FSI', 'L', 'RLI', 'PDI', 'L', 'ON', 'PDI', 'R', 'R'], [1, 1, 2, 4, 4, 4, 4, 4, 2, 2, 3], 1], [[[0, 1], 'R', 'R'], [[2, 8, 9], 'L', 'R'], [[3, 4, 5, 6, 7], 'L', 'L'], [[10], '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']]), ('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', [['ON', 'L', 'PDI', 'FSI', 'EN', 'RLI', 'EN'], [1, 2, 1, 1, 2, 2, 3], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R'], [[4, 5], 'L', 'L'], [[6], 'R', 'R']]), ('control layout', [['ON', 'PDI', 'ON', 'ON', 'RLI', 'EN', 'R', 'R'], [0, 0, 0, 0, 0, 2, 1, 1], 0], [[[0, 1, 2, 3, 4], 'L', 'L'], [[5], 'L', 'L'], [[6, 7], 'L', 'R']])], [('regression: chained run bookkeeping', [['ON', 'PDI', 'L', 'PDI', 'L', 'RLI', 'EN', 'PDI', 'LRI', 'EN', 'FSI'], [0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 2], 0], [[[0, 1, 2, 3, 4, 5, 7, 8], 'L', 'L'], [[6], 'L', 'L'], [[9, 10], 'L', 'L']]), ('regression: chained run bookkeeping', [['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: chained run bookkeeping', [['FSI', 'R', 'RLI', 'L', 'LRI', 'R', 'PDI', 'RLI', 'EN', 'PDI'], [1, 3, 2, 4, 3, 5, 3, 3, 5, 3], 1], [[[0], 'R', 'R'], [[1], 'R', 'R'], [[2], 'R', 'L'], [[3], 'L', 'L'], [[4, 6, 7, 9], 'L', 'R'], [[5], 'R', 'R'], [[8], 'R', 'R']]), ('regression: chained run bookkeeping', [['RLI', 'RLI', 'L', 'RLI', 'EN', 'PDI', 'EN', 'PDI', 'PDI', 'EN', 'PDI', 'R'], [0, 1, 4, 3, 6, 3, 3, 1, 0, 0, 0, 1], 0], [[[0, 8, 9, 10], 'L', 'R'], [[1, 7], 'R', 'R'], [[2], 'L', 'L'], [[3, 5, 6], 'L', 'R'], [[4], 'L', 'L'], [[11], 'R', 'R']]), ('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', [['ON', 'EN', 'PDI', 'L'], [1, 2, 1, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 3], 'L', 'R']]), ('control layout', [['LRI', 'PDI', 'PDI', 'R', 'R', 'PDI', 'EN', 'LRI', 'EN', 'FSI'], [0, 0, 0, 1, 1, 0, 0, 0, 2, 2], 0], [[[0, 1, 2], 'L', 'R'], [[3, 4], 'R', 'R'], [[5, 6, 7], 'R', 'L'], [[8, 9], '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:
The run after a PDI appears twice: joined and as its own sequence.

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[0] + len(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 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 = [[('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']]), ('regression: chained sequence end', [['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']]), ('partial-repair probe', [['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']]), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'ON', 'ON', 'PDI', 'R', 'RLI', 'PDI'], [1, 1, 1, 2, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7, 8, 9], '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', [['PDI', 'EN', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R']]), ('control layout', [['R', 'EN', 'EN', 'L', 'R', 'PDI', 'ON'], [1, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4, 5, 6], 'R', 'R']])], [('regression: chained sequence end', [['ON', 'EN', 'RLI', 'ON', 'R', 'PDI', 'R', 'PDI', 'ON', 'L', 'PDI', 'L', 'PDI'], [1, 1, 1, 3, 3, 1, 1, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 2, 5, 6, 7, 8], 'R', 'L'], [[3, 4], 'R', 'R'], [[9], 'L', 'L'], [[10, 11, 12], 'L', 'R']]), ('regression: chained sequence end', [['ON', 'EN', 'LRI', 'L', 'LRI', 'PDI', 'L', 'RLI', 'ON', 'ON', 'PDI', 'FSI', 'RLI'], [1, 2, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2, 4], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2], 'L', 'R'], [[3, 4, 5, 6, 7, 10, 11], 'L', 'L'], [[8, 9], 'R', 'R'], [[12], 'L', 'L']]), ('partial-repair probe', [['L', 'PDI', 'L', 'L', 'L', 'R', 'R', 'PDI', 'R', 'R', 'LRI', 'R', 'L', 'PDI'], [1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7, 8, 9, 10, 13], 'L', 'R'], [[11], 'R', 'R'], [[12], 'R', 'L']]), ('partial-repair probe', [['EN', 'PDI', 'L', 'PDI', 'EN', 'PDI', 'EN', 'LRI', 'ON', 'ON', 'EN', 'RLI', 'R'], [0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3], 0], [[[0, 1, 2, 3, 4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], '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']]), ('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', 'R', 'L', 'ON', 'FSI', 'PDI'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['FSI', 'EN', 'L', 'L', 'FSI', 'L'], [0, 2, 2, 2, 2, 4], 0], [[[0], 'L', 'L'], [[1, 2, 3, 4], 'L', 'L'], [[5], 'L', 'L']])], [('regression: chained sequence end', [['L', 'FSI', 'L', 'ON', 'RLI', 'ON', 'L', 'R', 'PDI', 'ON', 'PDI', 'ON', 'L'], [0, 0, 2, 2, 2, 3, 3, 3, 2, 2, 0, 0, 0], 0], [[[0, 1, 10, 11, 12], 'L', 'L'], [[2, 3, 4, 8, 9], 'L', 'L'], [[5, 6, 7], 'R', 'R']]), ('regression: chained sequence end', [['ON', 'EN', 'EN', 'PDI', 'EN', 'R', 'EN', 'RLI', 'ON', 'ON', 'PDI'], [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0], 0], [[[0, 1, 2, 3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7, 10], 'R', 'L'], [[8, 9], 'R', 'R']]), ('partial-repair probe', [['L', 'PDI', 'ON', 'ON', 'R', 'R', 'L', 'EN'], [0, 0, 0, 0, 1, 1, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'R'], [[4, 5], 'R', 'R'], [[6, 7], 'R', 'L']]), ('partial-repair probe', [['PDI', 'PDI', 'RLI', 'RLI', 'EN', 'FSI', 'L'], [0, 0, 0, 1, 4, 3, 4], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'R'], [[6], 'L', 'L']]), ('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', [['PDI', 'EN', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R']]), ('control layout', [['ON', 'PDI', 'RLI', 'L'], [0, 0, 0, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'L', 'L']])], [('regression: chained sequence end', [['EN', 'PDI', 'FSI', 'LRI', 'L', 'L', 'LRI', 'ON', 'PDI', 'R', 'L', 'EN', 'L', 'R'], [1, 1, 1, 2, 4, 4, 4, 6, 4, 4, 4, 4, 4, 5], 1], [[[0, 1, 2], 'R', 'R'], [[3], 'L', 'L'], [[4, 5, 6, 8, 9, 10, 11, 12], 'L', 'R'], [[7], 'L', 'L'], [[13], 'R', 'R']]), ('regression: chained sequence end', [['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']]), ('partial-repair probe', [['L', 'PDI', 'FSI', 'EN', 'R', 'R', 'ON', 'EN', 'PDI', 'L'], [2, 1, 1, 2, 3, 2, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 8, 9], 'L', 'R'], [[3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: chained sequence end', [['ON', 'L', 'R', 'LRI', 'EN', 'L', 'R', 'RLI', 'ON', 'R', 'ON', 'LRI', 'L', 'PDI'], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11, 13], 'R', 'R'], [[12], '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', 'PDI', 'L'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']]), ('control layout', [['L', 'L', 'PDI', 'L', 'EN', 'PDI', 'LRI', 'L', 'L', 'L'], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L'], [[7, 8, 9], 'L', 'L']])], [('regression: chained sequence end', [['EN', 'FSI', 'EN', 'RLI', 'RLI', 'EN', 'EN', 'PDI', 'RLI', 'EN', 'PDI', 'L', 'FSI', 'L'], [2, 1, 2, 2, 3, 5, 6, 3, 3, 6, 3, 4, 3, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3], 'L', 'L'], [[4, 7, 8, 10], 'R', 'L'], [[5], 'R', 'L'], [[6], 'L', 'L'], [[9], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R'], [[13], 'L', 'L']]), ('regression: chained sequence end', [['ON', 'L', 'R', 'LRI', 'EN', 'L', 'R', 'RLI', 'ON', 'R', 'ON', 'LRI', 'L', 'PDI'], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11, 13], 'R', 'R'], [[12], 'L', 'L']]), ('regression: chained sequence end', [['RLI', 'EN', 'EN', 'PDI', 'RLI', 'L', 'LRI', 'FSI', 'ON', 'R'], [1, 3, 4, 1, 1, 3, 3, 4, 6, 7], 1], [[[0, 3, 4], 'R', 'R'], [[1], 'R', 'L'], [[2], 'L', 'L'], [[5, 6], 'R', 'R'], [[7], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R']]), ('partial-repair probe', [['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']]), ('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', [['LRI', 'EN', 'EN', 'ON', 'PDI', 'EN', 'ON', 'L', 'PDI', 'ON', 'PDI', 'L', 'PDI'], [0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'L', 'L'], [[1, 2, 3], 'L', 'L']]), ('control layout', [['R', 'PDI', 'R', 'EN'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])]]
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], min(seq[0] + len(seq), n - 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 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 = [[('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']]), ('regression: chained sequence end', [['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']]), ('partial-repair probe', [['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']]), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'ON', 'ON', 'PDI', 'R', 'RLI', 'PDI'], [1, 1, 1, 2, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7, 8, 9], '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', [['PDI', 'EN', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R']]), ('control layout', [['R', 'EN', 'EN', 'L', 'R', 'PDI', 'ON'], [1, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4, 5, 6], 'R', 'R']])], [('regression: chained sequence end', [['ON', 'EN', 'RLI', 'ON', 'R', 'PDI', 'R', 'PDI', 'ON', 'L', 'PDI', 'L', 'PDI'], [1, 1, 1, 3, 3, 1, 1, 1, 1, 2, 1, 1, 1], 1], [[[0, 1, 2, 5, 6, 7, 8], 'R', 'L'], [[3, 4], 'R', 'R'], [[9], 'L', 'L'], [[10, 11, 12], 'L', 'R']]), ('regression: chained sequence end', [['ON', 'EN', 'LRI', 'L', 'LRI', 'PDI', 'L', 'RLI', 'ON', 'ON', 'PDI', 'FSI', 'RLI'], [1, 2, 1, 2, 2, 2, 2, 2, 3, 3, 2, 2, 4], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2], 'L', 'R'], [[3, 4, 5, 6, 7, 10, 11], 'L', 'L'], [[8, 9], 'R', 'R'], [[12], 'L', 'L']]), ('partial-repair probe', [['L', 'PDI', 'L', 'L', 'L', 'R', 'R', 'PDI', 'R', 'R', 'LRI', 'R', 'L', 'PDI'], [1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 3, 2, 1], 1], [[[0, 1, 2], 'R', 'L'], [[3], 'L', 'L'], [[4, 5, 6, 7, 8, 9, 10, 13], 'L', 'R'], [[11], 'R', 'R'], [[12], 'R', 'L']]), ('partial-repair probe', [['EN', 'PDI', 'L', 'PDI', 'EN', 'PDI', 'EN', 'LRI', 'ON', 'ON', 'EN', 'RLI', 'R'], [0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3], 0], [[[0, 1, 2, 3, 4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11], 'L', 'L'], [[12], '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']]), ('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', 'R', 'L', 'ON', 'FSI', 'PDI'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['FSI', 'EN', 'L', 'L', 'FSI', 'L'], [0, 2, 2, 2, 2, 4], 0], [[[0], 'L', 'L'], [[1, 2, 3, 4], 'L', 'L'], [[5], 'L', 'L']])], [('regression: chained sequence end', [['L', 'FSI', 'L', 'ON', 'RLI', 'ON', 'L', 'R', 'PDI', 'ON', 'PDI', 'ON', 'L'], [0, 0, 2, 2, 2, 3, 3, 3, 2, 2, 0, 0, 0], 0], [[[0, 1, 10, 11, 12], 'L', 'L'], [[2, 3, 4, 8, 9], 'L', 'L'], [[5, 6, 7], 'R', 'R']]), ('regression: chained sequence end', [['ON', 'EN', 'EN', 'PDI', 'EN', 'R', 'EN', 'RLI', 'ON', 'ON', 'PDI'], [0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0], 0], [[[0, 1, 2, 3, 4], 'L', 'R'], [[5], 'R', 'R'], [[6, 7, 10], 'R', 'L'], [[8, 9], 'R', 'R']]), ('partial-repair probe', [['L', 'PDI', 'ON', 'ON', 'R', 'R', 'L', 'EN'], [0, 0, 0, 0, 1, 1, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'R'], [[4, 5], 'R', 'R'], [[6, 7], 'R', 'L']]), ('partial-repair probe', [['PDI', 'PDI', 'RLI', 'RLI', 'EN', 'FSI', 'L'], [0, 0, 0, 1, 4, 3, 4], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'R', 'R'], [[4], 'L', 'L'], [[5], 'L', 'R'], [[6], 'L', 'L']]), ('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', [['PDI', 'EN', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[2], 'R', 'R']]), ('control layout', [['ON', 'PDI', 'RLI', 'L'], [0, 0, 0, 2], 0], [[[0, 1, 2], 'L', 'L'], [[3], 'L', 'L']])], [('regression: chained sequence end', [['EN', 'PDI', 'FSI', 'LRI', 'L', 'L', 'LRI', 'ON', 'PDI', 'R', 'L', 'EN', 'L', 'R'], [1, 1, 1, 2, 4, 4, 4, 6, 4, 4, 4, 4, 4, 5], 1], [[[0, 1, 2], 'R', 'R'], [[3], 'L', 'L'], [[4, 5, 6, 8, 9, 10, 11, 12], 'L', 'R'], [[7], 'L', 'L'], [[13], 'R', 'R']]), ('regression: chained sequence end', [['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']]), ('partial-repair probe', [['L', 'PDI', 'FSI', 'EN', 'R', 'R', 'ON', 'EN', 'PDI', 'L'], [2, 1, 1, 2, 3, 2, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 8, 9], 'L', 'R'], [[3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: chained sequence end', [['ON', 'L', 'R', 'LRI', 'EN', 'L', 'R', 'RLI', 'ON', 'R', 'ON', 'LRI', 'L', 'PDI'], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11, 13], 'R', 'R'], [[12], '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', 'PDI', 'L'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']]), ('control layout', [['L', 'L', 'PDI', 'L', 'EN', 'PDI', 'LRI', 'L', 'L', 'L'], [0, 0, 0, 0, 0, 0, 0, 2, 2, 2], 0], [[[0, 1, 2, 3, 4, 5, 6], 'L', 'L'], [[7, 8, 9], 'L', 'L']])], [('regression: chained sequence end', [['EN', 'FSI', 'EN', 'RLI', 'RLI', 'EN', 'EN', 'PDI', 'RLI', 'EN', 'PDI', 'L', 'FSI', 'L'], [2, 1, 2, 2, 3, 5, 6, 3, 3, 6, 3, 4, 3, 4], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3], 'L', 'L'], [[4, 7, 8, 10], 'R', 'L'], [[5], 'R', 'L'], [[6], 'L', 'L'], [[9], 'L', 'L'], [[11], 'L', 'L'], [[12], 'L', 'R'], [[13], 'L', 'L']]), ('regression: chained sequence end', [['ON', 'L', 'R', 'LRI', 'EN', 'L', 'R', 'RLI', 'ON', 'R', 'ON', 'LRI', 'L', 'PDI'], [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 3], 1], [[[0, 1, 2, 3], 'R', 'R'], [[4, 5, 6, 7], 'L', 'L'], [[8, 9, 10, 11, 13], 'R', 'R'], [[12], 'L', 'L']]), ('regression: chained sequence end', [['RLI', 'EN', 'EN', 'PDI', 'RLI', 'L', 'LRI', 'FSI', 'ON', 'R'], [1, 3, 4, 1, 1, 3, 3, 4, 6, 7], 1], [[[0, 3, 4], 'R', 'R'], [[1], 'R', 'L'], [[2], 'L', 'L'], [[5, 6], 'R', 'R'], [[7], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R']]), ('partial-repair probe', [['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']]), ('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', [['LRI', 'EN', 'EN', 'ON', 'PDI', 'EN', 'ON', 'L', 'PDI', 'ON', 'PDI', 'L', 'PDI'], [0, 2, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 4, 5, 6, 7, 8, 9, 10, 11, 12], 'L', 'L'], [[1, 2, 3], 'L', 'L']]), ('control layout', [['R', 'PDI', 'R', 'EN'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])]]
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:
eos of an isolate-spanning sequence is taken from the middle of the text.

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 lv % 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 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: sos level comparison', [['L', 'FSI', 'EN', 'PDI', 'EN', 'EN', 'ON', 'EN', 'PDI', 'L', 'FSI', 'FSI', 'ON', 'ON'], [2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 4, 4], 1], [[[0], 'L', 'L'], [[1, 3, 4], 'L', 'L'], [[2], 'L', 'L'], [[5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'L'], [[8, 9, 10], 'L', 'R'], [[11], 'L', 'L'], [[12, 13], 'L', 'L']]), ('regression: sos level comparison', [['L', 'PDI', 'PDI', 'LRI', 'ON', 'FSI', 'ON', 'PDI'], [2, 1, 1, 1, 2, 2, 4, 2], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R'], [[4, 5, 7], 'L', 'L'], [[6], 'L', 'L']]), ('partial-repair probe', [['ON', 'RLI', 'L', 'LRI'], [0, 0, 1, 1], 0], [[[0, 1], 'L', 'L'], [[2, 3], 'R', 'R']]), ('partial-repair probe', [['PDI', 'R', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['L', 'ON', 'R', 'RLI', 'PDI', 'LRI', 'L', 'FSI', 'ON', 'LRI'], [0, 0, 0, 0, 0, 0, 2, 2, 4, 4], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L'], [[6, 7], 'L', 'L'], [[8, 9], 'L', 'L']]), ('control layout', [['R', 'PDI', 'R', 'FSI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])], [('regression: sos level comparison', [['L', 'RLI', 'RLI', 'RLI', 'ON', 'PDI', 'ON', 'L', 'ON'], [2, 1, 3, 5, 7, 5, 5, 5, 5], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2], 'R', 'R'], [[3, 5, 6, 7, 8], 'R', 'R'], [[4], 'R', 'R']]), ('regression: sos level comparison', [['L', 'R', 'LRI', 'PDI'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('partial-repair probe', [['PDI', 'RLI', 'ON'], [0, 0, 1], 0], [[[0, 1], 'L', 'L'], [[2], 'R', 'R']]), ('regression: sos level comparison', [['R', 'PDI', 'ON', 'R', 'R', 'RLI', 'L', 'L', 'LRI', 'R', 'LRI', 'L', 'EN'], [1, 1, 1, 1, 1, 1, 3, 4, 3, 5, 4, 6, 6], 1], [[[0, 1, 2, 3, 4, 5], 'R', 'R'], [[6], 'R', 'L'], [[7], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L'], [[11, 12], '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', [['L', 'L', 'ON'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'L', 'FSI', 'EN', 'L', 'PDI', 'L', 'ON', 'L', 'LRI', 'EN'], [0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2], 0], [[[0, 1, 2, 3, 4, 7, 8, 9, 10, 11], 'L', 'L'], [[5, 6], 'L', 'L'], [[12], 'L', 'L']])], [('regression: sos level comparison', [['R', 'EN', 'L', 'FSI', 'PDI', 'PDI', 'PDI', 'EN', 'L', 'RLI', 'ON', 'RLI'], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4, 5, 6, 7, 8, 9], 'R', 'L'], [[10, 11], 'R', 'R']]), ('regression: sos level comparison', [['L', 'PDI', 'FSI', 'EN', 'R', 'R', 'ON', 'EN', 'PDI', 'L'], [2, 1, 1, 2, 3, 2, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 8, 9], 'L', 'R'], [[3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('partial-repair probe', [['FSI', 'EN', 'R', 'RLI', 'L', 'PDI', 'L'], [1, 2, 2, 2, 4, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 5, 6], 'L', 'L'], [[4], 'L', 'L']]), ('partial-repair probe', [['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']]), ('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', [['ON', 'PDI', 'L', 'LRI'], [0, 0, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'L']]), ('control layout', [['LRI', 'PDI', 'FSI'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])], [('regression: sos level comparison', [['RLI', 'EN', 'ON'], [1, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'R']]), ('regression: sos level comparison', [['R', 'L', 'EN', 'L', 'PDI'], [1, 0, 0, 0, 0], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'R', 'L']]), ('regression: sos level comparison', [['PDI', 'R', 'L', 'ON', 'R', 'L', 'EN', 'L'], [0, 0, 0, 0, 1, 0, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: sos level comparison', [['R', 'EN', 'FSI', 'FSI', 'PDI', 'L', 'PDI'], [1, 2, 1, 2, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 6], 'L', 'R'], [[3, 4, 5], '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', [['PDI', 'R', 'LRI', 'PDI', 'ON', 'EN'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['ON', 'PDI', 'PDI', 'EN', 'R', 'L', 'L', 'ON', 'L'], [0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'L', 'L']])], [('regression: sos level comparison', [['EN', 'RLI', 'L', 'RLI', 'ON', 'L', 'EN', 'PDI', 'EN'], [2, 1, 3, 3, 5, 6, 6, 3, 3], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3, 7, 8], 'R', 'R'], [[4], 'R', 'L'], [[5, 6], 'L', 'L']]), ('regression: sos level comparison', [['EN', 'ON', 'ON', 'ON'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('regression: sos level comparison', [['ON', 'R', 'PDI', 'R'], [0, 1, 0, 1], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2], 'R', 'R'], [[3], 'R', 'R']]), ('regression: sos level comparison', [['PDI', 'PDI', 'EN', 'R', 'ON', 'L', 'R', 'R', 'L', 'L', 'R', 'R'], [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4], 'R', 'L'], [[5], 'L', 'L'], [[6, 7, 8, 9, 10, 11], 'L', 'R']]), ('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', [['L', 'FSI', 'PDI', 'PDI', 'L', 'LRI', 'ON', 'L', 'EN', 'ON'], [0, 0, 0, 0, 0, 0, 2, 2, 2, 2], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L'], [[6, 7, 8, 9], 'L', 'L']]), ('control layout', [['PDI', 'PDI', 'L', 'EN', 'EN'], [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)

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 min(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 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: sos level comparison', [['L', 'FSI', 'EN', 'PDI', 'EN', 'EN', 'ON', 'EN', 'PDI', 'L', 'FSI', 'FSI', 'ON', 'ON'], [2, 1, 2, 1, 1, 2, 1, 2, 1, 1, 1, 2, 4, 4], 1], [[[0], 'L', 'L'], [[1, 3, 4], 'L', 'L'], [[2], 'L', 'L'], [[5], 'L', 'L'], [[6], 'L', 'L'], [[7], 'L', 'L'], [[8, 9, 10], 'L', 'R'], [[11], 'L', 'L'], [[12, 13], 'L', 'L']]), ('regression: sos level comparison', [['L', 'PDI', 'PDI', 'LRI', 'ON', 'FSI', 'ON', 'PDI'], [2, 1, 1, 1, 2, 2, 4, 2], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R'], [[4, 5, 7], 'L', 'L'], [[6], 'L', 'L']]), ('partial-repair probe', [['ON', 'RLI', 'L', 'LRI'], [0, 0, 1, 1], 0], [[[0, 1], 'L', 'L'], [[2, 3], 'R', 'R']]), ('partial-repair probe', [['PDI', 'R', 'R'], [0, 0, 1], 0], [[[0, 1], 'L', 'R'], [[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']]), ('unmatched initiator at end', [['L', 'R', 'RLI'], [0, 1, 1], 0], [[[0], 'L', 'R'], [[1, 2], 'R', 'R']]), ('control layout', [['L', 'ON', 'R', 'RLI', 'PDI', 'LRI', 'L', 'FSI', 'ON', 'LRI'], [0, 0, 0, 0, 0, 0, 2, 2, 4, 4], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L'], [[6, 7], 'L', 'L'], [[8, 9], 'L', 'L']]), ('control layout', [['R', 'PDI', 'R', 'FSI'], [1, 1, 1, 1], 1], [[[0, 1, 2, 3], 'R', 'R']])], [('regression: sos level comparison', [['L', 'RLI', 'RLI', 'RLI', 'ON', 'PDI', 'ON', 'L', 'ON'], [2, 1, 3, 5, 7, 5, 5, 5, 5], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2], 'R', 'R'], [[3, 5, 6, 7, 8], 'R', 'R'], [[4], 'R', 'R']]), ('regression: sos level comparison', [['L', 'R', 'LRI', 'PDI'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('partial-repair probe', [['PDI', 'RLI', 'ON'], [0, 0, 1], 0], [[[0, 1], 'L', 'L'], [[2], 'R', 'R']]), ('regression: sos level comparison', [['R', 'PDI', 'ON', 'R', 'R', 'RLI', 'L', 'L', 'LRI', 'R', 'LRI', 'L', 'EN'], [1, 1, 1, 1, 1, 1, 3, 4, 3, 5, 4, 6, 6], 1], [[[0, 1, 2, 3, 4, 5], 'R', 'R'], [[6], 'R', 'L'], [[7], 'L', 'L'], [[8], 'L', 'R'], [[9], 'R', 'R'], [[10], 'R', 'L'], [[11, 12], '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', [['L', 'L', 'ON'], [1, 1, 1], 1], [[[0, 1, 2], 'R', 'R']]), ('control layout', [['EN', 'L', 'EN', 'L', 'FSI', 'EN', 'L', 'PDI', 'L', 'ON', 'L', 'LRI', 'EN'], [0, 0, 0, 0, 0, 2, 2, 0, 0, 0, 0, 0, 2], 0], [[[0, 1, 2, 3, 4, 7, 8, 9, 10, 11], 'L', 'L'], [[5, 6], 'L', 'L'], [[12], 'L', 'L']])], [('regression: sos level comparison', [['R', 'EN', 'L', 'FSI', 'PDI', 'PDI', 'PDI', 'EN', 'L', 'RLI', 'ON', 'RLI'], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4, 5, 6, 7, 8, 9], 'R', 'L'], [[10, 11], 'R', 'R']]), ('regression: sos level comparison', [['L', 'PDI', 'FSI', 'EN', 'R', 'R', 'ON', 'EN', 'PDI', 'L'], [2, 1, 1, 2, 3, 2, 2, 2, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 8, 9], 'L', 'R'], [[3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('partial-repair probe', [['FSI', 'EN', 'R', 'RLI', 'L', 'PDI', 'L'], [1, 2, 2, 2, 4, 2, 2], 1], [[[0], 'R', 'R'], [[1, 2, 3, 5, 6], 'L', 'L'], [[4], 'L', 'L']]), ('partial-repair probe', [['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']]), ('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', [['ON', 'PDI', 'L', 'LRI'], [0, 0, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'L']]), ('control layout', [['LRI', 'PDI', 'FSI'], [0, 0, 0], 0], [[[0, 1, 2], 'L', 'L']])], [('regression: sos level comparison', [['RLI', 'EN', 'ON'], [1, 4, 3], 1], [[[0], 'R', 'R'], [[1], 'L', 'L'], [[2], 'L', 'R']]), ('regression: sos level comparison', [['R', 'L', 'EN', 'L', 'PDI'], [1, 0, 0, 0, 0], 0], [[[0], 'R', 'R'], [[1, 2, 3, 4], 'R', 'L']]), ('regression: sos level comparison', [['PDI', 'R', 'L', 'ON', 'R', 'L', 'EN', 'L'], [0, 0, 0, 0, 1, 0, 0, 0], 0], [[[0, 1, 2, 3], 'L', 'R'], [[4], 'R', 'R'], [[5, 6, 7], 'R', 'L']]), ('regression: sos level comparison', [['R', 'EN', 'FSI', 'FSI', 'PDI', 'L', 'PDI'], [1, 2, 1, 2, 2, 2, 1], 1], [[[0], 'R', 'L'], [[1], 'L', 'L'], [[2, 6], 'L', 'R'], [[3, 4, 5], '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', [['PDI', 'R', 'LRI', 'PDI', 'ON', 'EN'], [0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L']]), ('control layout', [['ON', 'PDI', 'PDI', 'EN', 'R', 'L', 'L', 'ON', 'L'], [0, 0, 0, 0, 0, 0, 0, 0, 0], 0], [[[0, 1, 2, 3, 4, 5, 6, 7, 8], 'L', 'L']])], [('regression: sos level comparison', [['EN', 'RLI', 'L', 'RLI', 'ON', 'L', 'EN', 'PDI', 'EN'], [2, 1, 3, 3, 5, 6, 6, 3, 3], 1], [[[0], 'L', 'L'], [[1], 'L', 'R'], [[2, 3, 7, 8], 'R', 'R'], [[4], 'R', 'L'], [[5, 6], 'L', 'L']]), ('regression: sos level comparison', [['EN', 'ON', 'ON', 'ON'], [2, 1, 1, 1], 1], [[[0], 'L', 'L'], [[1, 2, 3], 'L', 'R']]), ('regression: sos level comparison', [['ON', 'R', 'PDI', 'R'], [0, 1, 0, 1], 0], [[[0], 'L', 'R'], [[1], 'R', 'R'], [[2], 'R', 'R'], [[3], 'R', 'R']]), ('regression: sos level comparison', [['PDI', 'PDI', 'EN', 'R', 'ON', 'L', 'R', 'R', 'L', 'L', 'R', 'R'], [1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1], 1], [[[0, 1, 2, 3, 4], 'R', 'L'], [[5], 'L', 'L'], [[6, 7, 8, 9, 10, 11], 'L', 'R']]), ('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', [['L', 'FSI', 'PDI', 'PDI', 'L', 'LRI', 'ON', 'L', 'EN', 'ON'], [0, 0, 0, 0, 0, 0, 2, 2, 2, 2], 0], [[[0, 1, 2, 3, 4, 5], 'L', 'L'], [[6, 7, 8, 9], 'L', 'L']]), ('control layout', [['PDI', 'PDI', 'L', 'EN', 'EN'], [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 start types follow the run level 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