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

N = 1
observations = []
def solve(x):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in sorted(pairs):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            continue
        if inside[0] == e:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'L', 'ON', 'ON', 'R', 'ON'], [[1, 8], [2, 5]], 0, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'ON', 'R', 'L']), ('regression: embedding match test', [['ON', 'R', 'ON', 'L', 'ON', 'ON', 'L', 'ON', 'ON', 'R', 'R'], [[2, 4], [0, 5]], 2, 'R'], ['L', 'R', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', 'R', 'R']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'L', 'L'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'L', 'R', 'L', 'EN', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['L', 'ON', 'ON', 'R', 'ON', 'ON', 'R', 'R'], [[1, 5], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'R', 'R', 'R', 'R', 'R']), ('control layout', [['R', 'R', 'AN', 'R', 'L', 'L', 'L', 'L', 'EN', 'L', 'ON', 'ON', 'ON'], [[10, 11]], 0, 'R'], ['R', 'R', 'AN', 'R', 'L', 'L', 'L', 'L', 'EN', 'L', 'ON', 'ON', 'ON'])], [('regression: embedding match test', [['EN', 'ON', 'ON', 'ON', 'ON', 'R', 'L', 'ON', 'L', 'R'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'R', 'L', 'L', 'L', 'R']), ('regression: embedding match test', [['ON', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'ON'], [[5, 7], [2, 8]], 0, 'R'], ['ON', 'ON', 'L', 'AN', 'ON', 'L', 'L', 'L', 'L', 'L', 'ON']), ('regression: embedding match test', [['L', 'ON', 'ON', 'L', 'ON', 'EN', 'ON', 'R', 'L'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'EN', 'R', 'R', 'L']), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'R', 'ON', 'ON', 'ON'], [[1, 7], [2, 5]], 2, 'L'], ['R', 'L', 'L', 'L', 'R', 'L', 'ON', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'ON', 'ON', 'AN', 'L', 'ON', 'ON', 'ON', 'R', 'L'], [[1, 7], [2, 5]], 1, 'R'], ['R', 'R', 'R', 'AN', 'L', 'R', 'ON', 'R', 'R', 'L']), ('control layout', [['R', 'R', 'EN', 'R', 'L', 'EN', 'ON', 'AN', 'ON'], [], 2, 'R'], ['R', 'R', 'EN', 'R', 'L', 'EN', 'ON', 'AN', 'ON'])], [('regression: embedding match test', [['R', 'AN', 'L', 'ON', 'L', 'R', 'L', 'L', 'ON', 'AN'], [[3, 8]], 1, 'R'], ['R', 'AN', 'L', 'R', 'L', 'R', 'L', 'L', 'R', 'AN']), ('regression: embedding match test', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('partial-repair probe', [['L', 'ON', 'ON', 'R', 'EN', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'ON'], [[8, 11]], 2, 'R'], ['L', 'ON', 'ON', 'R', 'EN', 'ON', 'ON', 'ON', 'L', 'L', 'R', 'L']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'AN', 'L', 'ON', 'R', 'L', 'ON', 'R', 'R'], [[1, 8], [2, 5]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'L', 'R', 'L', 'L', 'R', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'AN', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'R', 'R'], [[4, 6]], 2, 'R'], ['R', 'AN', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'EN', 'R'], [], 2, 'R'], ['ON', 'EN', 'R'])], [('regression: embedding match test', [['R', 'ON', 'ON', 'AN', 'L', 'ON', 'L', 'ON', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'AN', 'L', 'L', 'L', 'ON', 'L', 'R']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'R', 'ON', 'ON', 'ON'], [[1, 7], [2, 5]], 0, 'R'], ['R', 'L', 'L', 'L', 'R', 'L', 'ON', 'L']), ('partial-repair probe', [['ON', 'L', 'R', 'ON', 'ON', 'L', 'L', 'R', 'R', 'ON', 'R', 'L'], [[0, 4]], 2, 'R'], ['L', 'L', 'R', 'ON', 'L', 'L', 'L', 'R', 'R', 'ON', 'R', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'AN', 'L', 'ON'], [], 0, 'L'], ['L', 'AN', 'L', 'ON']), ('control layout', [['ON', 'R', 'R', 'AN', 'L', 'AN', 'EN', 'ON'], [], 2, 'L'], ['ON', 'R', 'R', 'AN', 'L', 'AN', 'EN', 'ON'])], [('regression: embedding match test', [['R', 'AN', 'L', 'ON', 'L', 'R', 'L', 'L', 'ON', 'AN'], [[3, 8]], 1, 'R'], ['R', 'AN', 'L', 'R', 'L', 'R', 'L', 'L', 'R', 'AN']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'AN', 'L', 'ON', 'R', 'L', 'ON', 'R', 'R'], [[1, 8], [2, 5]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'L', 'R', 'L', 'L', 'R', 'R']), ('regression: embedding match test', [['L', 'ON', 'R', 'ON', 'AN', 'L', 'ON', 'L', 'ON', 'EN', 'L', 'AN'], [[3, 6]], 2, 'R'], ['L', 'ON', 'R', 'L', 'AN', 'L', 'L', 'L', 'ON', 'EN', 'L', 'AN']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'R', 'ON', 'L', 'L', 'ON', 'L'], [[1, 8], [2, 5]], 2, 'R'], ['R', 'L', 'L', 'R', 'R', 'L', 'L', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['R', 'L', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'ON', 'R', 'ON', 'R', 'L'], [], 0, 'R'], ['R', 'L', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'ON', 'R', 'ON', 'R', 'L']), ('control layout', [['ON', 'ON', 'R', 'L', 'R', 'EN', 'R', 'ON', 'ON', 'L', 'ON'], [], 1, 'R'], ['ON', 'ON', 'R', 'L', 'R', 'EN', 'R', 'ON', 'ON', 'L', 'ON'])]]
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):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in sorted(pairs):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            continue
        if inside.count(e) > len(inside) // 2:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'L', 'ON', 'ON', 'R', 'ON'], [[1, 8], [2, 5]], 0, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'ON', 'R', 'L']), ('regression: embedding match test', [['ON', 'R', 'ON', 'L', 'ON', 'ON', 'L', 'ON', 'ON', 'R', 'R'], [[2, 4], [0, 5]], 2, 'R'], ['L', 'R', 'L', 'L', 'L', 'L', 'L', 'ON', 'ON', 'R', 'R']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'L', 'L'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'L', 'R', 'L', 'EN', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['L', 'ON', 'ON', 'R', 'ON', 'ON', 'R', 'R'], [[1, 5], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'R', 'R', 'R', 'R', 'R']), ('control layout', [['R', 'R', 'AN', 'R', 'L', 'L', 'L', 'L', 'EN', 'L', 'ON', 'ON', 'ON'], [[10, 11]], 0, 'R'], ['R', 'R', 'AN', 'R', 'L', 'L', 'L', 'L', 'EN', 'L', 'ON', 'ON', 'ON'])], [('regression: embedding match test', [['EN', 'ON', 'ON', 'ON', 'ON', 'R', 'L', 'ON', 'L', 'R'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'R', 'L', 'L', 'L', 'R']), ('regression: embedding match test', [['ON', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'ON'], [[5, 7], [2, 8]], 0, 'R'], ['ON', 'ON', 'L', 'AN', 'ON', 'L', 'L', 'L', 'L', 'L', 'ON']), ('regression: embedding match test', [['L', 'ON', 'ON', 'L', 'ON', 'EN', 'ON', 'R', 'L'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'EN', 'R', 'R', 'L']), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'R', 'ON', 'ON', 'ON'], [[1, 7], [2, 5]], 2, 'L'], ['R', 'L', 'L', 'L', 'R', 'L', 'ON', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'ON', 'ON', 'AN', 'L', 'ON', 'ON', 'ON', 'R', 'L'], [[1, 7], [2, 5]], 1, 'R'], ['R', 'R', 'R', 'AN', 'L', 'R', 'ON', 'R', 'R', 'L']), ('control layout', [['R', 'R', 'EN', 'R', 'L', 'EN', 'ON', 'AN', 'ON'], [], 2, 'R'], ['R', 'R', 'EN', 'R', 'L', 'EN', 'ON', 'AN', 'ON'])], [('regression: embedding match test', [['R', 'AN', 'L', 'ON', 'L', 'R', 'L', 'L', 'ON', 'AN'], [[3, 8]], 1, 'R'], ['R', 'AN', 'L', 'R', 'L', 'R', 'L', 'L', 'R', 'AN']), ('regression: embedding match test', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('partial-repair probe', [['L', 'ON', 'ON', 'R', 'EN', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'ON'], [[8, 11]], 2, 'R'], ['L', 'ON', 'ON', 'R', 'EN', 'ON', 'ON', 'ON', 'L', 'L', 'R', 'L']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'AN', 'L', 'ON', 'R', 'L', 'ON', 'R', 'R'], [[1, 8], [2, 5]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'L', 'R', 'L', 'L', 'R', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'AN', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'R', 'R'], [[4, 6]], 2, 'R'], ['R', 'AN', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'EN', 'R'], [], 2, 'R'], ['ON', 'EN', 'R'])], [('regression: embedding match test', [['R', 'ON', 'ON', 'AN', 'L', 'ON', 'L', 'ON', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'AN', 'L', 'L', 'L', 'ON', 'L', 'R']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('partial-repair probe', [['R', 'ON', 'ON', 'L', 'R', 'ON', 'ON', 'ON'], [[1, 7], [2, 5]], 0, 'R'], ['R', 'L', 'L', 'L', 'R', 'L', 'ON', 'L']), ('partial-repair probe', [['ON', 'L', 'R', 'ON', 'ON', 'L', 'L', 'R', 'R', 'ON', 'R', 'L'], [[0, 4]], 2, 'R'], ['L', 'L', 'R', 'ON', 'L', 'L', 'L', 'R', 'R', 'ON', 'R', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'AN', 'L', 'ON'], [], 0, 'L'], ['L', 'AN', 'L', 'ON']), ('control layout', [['ON', 'R', 'R', 'AN', 'L', 'AN', 'EN', 'ON'], [], 2, 'L'], ['ON', 'R', 'R', 'AN', 'L', 'AN', 'EN', 'ON'])], [('regression: embedding match test', [['R', 'AN', 'L', 'ON', 'L', 'R', 'L', 'L', 'ON', 'AN'], [[3, 8]], 1, 'R'], ['R', 'AN', 'L', 'R', 'L', 'R', 'L', 'L', 'R', 'AN']), ('regression: embedding match test', [['EN', 'ON', 'ON', 'AN', 'L', 'ON', 'R', 'L', 'ON', 'R', 'R'], [[1, 8], [2, 5]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'L', 'R', 'L', 'L', 'R', 'R']), ('regression: embedding match test', [['L', 'ON', 'R', 'ON', 'AN', 'L', 'ON', 'L', 'ON', 'EN', 'L', 'AN'], [[3, 6]], 2, 'R'], ['L', 'ON', 'R', 'L', 'AN', 'L', 'L', 'L', 'ON', 'EN', 'L', 'AN']), ('regression: embedding match test', [['R', 'ON', 'ON', 'R', 'R', 'ON', 'L', 'L', 'ON', 'L'], [[1, 8], [2, 5]], 2, 'R'], ['R', 'L', 'L', 'R', 'R', 'L', 'L', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['R', 'L', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'ON', 'R', 'ON', 'R', 'L'], [], 0, 'R'], ['R', 'L', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'ON', 'R', 'ON', 'R', 'L']), ('control layout', [['ON', 'ON', 'R', 'L', 'R', 'EN', 'R', 'ON', 'ON', 'L', 'ON'], [], 1, 'R'], ['ON', 'ON', 'R', 'L', 'R', 'EN', 'R', 'ON', 'ON', 'L', 'ON'])]]
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:
A pair containing both directions resolves to the opposite direction.

Input [classes, bracket pairs, embedding level, sos]. Process pairs in opener order. Strong types inside (EN/AN count as R) matching the embedding direction set both brackets to it; if only the opposite direction is inside, use the first strong type before the opener (checking already-updated brackets; sos if none); with no strong type inside leave them. Return classes.

split: open-access


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

N = 1
observations = []
def solve(x):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in sorted(pairs, key=lambda p: p[1]):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            continue
        if e in inside:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'AN', 'R', 'ON', 'L', 'EN', 'ON'], [[1, 8], [2, 5]], 0, 'R'], ['ON', 'L', 'L', 'AN', 'R', 'L', 'L', 'EN', 'L']), ('regression: pair processing order', [['R', 'ON', 'ON', 'ON', 'AN', 'ON', 'L', 'EN', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'ON', 'AN', 'L', 'L', 'EN', 'L', 'R']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L'], [[1, 6], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'L', 'R', 'R', 'R', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'ON', 'R', 'L', 'L', 'L', 'R'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'R', 'L', 'L', 'L', 'R']), ('control layout', [['R', 'R', 'EN', 'ON', 'EN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON', 'ON'], [[5, 10], [3, 11]], 0, 'R'], ['R', 'R', 'EN', 'R', 'EN', 'R', 'ON', 'R', 'ON', 'ON', 'R', 'R'])], [('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'L', 'R', 'L', 'R', 'L']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'R', 'L', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'R', 'L', 'L', 'R', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'L', 'ON', 'ON', 'R', 'ON'], [[1, 7], [2, 4]], 1, 'L'], ['ON', 'R', 'R', 'L', 'R', 'ON', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'AN', 'ON', 'AN', 'L', 'L', 'AN'], [], 0, 'L'], ['R', 'AN', 'ON', 'AN', 'L', 'L', 'AN']), ('control layout', [['EN', 'ON', 'ON', 'ON', 'ON', 'R', 'L', 'ON', 'L', 'R'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'R', 'L', 'L', 'L', 'R'])], [('regression: pair processing order', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L'], [[1, 6], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'L', 'R', 'R', 'R', 'L']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 7], [2, 5]], 0, 'R'], ['EN', 'L', 'L', 'AN', 'ON', 'L', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'EN', 'AN', 'R', 'EN', 'R', 'R', 'AN', 'EN', 'L'], [], 2, 'L'], ['L', 'EN', 'AN', 'R', 'EN', 'R', 'R', 'AN', 'EN', 'L']), ('control layout', [['R', 'L', 'ON', 'ON', 'ON', 'EN', 'R', 'AN', 'L', 'ON'], [[2, 3]], 1, 'R'], ['R', 'L', 'ON', 'ON', 'ON', 'EN', 'R', 'AN', 'L', 'ON'])], [('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['R', 'ON', 'ON', 'ON', 'AN', 'ON', 'L', 'EN', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'ON', 'AN', 'L', 'L', 'EN', 'L', 'R']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'L', 'L'], [[1, 7], [2, 4]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'ON', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['R', 'ON', 'ON', 'AN', 'R', 'ON', 'ON', 'L', 'R'], [[1, 6], [2, 5]], 0, 'L'], ['R', 'R', 'R', 'AN', 'R', 'R', 'R', 'L', 'R']), ('control layout', [['AN', 'R', 'EN', 'ON', 'EN', 'ON', 'EN', 'ON', 'ON', 'ON', 'R'], [[5, 7]], 1, 'R'], ['AN', 'R', 'EN', 'ON', 'EN', 'R', 'EN', 'R', 'ON', 'ON', 'R'])], [('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'ON', 'R'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'L', 'R', 'L', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('regression: pair processing order', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'ON', 'ON', 'AN', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 2, 'L'], ['L', 'L', 'L', 'AN', 'L', 'L', 'L']), ('control layout', [['R', 'L', 'R', 'AN', 'AN', 'R', 'ON', 'L', 'ON', 'L', 'ON'], [], 0, 'L'], ['R', 'L', 'R', 'AN', 'AN', 'R', 'ON', 'L', 'ON', 'L', 'ON'])]]
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):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in reversed(sorted(pairs)):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            continue
        if e in inside:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'AN', 'R', 'ON', 'L', 'EN', 'ON'], [[1, 8], [2, 5]], 0, 'R'], ['ON', 'L', 'L', 'AN', 'R', 'L', 'L', 'EN', 'L']), ('regression: pair processing order', [['R', 'ON', 'ON', 'ON', 'AN', 'ON', 'L', 'EN', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'ON', 'AN', 'L', 'L', 'EN', 'L', 'R']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L'], [[1, 6], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'L', 'R', 'R', 'R', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'ON', 'R', 'L', 'L', 'L', 'R'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'R', 'L', 'L', 'L', 'R']), ('control layout', [['R', 'R', 'EN', 'ON', 'EN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON', 'ON'], [[5, 10], [3, 11]], 0, 'R'], ['R', 'R', 'EN', 'R', 'EN', 'R', 'ON', 'R', 'ON', 'ON', 'R', 'R'])], [('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'L', 'R', 'L', 'R', 'L']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'R', 'L', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'R', 'L', 'L', 'R', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'L', 'ON', 'ON', 'R', 'ON'], [[1, 7], [2, 4]], 1, 'L'], ['ON', 'R', 'R', 'L', 'R', 'ON', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['R', 'AN', 'ON', 'AN', 'L', 'L', 'AN'], [], 0, 'L'], ['R', 'AN', 'ON', 'AN', 'L', 'L', 'AN']), ('control layout', [['EN', 'ON', 'ON', 'ON', 'ON', 'R', 'L', 'ON', 'L', 'R'], [[1, 7], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'R', 'L', 'L', 'L', 'R'])], [('regression: pair processing order', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L'], [[1, 6], [2, 4]], 1, 'L'], ['L', 'R', 'R', 'L', 'R', 'R', 'R', 'L']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 7], [2, 5]], 0, 'R'], ['EN', 'L', 'L', 'AN', 'ON', 'L', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'EN', 'AN', 'R', 'EN', 'R', 'R', 'AN', 'EN', 'L'], [], 2, 'L'], ['L', 'EN', 'AN', 'R', 'EN', 'R', 'R', 'AN', 'EN', 'L']), ('control layout', [['R', 'L', 'ON', 'ON', 'ON', 'EN', 'R', 'AN', 'L', 'ON'], [[2, 3]], 1, 'R'], ['R', 'L', 'ON', 'ON', 'ON', 'EN', 'R', 'AN', 'L', 'ON'])], [('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('regression: pair processing order', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['R', 'ON', 'ON', 'ON', 'AN', 'ON', 'L', 'EN', 'ON', 'R'], [[1, 8], [2, 5]], 0, 'L'], ['R', 'L', 'L', 'ON', 'AN', 'L', 'L', 'EN', 'L', 'R']), ('regression: pair processing order', [['EN', 'ON', 'ON', 'AN', 'ON', 'ON', 'L', 'ON', 'L', 'L'], [[1, 7], [2, 4]], 2, 'R'], ['EN', 'L', 'L', 'AN', 'L', 'ON', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['R', 'ON', 'ON', 'AN', 'R', 'ON', 'ON', 'L', 'R'], [[1, 6], [2, 5]], 0, 'L'], ['R', 'R', 'R', 'AN', 'R', 'R', 'R', 'L', 'R']), ('control layout', [['AN', 'R', 'EN', 'ON', 'EN', 'ON', 'EN', 'ON', 'ON', 'ON', 'R'], [[5, 7]], 1, 'R'], ['AN', 'R', 'EN', 'ON', 'EN', 'R', 'EN', 'R', 'ON', 'ON', 'R'])], [('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'ON', 'R'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'L', 'R', 'L', 'R']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('regression: pair processing order', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'ON'], [[1, 7], [2, 4]], 2, 'R'], ['ON', 'L', 'L', 'R', 'L', 'ON', 'L', 'L']), ('regression: pair processing order', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 2, 'R'], ['R', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'ON', 'ON', 'AN', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 2, 'L'], ['L', 'L', 'L', 'AN', 'L', 'L', 'L']), ('control layout', [['R', 'L', 'R', 'AN', 'AN', 'R', 'ON', 'L', 'ON', 'L', 'ON'], [], 0, 'L'], ['R', 'L', 'R', 'AN', 'AN', 'R', 'ON', 'L', 'ON', 'L', 'ON'])]]
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 brackets resolve before their enclosing pair.

Input [classes, bracket pairs, embedding level, sos]. Process pairs in opener order. Strong types inside (EN/AN count as R) matching the embedding direction set both brackets to it; if only the opposite direction is inside, use the first strong type before the opener (checking already-updated brackets; sos if none); with no strong type inside leave them. Return classes.

split: open-access


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

N = 1
observations = []
def solve(x):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in sorted(pairs):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            inside = [e]
        if e in inside:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'ON'], [[1, 8], [2, 5]], 0, 'L'], ['ON', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'L']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'L', 'EN', 'EN', 'ON', 'R'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'ON', 'L', 'EN', 'EN', 'ON', 'R']), ('regression: empty pair handling', [['R', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L', 'R'], [[3, 4]], 0, 'R'], ['R', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L', 'R']), ('regression: empty pair handling', [['EN', 'ON', 'L', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', 'ON', 'R', 'ON'], [[3, 4]], 1, 'L'], ['EN', 'ON', 'L', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', 'ON', 'R', 'ON']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'L'], [[1, 5], [2, 4]], 0, 'L'], ['ON', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('control layout', [['AN', 'EN', 'ON', 'R', 'ON', 'L', 'AN', 'L', 'R', 'AN', 'EN'], [], 2, 'R'], ['AN', 'EN', 'ON', 'R', 'ON', 'L', 'AN', 'L', 'R', 'AN', 'EN'])], [('regression: empty pair handling', [['R', 'ON', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R'], [[1, 6], [2, 4]], 0, 'R'], ['R', 'R', 'ON', 'ON', 'ON', 'R', 'R', 'L', 'R']), ('regression: empty pair handling', [['EN', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'L', 'L', 'L']), ('regression: empty pair handling', [['L', 'L', 'ON', 'R', 'R', 'AN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON'], [[9, 10]], 0, 'L'], ['L', 'L', 'ON', 'R', 'R', 'AN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON']), ('regression: empty pair handling', [['R', 'L', 'R', 'EN', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'EN', 'AN', 'R'], [[4, 5]], 2, 'R'], ['R', 'L', 'R', 'EN', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'EN', 'AN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'ON', 'L', 'L'], [[1, 8], [2, 5]], 1, 'R'], ['ON', 'R', 'R', 'ON', 'L', 'R', 'R', 'ON', 'R', 'L', 'L']), ('control layout', [['EN', 'L', 'ON', 'R', 'ON', 'AN', 'ON', 'L', 'R', 'ON', 'R', 'AN', 'R', 'R'], [[4, 6]], 1, 'R'], ['EN', 'L', 'ON', 'R', 'R', 'AN', 'R', 'L', 'R', 'ON', 'R', 'AN', 'R', 'R'])], [('regression: empty pair handling', [['ON', 'AN', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'R', 'R', 'AN', 'EN', 'L'], [[6, 7]], 2, 'R'], ['ON', 'AN', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'R', 'R', 'AN', 'EN', 'L']), ('regression: empty pair handling', [['L', 'L', 'L', 'R', 'AN', 'R', 'L', 'ON', 'ON', 'L', 'EN', 'L', 'ON'], [[7, 8]], 1, 'L'], ['L', 'L', 'L', 'R', 'AN', 'R', 'L', 'ON', 'ON', 'L', 'EN', 'L', 'ON']), ('regression: empty pair handling', [['ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'R', 'AN', 'AN', 'L', 'AN', 'L', 'ON'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'R', 'AN', 'AN', 'L', 'AN', 'L', 'ON']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 7], [2, 5]], 2, 'L'], ['ON', 'L', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['L', 'ON', 'L', 'ON', 'R', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'EN'], [[3, 6]], 2, 'R'], ['L', 'ON', 'L', 'L', 'R', 'L', 'L', 'AN', 'ON', 'R', 'ON', 'EN']), ('control layout', [['L', 'ON', 'ON', 'R', 'ON', 'L', 'L', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 0, 'L'], ['L', 'L', 'L', 'R', 'L', 'L', 'L', 'L', 'R', 'L'])], [('regression: empty pair handling', [['L', 'L', 'ON', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'AN', 'R', 'EN'], [[6, 7]], 0, 'R'], ['L', 'L', 'ON', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'AN', 'R', 'EN']), ('regression: empty pair handling', [['ON', 'ON', 'EN', 'L', 'L', 'R', 'ON', 'L', 'ON', 'ON'], [[0, 1]], 1, 'L'], ['ON', 'ON', 'EN', 'L', 'L', 'R', 'ON', 'L', 'ON', 'ON']), ('regression: empty pair handling', [['ON', 'R', 'L', 'ON', 'L', 'R', 'R', 'ON', 'ON', 'R', 'R', 'ON', 'AN', 'AN'], [[7, 8]], 1, 'R'], ['ON', 'R', 'L', 'ON', 'L', 'R', 'R', 'ON', 'ON', 'R', 'R', 'ON', 'AN', 'AN']), ('regression: empty pair handling', [['L', 'L', 'EN', 'EN', 'AN', 'R', 'ON', 'ON', 'EN', 'ON', 'ON'], [[9, 10]], 0, 'R'], ['L', 'L', 'EN', 'EN', 'AN', 'R', 'ON', 'ON', 'EN', 'ON', 'ON']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'ON', 'AN'], [], 0, 'L'], ['L', 'ON', 'AN']), ('control layout', [['L', 'ON', 'ON', 'AN', 'AN', 'ON', 'ON'], [[1, 6], [2, 5]], 0, 'R'], ['L', 'L', 'L', 'AN', 'AN', 'L', 'L'])], [('regression: empty pair handling', [['EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'R'], [[1, 6], [2, 4]], 0, 'R'], ['EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'R']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'L'], [[1, 2]], 1, 'R'], ['ON', 'ON', 'ON', 'ON', 'ON', 'L']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R'], [[5, 6]], 0, 'R'], ['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R']), ('regression: empty pair handling', [['ON', 'ON', 'AN', 'L', 'ON', 'R', 'ON', 'L', 'EN', 'R', 'L'], [[0, 1], [4, 6]], 1, 'L'], ['ON', 'ON', 'AN', 'L', 'R', 'R', 'R', 'L', 'EN', 'R', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['ON', 'EN', 'R', 'L', 'ON', 'ON', 'L', 'ON', 'R'], [[0, 4]], 2, 'L'], ['L', 'EN', 'R', 'L', 'L', 'ON', 'L', 'ON', 'R']), ('control layout', [['L', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'ON', 'R', 'R'], [[1, 7], [2, 4]], 1, 'L'], ['L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', '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):
    types, pairs, level, sos = x
    t = list(types)
    e = 'L' if level % 2 == 0 else 'R'
    def dirof(c):
        if c == 'L':
            return 'L'
        if c in ('R', 'EN', 'AN'):
            return 'R'
        return None
    for o, c in sorted(pairs):
        inside = [dirof(t[k]) for k in range(o + 1, c)]
        inside = [d for d in inside if d]
        if not inside:
            inside = [sos]
        if e in inside:
            new = e
        else:
            ctx = sos
            for k in range(o - 1, -1, -1):
                d = dirof(t[k])
                if d:
                    ctx = d
                    break
            new = ctx
        t[o] = new
        t[c] = new
    return t
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'ON'], [[1, 8], [2, 5]], 0, 'L'], ['ON', 'L', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'L']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'L', 'EN', 'EN', 'ON', 'R'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'ON', 'L', 'EN', 'EN', 'ON', 'R']), ('regression: empty pair handling', [['R', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L', 'R'], [[3, 4]], 0, 'R'], ['R', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'L', 'R']), ('regression: empty pair handling', [['EN', 'ON', 'L', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', 'ON', 'R', 'ON'], [[3, 4]], 1, 'L'], ['EN', 'ON', 'L', 'ON', 'ON', 'EN', 'L', 'ON', 'ON', 'ON', 'ON', 'R', 'ON']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['ON', 'ON', 'ON', 'R', 'ON', 'ON', 'L', 'L'], [[1, 5], [2, 4]], 0, 'L'], ['ON', 'L', 'L', 'R', 'L', 'L', 'L', 'L']), ('control layout', [['AN', 'EN', 'ON', 'R', 'ON', 'L', 'AN', 'L', 'R', 'AN', 'EN'], [], 2, 'R'], ['AN', 'EN', 'ON', 'R', 'ON', 'L', 'AN', 'L', 'R', 'AN', 'EN'])], [('regression: empty pair handling', [['R', 'ON', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R'], [[1, 6], [2, 4]], 0, 'R'], ['R', 'R', 'ON', 'ON', 'ON', 'R', 'R', 'L', 'R']), ('regression: empty pair handling', [['EN', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 6], [2, 4]], 0, 'R'], ['EN', 'L', 'ON', 'ON', 'ON', 'L', 'L', 'L']), ('regression: empty pair handling', [['L', 'L', 'ON', 'R', 'R', 'AN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON'], [[9, 10]], 0, 'L'], ['L', 'L', 'ON', 'R', 'R', 'AN', 'ON', 'ON', 'R', 'ON', 'ON', 'ON']), ('regression: empty pair handling', [['R', 'L', 'R', 'EN', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'EN', 'AN', 'R'], [[4, 5]], 2, 'R'], ['R', 'L', 'R', 'EN', 'ON', 'ON', 'ON', 'R', 'ON', 'L', 'R', 'EN', 'AN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('control layout', [['ON', 'ON', 'ON', 'ON', 'L', 'ON', 'R', 'ON', 'ON', 'L', 'L'], [[1, 8], [2, 5]], 1, 'R'], ['ON', 'R', 'R', 'ON', 'L', 'R', 'R', 'ON', 'R', 'L', 'L']), ('control layout', [['EN', 'L', 'ON', 'R', 'ON', 'AN', 'ON', 'L', 'R', 'ON', 'R', 'AN', 'R', 'R'], [[4, 6]], 1, 'R'], ['EN', 'L', 'ON', 'R', 'R', 'AN', 'R', 'L', 'R', 'ON', 'R', 'AN', 'R', 'R'])], [('regression: empty pair handling', [['ON', 'AN', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'R', 'R', 'AN', 'EN', 'L'], [[6, 7]], 2, 'R'], ['ON', 'AN', 'ON', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'R', 'R', 'AN', 'EN', 'L']), ('regression: empty pair handling', [['L', 'L', 'L', 'R', 'AN', 'R', 'L', 'ON', 'ON', 'L', 'EN', 'L', 'ON'], [[7, 8]], 1, 'L'], ['L', 'L', 'L', 'R', 'AN', 'R', 'L', 'ON', 'ON', 'L', 'EN', 'L', 'ON']), ('regression: empty pair handling', [['ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'R', 'AN', 'AN', 'L', 'AN', 'L', 'ON'], [[0, 1]], 2, 'L'], ['ON', 'ON', 'R', 'ON', 'EN', 'L', 'ON', 'R', 'AN', 'AN', 'L', 'AN', 'L', 'ON']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'L', 'ON', 'L'], [[1, 7], [2, 5]], 2, 'L'], ['ON', 'L', 'ON', 'ON', 'ON', 'ON', 'L', 'L', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['L', 'ON', 'L', 'ON', 'R', 'L', 'ON', 'AN', 'ON', 'R', 'ON', 'EN'], [[3, 6]], 2, 'R'], ['L', 'ON', 'L', 'L', 'R', 'L', 'L', 'AN', 'ON', 'R', 'ON', 'EN']), ('control layout', [['L', 'ON', 'ON', 'R', 'ON', 'L', 'L', 'ON', 'R', 'L'], [[1, 7], [2, 4]], 0, 'L'], ['L', 'L', 'L', 'R', 'L', 'L', 'L', 'L', 'R', 'L'])], [('regression: empty pair handling', [['L', 'L', 'ON', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'AN', 'R', 'EN'], [[6, 7]], 0, 'R'], ['L', 'L', 'ON', 'R', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'AN', 'R', 'EN']), ('regression: empty pair handling', [['ON', 'ON', 'EN', 'L', 'L', 'R', 'ON', 'L', 'ON', 'ON'], [[0, 1]], 1, 'L'], ['ON', 'ON', 'EN', 'L', 'L', 'R', 'ON', 'L', 'ON', 'ON']), ('regression: empty pair handling', [['ON', 'R', 'L', 'ON', 'L', 'R', 'R', 'ON', 'ON', 'R', 'R', 'ON', 'AN', 'AN'], [[7, 8]], 1, 'R'], ['ON', 'R', 'L', 'ON', 'L', 'R', 'R', 'ON', 'ON', 'R', 'R', 'ON', 'AN', 'AN']), ('regression: empty pair handling', [['L', 'L', 'EN', 'EN', 'AN', 'R', 'ON', 'ON', 'EN', 'ON', 'ON'], [[9, 10]], 0, 'R'], ['L', 'L', 'EN', 'EN', 'AN', 'R', 'ON', 'ON', 'EN', 'ON', 'ON']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('nested pair uses updated outer bracket', [['R', 'ON', 'ON', 'R', 'ON', 'L', 'ON'], [[1, 6], [2, 4]], 0, 'L'], ['R', 'L', 'L', 'R', 'L', 'L', 'L']), ('control layout', [['L', 'ON', 'AN'], [], 0, 'L'], ['L', 'ON', 'AN']), ('control layout', [['L', 'ON', 'ON', 'AN', 'AN', 'ON', 'ON'], [[1, 6], [2, 5]], 0, 'R'], ['L', 'L', 'L', 'AN', 'AN', 'L', 'L'])], [('regression: empty pair handling', [['EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'R'], [[1, 6], [2, 4]], 0, 'R'], ['EN', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R', 'R']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'L'], [[1, 2]], 1, 'R'], ['ON', 'ON', 'ON', 'ON', 'ON', 'L']), ('regression: empty pair handling', [['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R'], [[5, 6]], 0, 'R'], ['ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'ON', 'R']), ('regression: empty pair handling', [['ON', 'ON', 'AN', 'L', 'ON', 'R', 'ON', 'L', 'EN', 'R', 'L'], [[0, 1], [4, 6]], 1, 'L'], ['ON', 'ON', 'AN', 'L', 'R', 'R', 'R', 'L', 'EN', 'R', 'L']), ('nested pair in RTL embedding', [['L', 'ON', 'ON', 'L', 'ON', 'R', 'ON'], [[1, 6], [2, 4]], 1, 'R'], ['L', 'R', 'R', 'L', 'R', 'R', 'R']), ('number inside brackets in RTL context', [['R', 'ON', 'EN', 'ON'], [[1, 3]], 0, 'L'], ['R', 'R', 'EN', 'R']), ('control layout', [['ON', 'EN', 'R', 'L', 'ON', 'ON', 'L', 'ON', 'R'], [[0, 4]], 2, 'L'], ['L', 'EN', 'R', 'L', 'L', 'ON', 'L', 'ON', 'R']), ('control layout', [['L', 'ON', 'ON', 'L', 'ON', 'ON', 'L', 'ON', 'R', 'R'], [[1, 7], [2, 4]], 1, 'L'], ['L', 'L', 'L', 'L', 'L', 'ON', 'L', 'L', '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:
Brackets with no strong content are forced to the embedding direction.

Input [classes, bracket pairs, embedding level, sos]. Process pairs in opener order. Strong types inside (EN/AN count as R) matching the embedding direction set both brackets to it; if only the opposite direction is inside, use the first strong type before the opener (checking already-updated brackets; sos if none); with no strong type inside leave them. Return classes.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] if rtl else pos[j] + 1
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('regression: leading edge side', [[1, 1, 1, 0], 0, 'after'], 3), ('regression: leading edge side', [[2, 0], 0, 'after'], 0), ('regression: leading edge side', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[3, 3, 3, 2, 2, 3, 3, 3, 3, 2], 9, 'before'], 5), ('control layout', [[2, 2, 2, 2, 1, 1, 3], 3, 'before'], 6)], [('regression: leading edge side', [[1, 1, 1, 1, 0, 1, 1, 1], 4, 'after'], 4), ('regression: leading edge side', [[4, 3, 3, 3], 3, 'after'], 1), ('regression: leading edge side', [[2, 2, 2, 2, 2, 2, 2], 2, 'after'], 2), ('regression: leading edge side', [[4], 0, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[1, 1, 1, 1, 3, 3, 1, 1], 1, 'before'], 7), ('control layout', [[0, 0, 1, 1, 2, 2, 2, 0, 0], 6, 'before'], 4)], [('regression: leading edge side', [[2, 2, 2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('regression: leading edge side', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: leading edge side', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('regression: leading edge side', [[3, 2, 2, 2, 1, 1, 3, 3, 3], 1, 'after'], 6), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 1, 1, 3, 3, 3], 4, 'before'], 3), ('control layout', [[0, 0, 0, 1, 3, 3, 3, 3], 6, 'before'], 5)], [('regression: leading edge side', [[3, 3, 3, 3, 1, 1, 1], 6, 'after'], 1), ('regression: leading edge side', [[1, 1], 1, 'after'], 1), ('regression: leading edge side', [[2, 2, 3, 3, 3, 1, 1, 3], 0, 'after'], 3), ('regression: leading edge side', [[4, 4, 4, 4, 2], 1, 'after'], 1), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[2, 2, 2, 3, 3, 2, 2, 2, 3, 3], 10, 'after'], 8), ('control layout', [[1, 1, 1, 1, 1, 0, 2, 2], 4, 'before'], 1)], [('regression: leading edge side', [[2, 2, 2, 4, 4, 4, 4, 3, 3], 4, 'after'], 6), ('regression: leading edge side', [[1, 1, 1, 1, 1], 2, 'after'], 3), ('regression: leading edge side', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: leading edge side', [[3, 3, 2, 2, 2, 2, 2, 2, 1, 1], 7, 'after'], 9), ('line end', [[1, 1, 1], 3, 'after'], 0), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[2, 3, 4, 1, 1, 1, 1, 2], 5, 'before'], 3), ('control layout', [[1, 1, 1, 3, 3, 3, 3, 1, 1, 1], 9, 'before'], 1)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('regression: leading edge side', [[1, 1, 1, 0], 0, 'after'], 3), ('regression: leading edge side', [[2, 0], 0, 'after'], 0), ('regression: leading edge side', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[3, 3, 3, 2, 2, 3, 3, 3, 3, 2], 9, 'before'], 5), ('control layout', [[2, 2, 2, 2, 1, 1, 3], 3, 'before'], 6)], [('regression: leading edge side', [[1, 1, 1, 1, 0, 1, 1, 1], 4, 'after'], 4), ('regression: leading edge side', [[4, 3, 3, 3], 3, 'after'], 1), ('regression: leading edge side', [[2, 2, 2, 2, 2, 2, 2], 2, 'after'], 2), ('regression: leading edge side', [[4], 0, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[1, 1, 1, 1, 3, 3, 1, 1], 1, 'before'], 7), ('control layout', [[0, 0, 1, 1, 2, 2, 2, 0, 0], 6, 'before'], 4)], [('regression: leading edge side', [[2, 2, 2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('regression: leading edge side', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: leading edge side', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('regression: leading edge side', [[3, 2, 2, 2, 1, 1, 3, 3, 3], 1, 'after'], 6), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 1, 1, 3, 3, 3], 4, 'before'], 3), ('control layout', [[0, 0, 0, 1, 3, 3, 3, 3], 6, 'before'], 5)], [('regression: leading edge side', [[3, 3, 3, 3, 1, 1, 1], 6, 'after'], 1), ('regression: leading edge side', [[1, 1], 1, 'after'], 1), ('regression: leading edge side', [[2, 2, 3, 3, 3, 1, 1, 3], 0, 'after'], 3), ('regression: leading edge side', [[4, 4, 4, 4, 2], 1, 'after'], 1), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[2, 2, 2, 3, 3, 2, 2, 2, 3, 3], 10, 'after'], 8), ('control layout', [[1, 1, 1, 1, 1, 0, 2, 2], 4, 'before'], 1)], [('regression: leading edge side', [[2, 2, 2, 4, 4, 4, 4, 3, 3], 4, 'after'], 6), ('regression: leading edge side', [[1, 1, 1, 1, 1], 2, 'after'], 3), ('regression: leading edge side', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: leading edge side', [[3, 3, 2, 2, 2, 2, 2, 2, 1, 1], 7, 'after'], 9), ('line end', [[1, 1, 1], 3, 'after'], 0), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[2, 3, 4, 1, 1, 1, 1, 2], 5, 'before'], 3), ('control layout', [[1, 1, 1, 3, 3, 3, 3, 1, 1, 1], 9, 'before'], 1)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Carets attached to RTL characters appear on the wrong side.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('line end', [[1, 1, 1], 3, 'after'], 0), ('regression: trailing edge side', [[3, 3, 3, 3, 3, 3, 1, 1], 7, 'before'], 1), ('regression: trailing edge side', [[1, 1, 1, 1, 1, 1, 0, 0, 0, 0], 6, 'before'], 0), ('regression: trailing edge side', [[2, 2, 2, 3, 3, 2, 2, 2, 3, 3], 10, 'after'], 8), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[2, 2], 2, 'after'], 2), ('control layout', [[1, 2, 2, 1], 3, 'after'], 1)], [('regression: trailing edge side', [[1, 1, 1, 1, 3, 3, 1, 1], 1, 'before'], 7), ('regression: trailing edge side', [[3, 3, 3, 3, 2, 2, 2, 0, 0], 2, 'before'], 2), ('regression: trailing edge side', [[3, 3, 3, 3, 1, 4, 4, 1, 2, 1], 10, 'after'], 0), ('regression: trailing edge side', [[2, 1], 2, 'after'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[0], 1, 'before'], 1), ('control layout', [[3, 3, 3, 1, 1, 0, 0, 0, 0, 0], 5, 'after'], 5)], [('regression: trailing edge side', [[1, 1, 1, 1, 1, 1], 2, 'before'], 4), ('regression: trailing edge side', [[0, 0, 0, 1, 3, 3, 3, 3], 6, 'before'], 5), ('regression: trailing edge side', [[2, 1, 1, 1, 1], 5, 'after'], 0), ('regression: trailing edge side', [[2, 2, 2, 2, 3, 3, 3, 1, 1, 1], 10, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1, 1, 1, 1, 1, 1], 1, 'after'], 6), ('control layout', [[3, 3, 3], 0, 'after'], 3)], [('regression: trailing edge side', [[3, 3, 3, 3], 1, 'before'], 3), ('regression: trailing edge side', [[3, 3, 3, 2, 2, 3, 3, 3, 3, 2], 9, 'before'], 5), ('regression: trailing edge side', [[1, 3, 3], 3, 'before'], 0), ('regression: trailing edge side', [[2, 2, 1, 1, 3, 2, 2, 3], 4, 'before'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[0, 0, 3, 0, 2], 3, 'after'], 3), ('control layout', [[3, 3, 0, 0, 0], 3, 'before'], 3)], [('regression: trailing edge side', [[0, 1, 1, 2, 2, 2, 3, 3, 3, 1], 3, 'before'], 8), ('regression: trailing edge side', [[1], 1, 'after'], 0), ('regression: trailing edge side', [[1, 3, 3, 1, 1, 1, 1, 1], 2, 'before'], 6), ('regression: trailing edge side', [[1, 1, 2, 2, 1, 1, 1], 2, 'before'], 5), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[1, 1, 1, 1, 0, 1, 1, 1, 1], 0, 'before'], 4), ('control layout', [[3, 3, 0, 0, 0], 5, 'after'], 5)]]
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):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] - 1 if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('line end', [[1, 1, 1], 3, 'after'], 0), ('regression: trailing edge side', [[3, 3, 3, 3, 3, 3, 1, 1], 7, 'before'], 1), ('regression: trailing edge side', [[1, 1, 1, 1, 1, 1, 0, 0, 0, 0], 6, 'before'], 0), ('regression: trailing edge side', [[2, 2, 2, 3, 3, 2, 2, 2, 3, 3], 10, 'after'], 8), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[2, 2], 2, 'after'], 2), ('control layout', [[1, 2, 2, 1], 3, 'after'], 1)], [('regression: trailing edge side', [[1, 1, 1, 1, 3, 3, 1, 1], 1, 'before'], 7), ('regression: trailing edge side', [[3, 3, 3, 3, 2, 2, 2, 0, 0], 2, 'before'], 2), ('regression: trailing edge side', [[3, 3, 3, 3, 1, 4, 4, 1, 2, 1], 10, 'after'], 0), ('regression: trailing edge side', [[2, 1], 2, 'after'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[0], 1, 'before'], 1), ('control layout', [[3, 3, 3, 1, 1, 0, 0, 0, 0, 0], 5, 'after'], 5)], [('regression: trailing edge side', [[1, 1, 1, 1, 1, 1], 2, 'before'], 4), ('regression: trailing edge side', [[0, 0, 0, 1, 3, 3, 3, 3], 6, 'before'], 5), ('regression: trailing edge side', [[2, 1, 1, 1, 1], 5, 'after'], 0), ('regression: trailing edge side', [[2, 2, 2, 2, 3, 3, 3, 1, 1, 1], 10, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1, 1, 1, 1, 1, 1], 1, 'after'], 6), ('control layout', [[3, 3, 3], 0, 'after'], 3)], [('regression: trailing edge side', [[3, 3, 3, 3], 1, 'before'], 3), ('regression: trailing edge side', [[3, 3, 3, 2, 2, 3, 3, 3, 3, 2], 9, 'before'], 5), ('regression: trailing edge side', [[1, 3, 3], 3, 'before'], 0), ('regression: trailing edge side', [[2, 2, 1, 1, 3, 2, 2, 3], 4, 'before'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[0, 0, 3, 0, 2], 3, 'after'], 3), ('control layout', [[3, 3, 0, 0, 0], 3, 'before'], 3)], [('regression: trailing edge side', [[0, 1, 1, 2, 2, 2, 3, 3, 3, 1], 3, 'before'], 8), ('regression: trailing edge side', [[1], 1, 'after'], 0), ('regression: trailing edge side', [[1, 3, 3, 1, 1, 1, 1, 1], 2, 'before'], 6), ('regression: trailing edge side', [[1, 1, 2, 2, 1, 1, 1], 2, 'before'], 5), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[1, 1, 1, 1, 0, 1, 1, 1, 1], 0, 'before'], 4), ('control layout', [[3, 3, 0, 0, 0], 5, 'after'], 5)]]
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:
Carets after RTL characters are drawn past them.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n - 1 or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: affinity at line end', [[1, 2, 2, 1], 3, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[2, 2, 2, 3, 3, 3, 0, 0], 3, 'after'], 6), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1], 1, 'after'], 1), ('control layout', [[1, 4, 4, 4], 3, 'after'], 2)], [('regression: affinity at line end', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2], 9, 'after'], 0), ('regression: affinity at line end', [[2, 2, 1, 1, 2, 1], 5, 'after'], 1), ('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[2], 1, 'before'], 1), ('control layout', [[1, 1, 0, 0, 1], 2, 'after'], 2)], [('regression: affinity at line end', [[2, 2, 1, 1, 2, 1], 5, 'after'], 1), ('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('partial-repair probe', [[0, 0, 0, 1, 0, 0, 0], 3, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 0, 0], 0, 'after'], 1), ('control layout', [[2, 2], 0, 'before'], 0)], [('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: affinity at line end', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2], 9, 'after'], 0), ('partial-repair probe', [[2, 2, 2, 2, 3, 3, 2, 3, 3], 4, 'after'], 6), ('partial-repair probe', [[3, 3, 0, 3, 3, 3, 3, 0], 3, 'after'], 7), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('control layout', [[1, 1, 1, 1, 1, 1], 2, 'before'], 4)], [('regression: affinity at line end', [[3, 3, 2, 2, 3, 3, 3, 0], 7, 'after'], 7), ('regression: affinity at line end', [[1, 2, 2, 1], 3, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[2, 2, 1, 1, 1, 1, 2, 2, 2], 2, 'after'], 7), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 3, 3, 1, 1, 1, 1, 1], 2, 'before'], 6)]]
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):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n and levels[k] % 2 == 0 or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: affinity at line end', [[1, 2, 2, 1], 3, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[2, 2, 2, 3, 3, 3, 0, 0], 3, 'after'], 6), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1], 1, 'after'], 1), ('control layout', [[1, 4, 4, 4], 3, 'after'], 2)], [('regression: affinity at line end', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2], 9, 'after'], 0), ('regression: affinity at line end', [[2, 2, 1, 1, 2, 1], 5, 'after'], 1), ('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[2], 1, 'before'], 1), ('control layout', [[1, 1, 0, 0, 1], 2, 'after'], 2)], [('regression: affinity at line end', [[2, 2, 1, 1, 2, 1], 5, 'after'], 1), ('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('partial-repair probe', [[0, 0, 0, 1, 0, 0, 0], 3, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 0, 0], 0, 'after'], 1), ('control layout', [[2, 2], 0, 'before'], 0)], [('regression: affinity at line end', [[2, 2, 2, 2, 1], 4, 'after'], 1), ('regression: affinity at line end', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 2], 9, 'after'], 0), ('partial-repair probe', [[2, 2, 2, 2, 3, 3, 2, 3, 3], 4, 'after'], 6), ('partial-repair probe', [[3, 3, 0, 3, 3, 3, 3, 0], 3, 'after'], 7), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('control layout', [[1, 1, 1, 1, 1, 1], 2, 'before'], 4)], [('regression: affinity at line end', [[3, 3, 2, 2, 3, 3, 3, 0], 7, 'after'], 7), ('regression: affinity at line end', [[1, 2, 2, 1], 3, 'after'], 1), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[2, 2, 1, 1, 1, 1, 2, 2, 2], 2, 'after'], 7), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 3, 3, 1, 1, 1, 1, 1], 2, 'before'], 6)]]
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:
A caret before the last character jumps to the previous character.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[v] = i
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: position inverse map', [[2, 2, 2, 4, 4, 4, 4, 1, 1], 7, 'before'], 9), ('regression: position inverse map', [[2, 2, 2, 2, 2, 2, 2, 1], 2, 'after'], 3), ('regression: position inverse map', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: position inverse map', [[1, 2, 2], 1, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('control layout', [[4], 1, 'before'], 1)], [('regression: position inverse map', [[2, 2, 2, 2, 2, 2, 2, 2, 1], 6, 'after'], 7), ('regression: position inverse map', [[1, 1, 1, 1, 1, 2, 2, 2, 2, 1], 6, 'before'], 2), ('partial-repair probe', [[0, 0, 0, 3, 1, 1, 1, 1, 1, 1], 5, 'before'], 8), ('partial-repair probe', [[1, 1, 0, 0, 0, 0, 0, 0], 5, 'before'], 5), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3], 1, 'before'], 0), ('control layout', [[1], 1, 'before'], 0)], [('regression: position inverse map', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('regression: position inverse map', [[3, 3, 2, 2, 2, 2, 2, 2, 1, 1], 7, 'after'], 9), ('partial-repair probe', [[2, 2, 2, 2, 4, 2], 4, 'before'], 4), ('partial-repair probe', [[1, 1, 1, 1, 0, 0, 0, 0, 0, 2], 2, 'after'], 2), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[1, 2, 1, 1, 3, 3, 3], 3, 'after'], 4), ('control layout', [[3], 1, 'before'], 0)], [('regression: position inverse map', [[1, 1, 1, 1, 2, 2], 5, 'before'], 1), ('regression: position inverse map', [[2, 2, 1, 1, 1, 1, 2, 2, 2], 2, 'after'], 7), ('partial-repair probe', [[3, 3, 3, 0, 0, 0, 3], 5, 'after'], 5), ('partial-repair probe', [[3, 3, 3, 3, 3, 3, 3], 0, 'before'], 7), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1], 0, 'before'], 1), ('control layout', [[2, 2, 1, 1, 1], 3, 'before'], 2)], [('regression: position inverse map', [[4, 4, 3, 3, 3, 3, 2, 2, 2], 2, 'before'], 6), ('regression: position inverse map', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: position inverse map', [[1, 1, 1, 2, 2, 2, 2, 2, 2, 1], 9, 'before'], 7), ('regression: position inverse map', [[1, 1, 1, 1, 2, 2], 5, 'before'], 1), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1], 0, 'before'], 1), ('control layout', [[1, 1, 0], 1, 'before'], 1)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = n - 1 - v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: position inverse map', [[2, 2, 2, 4, 4, 4, 4, 1, 1], 7, 'before'], 9), ('regression: position inverse map', [[2, 2, 2, 2, 2, 2, 2, 1], 2, 'after'], 3), ('regression: position inverse map', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: position inverse map', [[1, 2, 2], 1, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('control layout', [[4], 1, 'before'], 1)], [('regression: position inverse map', [[2, 2, 2, 2, 2, 2, 2, 2, 1], 6, 'after'], 7), ('regression: position inverse map', [[1, 1, 1, 1, 1, 2, 2, 2, 2, 1], 6, 'before'], 2), ('partial-repair probe', [[0, 0, 0, 3, 1, 1, 1, 1, 1, 1], 5, 'before'], 8), ('partial-repair probe', [[1, 1, 0, 0, 0, 0, 0, 0], 5, 'before'], 5), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3], 1, 'before'], 0), ('control layout', [[1], 1, 'before'], 0)], [('regression: position inverse map', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('regression: position inverse map', [[3, 3, 2, 2, 2, 2, 2, 2, 1, 1], 7, 'after'], 9), ('partial-repair probe', [[2, 2, 2, 2, 4, 2], 4, 'before'], 4), ('partial-repair probe', [[1, 1, 1, 1, 0, 0, 0, 0, 0, 2], 2, 'after'], 2), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[1, 2, 1, 1, 3, 3, 3], 3, 'after'], 4), ('control layout', [[3], 1, 'before'], 0)], [('regression: position inverse map', [[1, 1, 1, 1, 2, 2], 5, 'before'], 1), ('regression: position inverse map', [[2, 2, 1, 1, 1, 1, 2, 2, 2], 2, 'after'], 7), ('partial-repair probe', [[3, 3, 3, 0, 0, 0, 3], 5, 'after'], 5), ('partial-repair probe', [[3, 3, 3, 3, 3, 3, 3], 0, 'before'], 7), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1], 0, 'before'], 1), ('control layout', [[2, 2, 1, 1, 1], 3, 'before'], 2)], [('regression: position inverse map', [[4, 4, 3, 3, 3, 3, 2, 2, 2], 2, 'before'], 6), ('regression: position inverse map', [[2, 2, 1, 1], 0, 'after'], 2), ('regression: position inverse map', [[1, 1, 1, 2, 2, 2, 2, 2, 2, 1], 9, 'before'], 7), ('regression: position inverse map', [[1, 1, 1, 1, 2, 2], 5, 'before'], 1), ('line end', [[1, 1, 1], 3, 'after'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1], 0, 'before'], 1), ('control layout', [[1, 1, 0], 1, 'before'], 1)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Carets are placed at the logical index instead of the visual slot.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] > 0
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: character direction test', [[3, 2, 2, 2, 1, 1, 3, 3, 3], 1, 'after'], 6), ('regression: character direction test', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 10, 'after'], 9), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 2, 2, 1, 1, 1], 2, 'before'], 5), ('control layout', [[1, 2, 2, 2, 2, 1], 6, 'after'], 0)], [('regression: character direction test', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('regression: character direction test', [[4, 4], 2, 'before'], 2), ('partial-repair probe', [[1, 3, 3, 3, 3, 1, 0, 0], 7, 'after'], 7), ('regression: character direction test', [[1, 1, 4, 4, 4, 4, 2], 5, 'before'], 3), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3, 2, 2, 3, 3, 3, 3], 6, 'after'], 4), ('control layout', [[3, 3, 3, 0, 2], 3, 'before'], 0)], [('regression: character direction test', [[2, 1, 1, 4, 4, 4, 4, 2, 2, 2], 5, 'before'], 2), ('regression: character direction test', [[2, 2, 2], 2, 'after'], 2), ('partial-repair probe', [[1, 0, 0, 0, 0], 3, 'after'], 3), ('partial-repair probe', [[1, 0, 1, 1, 0, 0, 0, 0, 0, 0], 7, 'after'], 7), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, 'before'], 0), ('control layout', [[3, 1, 1, 1, 1, 1, 1], 6, 'after'], 1)], [('regression: character direction test', [[2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('regression: character direction test', [[2, 2, 2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('partial-repair probe', [[4, 3], 1, 'after'], 1), ('partial-repair probe', [[0, 1, 1, 1, 1, 1, 1], 2, 'before'], 6), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1, 1, 1, 0, 0, 0, 0, 0, 2], 2, 'after'], 2), ('control layout', [[1, 1, 1, 0, 0, 0, 0, 1], 1, 'after'], 2)], [('regression: character direction test', [[1, 2, 2, 2, 2], 2, 'before'], 1), ('regression: character direction test', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('partial-repair probe', [[3, 0, 2, 2, 2], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3, 3, 1, 1, 1, 1, 1, 0, 1], 3, 'after'], 4), ('control layout', [[1, 1, 1, 1], 0, 'after'], 4)]]
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):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k == 0:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[0] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: character direction test', [[3, 2, 2, 2, 1, 1, 3, 3, 3], 1, 'after'], 6), ('regression: character direction test', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('partial-repair probe', [[4, 4, 4, 4, 3, 3], 4, 'after'], 2), ('partial-repair probe', [[0, 0, 0, 0, 0, 0, 0, 0, 0, 1], 10, 'after'], 9), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[1, 1, 2, 2, 1, 1, 1], 2, 'before'], 5), ('control layout', [[1, 2, 2, 2, 2, 1], 6, 'after'], 0)], [('regression: character direction test', [[1, 3, 3, 3, 3, 2, 2], 5, 'after'], 4), ('regression: character direction test', [[4, 4], 2, 'before'], 2), ('partial-repair probe', [[1, 3, 3, 3, 3, 1, 0, 0], 7, 'after'], 7), ('regression: character direction test', [[1, 1, 4, 4, 4, 4, 2], 5, 'before'], 3), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3, 2, 2, 3, 3, 3, 3], 6, 'after'], 4), ('control layout', [[3, 3, 3, 0, 2], 3, 'before'], 0)], [('regression: character direction test', [[2, 1, 1, 4, 4, 4, 4, 2, 2, 2], 5, 'before'], 2), ('regression: character direction test', [[2, 2, 2], 2, 'after'], 2), ('partial-repair probe', [[1, 0, 0, 0, 0], 3, 'after'], 3), ('partial-repair probe', [[1, 0, 1, 1, 0, 0, 0, 0, 0, 0], 7, 'after'], 7), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 0, 'before'], 0), ('control layout', [[3, 1, 1, 1, 1, 1, 1], 6, 'after'], 1)], [('regression: character direction test', [[2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('regression: character direction test', [[2, 2, 2, 2, 2, 2, 2, 1, 1], 1, 'after'], 3), ('partial-repair probe', [[4, 3], 1, 'after'], 1), ('partial-repair probe', [[0, 1, 1, 1, 1, 1, 1], 2, 'before'], 6), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('control layout', [[1, 1, 1, 1, 0, 0, 0, 0, 0, 2], 2, 'after'], 2), ('control layout', [[1, 1, 1, 0, 0, 0, 0, 1], 1, 'after'], 2)], [('regression: character direction test', [[1, 2, 2, 2, 2], 2, 'before'], 1), ('regression: character direction test', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[3, 0, 0, 0, 2, 2, 2], 3, 'after'], 3), ('partial-repair probe', [[3, 0, 2, 2, 2], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('line end', [[1, 1, 1], 3, 'after'], 0), ('control layout', [[3, 3, 1, 1, 1, 1, 1, 0, 1], 3, 'after'], 4), ('control layout', [[1, 1, 1, 1], 0, 'after'], 4)]]
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:
Carets next to characters at level 2 are placed as if RTL.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access


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

N = 1
observations = []
def solve(x):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('line start before', [[1, 1, 0], 0, 'before'], 2), ('regression: line start caret', [[2, 3], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[3], 0, 'after'], 1), ('control layout', [[1, 1, 1, 1, 1, 1, 1, 3], 6, 'before'], 2)], [('regression: line start caret', [[2], 0, 'before'], 0), ('regression: line start caret', [[2, 2], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[3, 3, 0, 2, 0, 0], 0, 'after'], 2), ('control layout', [[1, 1, 1, 1, 3], 2, 'after'], 3)], [('regression: line start caret', [[1], 0, 'before'], 1), ('regression: line start caret', [[1], 0, 'before'], 1), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[2, 2, 2, 2, 2, 2, 2, 2, 1], 6, 'after'], 7), ('control layout', [[0, 0, 0], 2, 'after'], 2)], [('regression: line start caret', [[2, 3, 3, 3, 1, 1, 1], 0, 'before'], 3), ('regression: line start caret', [[2, 1, 1, 1], 0, 'before'], 3), ('partial-repair probe', [[2, 1, 1, 1, 1, 1, 1, 1, 2], 1, 'before'], 9), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[0, 0, 1, 1, 2, 2, 2, 0, 0], 6, 'before'], 4), ('control layout', [[1, 1, 1, 2, 2, 2, 2, 3], 3, 'before'], 5)], [('regression: line start caret', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('regression: line start caret', [[2], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[0, 1, 1, 2, 2, 2, 3, 3, 3, 1], 3, 'before'], 8), ('control layout', [[0, 0, 0], 2, 'after'], 2)]]
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):
    def reorder(lv):
        vis = list(range(len(lv)))
        odd = [l for l in lv if l % 2 == 1]
        if not odd:
            return vis
        for lev in range(max(lv), min(odd) - 1, -1):
            i = 0
            while i < len(vis):
                if lv[vis[i]] >= lev:
                    j = i
                    while j < len(vis) and lv[vis[j]] >= lev:
                        j += 1
                    vis[i:j] = vis[i:j][::-1]
                    i = j
                else:
                    i += 1
        return vis

    levels, k, affinity = x
    n = len(levels)
    if n == 0:
        return 0
    vis = reorder(levels)
    pos = [0] * n
    for v, i in enumerate(vis):
        pos[i] = v
    if affinity == 'after' and k < n or k <= 1:
        j = k
        leading = True
    else:
        j = k - 1
        leading = False
    rtl = levels[j] % 2 == 1
    if leading:
        return pos[j] + 1 if rtl else pos[j]
    return pos[j] if rtl else pos[j] + 1
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('line start before', [[1, 1, 0], 0, 'before'], 2), ('regression: line start caret', [[2, 3], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[3], 0, 'after'], 1), ('control layout', [[1, 1, 1, 1, 1, 1, 1, 3], 6, 'before'], 2)], [('regression: line start caret', [[2], 0, 'before'], 0), ('regression: line start caret', [[2, 2], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[3, 3, 0, 2, 0, 0], 0, 'after'], 2), ('control layout', [[1, 1, 1, 1, 3], 2, 'after'], 3)], [('regression: line start caret', [[1], 0, 'before'], 1), ('regression: line start caret', [[1], 0, 'before'], 1), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('partial-repair probe', [[4, 1, 1, 1, 3, 3, 1], 1, 'before'], 7), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[2, 2, 2, 2, 2, 2, 2, 2, 1], 6, 'after'], 7), ('control layout', [[0, 0, 0], 2, 'after'], 2)], [('regression: line start caret', [[2, 3, 3, 3, 1, 1, 1], 0, 'before'], 3), ('regression: line start caret', [[2, 1, 1, 1], 0, 'before'], 3), ('partial-repair probe', [[2, 1, 1, 1, 1, 1, 1, 1, 2], 1, 'before'], 9), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line start before', [[1, 1, 0], 0, 'before'], 2), ('same boundary after', [[0, 0, 1, 1], 2, 'after'], 4), ('control layout', [[0, 0, 1, 1, 2, 2, 2, 0, 0], 6, 'before'], 4), ('control layout', [[1, 1, 1, 2, 2, 2, 2, 3], 3, 'before'], 5)], [('regression: line start caret', [[2, 2, 2, 2, 2, 3, 3, 2, 1], 0, 'before'], 1), ('regression: line start caret', [[2], 0, 'before'], 0), ('partial-repair probe', [[4, 1, 4, 4], 1, 'before'], 4), ('partial-repair probe', [[1, 0], 1, 'before'], 0), ('line end', [[1, 1, 1], 3, 'after'], 0), ('caret at direction boundary', [[0, 0, 1, 1], 2, 'before'], 2), ('control layout', [[0, 1, 1, 2, 2, 2, 3, 3, 3, 1], 3, 'before'], 8), ('control layout', [[0, 0, 0], 2, 'after'], 2)]]
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:
A caret at offset 0 with backward affinity lands at the end of the line.

Input [levels, logical caret offset k, affinity]. Characters are one unit wide and visually ordered by level reversal. Affinity "after" attaches the caret to the leading edge of character k (if k < n), otherwise to the trailing edge of character k-1; offset 0 always uses the leading edge of character 0. The leading edge of an LTR character is its left side, of an RTL (odd) character its right side. Return the visual x.

split: open-access