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, classes, para, line_lengths = x
    out = []
    start = 0
    glob = reorder(levels)
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append(glob[start:start + ln])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 1, 1, 1, 3, 3, 1, 1, 0, 0, 0], ['L', 'L', 'L', 'WS', 'L', 'WS', 'R', 'R', 'ON', 'WS', 'R', 'WS', 'R', 'WS'], 0, [1, 4, 2, 3, 4]], [[0], [4, 3, 2, 1], [6, 5], [8, 7, 9], [10, 11, 12, 13]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'R', 'L', 'WS', 'ON', 'WS', 'R', 'L', 'L', 'WS', 'L', 'WS'], 0, [4, 3, 4, 2]], [[3, 2, 1, 0], [4, 5, 6], [9, 8, 7, 10], [11, 12]]), ('regression: paragraph-wide reordering', [[1, 0, 0, 0, 0, 0], ['R', 'L', 'WS', 'WS', 'WS', 'WS'], 1, [3, 2, 1]], [[0, 1, 2], [4, 3], [5]]), ('control layout', [[2, 2, 2, 0], ['WS', 'ON', 'WS', 'L'], 0, [1, 2, 1]], [[0], [1, 2], [3]]), ('control layout', [[2, 2, 2, 0, 0, 1], ['R', 'R', 'R', 'L', 'R', 'R'], 0, [3, 2, 1]], [[0, 1, 2], [3, 4], [5]]), ('control layout', [[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0], ['WS', 'L', 'R', 'L', 'R', 'L', 'WS', 'R', 'WS', 'ON', 'WS', 'L', 'R', 'WS'], 0, [5, 5, 2, 2]], [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11], [12, 13]]), ('control layout', [[1, 1, 0, 0, 0], ['WS', 'WS', 'ON', 'R', 'R'], 0, [4, 1]], [[1, 0, 2, 3], [4]])], [('regression: paragraph-wide reordering', [[4, 4, 2, 2, 2, 1, 3], ['R', 'L', 'L', 'R', 'L', 'R', 'ON'], 0, [3, 2, 2]], [[0, 1, 2], [3, 4], [6, 5]]), ('regression: paragraph-wide reordering', [[0, 0, 3, 3, 3, 1, 1, 2, 0, 0], ['ON', 'R', 'L', 'ON', 'R', 'R', 'WS', 'L', 'R', 'WS'], 0, [5, 5]], [[0, 1, 4, 3, 2], [7, 6, 5, 8, 9]]), ('regression: paragraph-wide reordering', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('regression: paragraph-wide reordering', [[1, 1, 4, 1, 1, 2, 4, 4, 4, 3, 3], ['R', 'L', 'ON', 'WS', 'R', 'L', 'R', 'R', 'WS', 'R', 'WS'], 0, [4, 3, 2, 1, 1]], [[2, 1, 0, 3], [5, 6, 4], [7, 8], [9], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0, 0, 0, 0, 0], ['WS', 'R', 'L', 'WS', 'L', 'ON'], 0, [4, 1, 1]], [[0, 1, 2, 3], [4], [5]]), ('control layout', [[2, 2, 2, 2], ['R', 'R', 'R', 'WS'], 0, [2, 2]], [[0, 1], [2, 3]]), ('control layout', [[1, 1, 1], ['WS', 'WS', 'R'], 1, [3]], [[2, 1, 0]])], [('regression: paragraph-wide reordering', [[3, 3, 3, 3, 1, 1, 1, 1, 3], ['WS', 'R', 'WS', 'WS', 'R', 'WS', 'R', 'L', 'L'], 0, [5, 3, 1]], [[4, 3, 2, 1, 0], [7, 6, 5], [8]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'L', 'ON', 'L', 'WS', 'WS', 'WS'], 1, [2, 5, 1]], [[1, 0], [6, 5, 4, 3, 2], [7]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2], ['R', 'R', 'WS', 'R', 'ON', 'WS', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 5, 3]], [[2, 1, 0], [3, 4, 7, 6, 5], [8, 9, 10]]), ('regression: paragraph-wide reordering', [[0, 0, 0, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 1], ['WS', 'WS', 'L', 'R', 'R', 'R', 'L', 'WS', 'WS', 'WS', 'R', 'WS', 'L', 'ON'], 1, [1, 3, 1, 2, 5, 1, 1]], [[0], [1, 2, 3], [4], [6, 5], [7, 11, 8, 9, 10], [12], [13]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2], ['WS', 'WS', 'R'], 1, [3]], [[0, 1, 2]]), ('control layout', [[0, 0, 3, 0, 0, 0, 0, 0], ['WS', 'R', 'WS', 'WS', 'ON', 'WS', 'R', 'L'], 0, [2, 5, 1]], [[0, 1], [2, 3, 4, 5, 6], [7]]), ('control layout', [[3, 2], ['WS', 'ON'], 0, [2]], [[0, 1]])], [('regression: paragraph-wide reordering', [[1, 1, 1, 1, 3, 3, 2, 1, 1, 2, 2], ['ON', 'L', 'L', 'R', 'ON', 'WS', 'WS', 'R', 'L', 'R', 'L'], 1, [1, 3, 2, 4, 1]], [[0], [3, 2, 1], [5, 4], [9, 8, 7, 6], [10]]), ('regression: paragraph-wide reordering', [[3, 2, 2, 2, 2, 1, 1, 1, 4, 4], ['R', 'WS', 'R', 'R', 'WS', 'WS', 'L', 'R', 'ON', 'L'], 1, [2, 3, 2, 2, 1]], [[1, 0], [4, 2, 3], [6, 5], [8, 7], [9]]), ('regression: paragraph-wide reordering', [[2, 2, 2, 2, 3, 3, 1], ['R', 'L', 'WS', 'WS', 'R', 'WS', 'WS'], 1, [4, 3]], [[3, 2, 0, 1], [6, 5, 4]]), ('regression: paragraph-wide reordering', [[2, 4, 4, 3, 3, 3, 3], ['WS', 'ON', 'R', 'ON', 'WS', 'R', 'WS'], 1, [5, 2]], [[4, 0, 3, 1, 2], [6, 5]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0, 0, 0, 0, 0, 2], ['L', 'R', 'L', 'R', 'WS', 'L', 'WS'], 0, [1, 3, 1, 2]], [[0], [1, 2, 3], [4], [5, 6]]), ('control layout', [[2, 2, 2, 2], ['L', 'L', 'ON', 'R'], 0, [2, 2]], [[0, 1], [2, 3]]), ('control layout', [[2, 0, 0, 0, 0, 1], ['L', 'ON', 'WS', 'R', 'L', 'R'], 1, [5, 1]], [[0, 1, 2, 3, 4], [5]])], [('regression: paragraph-wide reordering', [[3, 3, 3, 3, 2, 2, 2, 0, 2, 2, 2, 2, 1, 1], ['WS', 'L', 'R', 'ON', 'L', 'R', 'WS', 'R', 'WS', 'R', 'WS', 'R', 'R', 'L'], 1, [3, 5, 5, 1]], [[2, 1, 0], [3, 4, 5, 6, 7], [12, 8, 9, 10, 11], [13]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 3, 0, 1], ['R', 'L', 'R', 'R', 'WS', 'L', 'R'], 0, [4, 1, 1, 1]], [[3, 2, 1, 0], [4], [5], [6]]), ('regression: paragraph-wide reordering', [[2, 2, 2, 1, 2, 4, 4, 4, 1, 1, 1, 1, 2, 2], ['R', 'L', 'R', 'WS', 'R', 'WS', 'WS', 'L', 'WS', 'L', 'WS', 'WS', 'ON', 'L'], 0, [2, 3, 2, 4, 3]], [[0, 1], [4, 3, 2], [5, 6], [9, 8, 7, 10], [12, 13, 11]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'R', 'L', 'WS', 'ON', 'WS', 'R', 'L', 'L', 'WS', 'L', 'WS'], 0, [4, 3, 4, 2]], [[3, 2, 1, 0], [4, 5, 6], [9, 8, 7, 10], [11, 12]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2], ['WS', 'L', 'ON'], 1, [3]], [[0, 1, 2]]), ('control layout', [[1, 1, 1], ['ON', 'R', 'R'], 0, [3]], [[2, 1, 0]]), ('control layout', [[1, 0, 0, 0, 0, 0, 0, 3], ['ON', 'WS', 'WS', 'L', 'R', 'WS', 'L', 'WS'], 1, [1, 1, 2, 2, 1, 1]], [[0], [1], [2, 3], [4, 5], [6], [7]])]]
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, classes, para, line_lengths = x
    out = []
    start = 0
    glob = reorder(levels)
    gpos = {i: v for v, i in enumerate(glob)}
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append(sorted(idx, key=lambda i: gpos[i]))
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 1, 1, 1, 3, 3, 1, 1, 0, 0, 0], ['L', 'L', 'L', 'WS', 'L', 'WS', 'R', 'R', 'ON', 'WS', 'R', 'WS', 'R', 'WS'], 0, [1, 4, 2, 3, 4]], [[0], [4, 3, 2, 1], [6, 5], [8, 7, 9], [10, 11, 12, 13]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'R', 'L', 'WS', 'ON', 'WS', 'R', 'L', 'L', 'WS', 'L', 'WS'], 0, [4, 3, 4, 2]], [[3, 2, 1, 0], [4, 5, 6], [9, 8, 7, 10], [11, 12]]), ('regression: paragraph-wide reordering', [[1, 0, 0, 0, 0, 0], ['R', 'L', 'WS', 'WS', 'WS', 'WS'], 1, [3, 2, 1]], [[0, 1, 2], [4, 3], [5]]), ('control layout', [[2, 2, 2, 0], ['WS', 'ON', 'WS', 'L'], 0, [1, 2, 1]], [[0], [1, 2], [3]]), ('control layout', [[2, 2, 2, 0, 0, 1], ['R', 'R', 'R', 'L', 'R', 'R'], 0, [3, 2, 1]], [[0, 1, 2], [3, 4], [5]]), ('control layout', [[0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0], ['WS', 'L', 'R', 'L', 'R', 'L', 'WS', 'R', 'WS', 'ON', 'WS', 'L', 'R', 'WS'], 0, [5, 5, 2, 2]], [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9], [10, 11], [12, 13]]), ('control layout', [[1, 1, 0, 0, 0], ['WS', 'WS', 'ON', 'R', 'R'], 0, [4, 1]], [[1, 0, 2, 3], [4]])], [('regression: paragraph-wide reordering', [[4, 4, 2, 2, 2, 1, 3], ['R', 'L', 'L', 'R', 'L', 'R', 'ON'], 0, [3, 2, 2]], [[0, 1, 2], [3, 4], [6, 5]]), ('regression: paragraph-wide reordering', [[0, 0, 3, 3, 3, 1, 1, 2, 0, 0], ['ON', 'R', 'L', 'ON', 'R', 'R', 'WS', 'L', 'R', 'WS'], 0, [5, 5]], [[0, 1, 4, 3, 2], [7, 6, 5, 8, 9]]), ('regression: paragraph-wide reordering', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('regression: paragraph-wide reordering', [[1, 1, 4, 1, 1, 2, 4, 4, 4, 3, 3], ['R', 'L', 'ON', 'WS', 'R', 'L', 'R', 'R', 'WS', 'R', 'WS'], 0, [4, 3, 2, 1, 1]], [[2, 1, 0, 3], [5, 6, 4], [7, 8], [9], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0, 0, 0, 0, 0], ['WS', 'R', 'L', 'WS', 'L', 'ON'], 0, [4, 1, 1]], [[0, 1, 2, 3], [4], [5]]), ('control layout', [[2, 2, 2, 2], ['R', 'R', 'R', 'WS'], 0, [2, 2]], [[0, 1], [2, 3]]), ('control layout', [[1, 1, 1], ['WS', 'WS', 'R'], 1, [3]], [[2, 1, 0]])], [('regression: paragraph-wide reordering', [[3, 3, 3, 3, 1, 1, 1, 1, 3], ['WS', 'R', 'WS', 'WS', 'R', 'WS', 'R', 'L', 'L'], 0, [5, 3, 1]], [[4, 3, 2, 1, 0], [7, 6, 5], [8]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'L', 'ON', 'L', 'WS', 'WS', 'WS'], 1, [2, 5, 1]], [[1, 0], [6, 5, 4, 3, 2], [7]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2], ['R', 'R', 'WS', 'R', 'ON', 'WS', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 5, 3]], [[2, 1, 0], [3, 4, 7, 6, 5], [8, 9, 10]]), ('regression: paragraph-wide reordering', [[0, 0, 0, 1, 1, 1, 1, 0, 2, 2, 2, 2, 2, 1], ['WS', 'WS', 'L', 'R', 'R', 'R', 'L', 'WS', 'WS', 'WS', 'R', 'WS', 'L', 'ON'], 1, [1, 3, 1, 2, 5, 1, 1]], [[0], [1, 2, 3], [4], [6, 5], [7, 11, 8, 9, 10], [12], [13]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2], ['WS', 'WS', 'R'], 1, [3]], [[0, 1, 2]]), ('control layout', [[0, 0, 3, 0, 0, 0, 0, 0], ['WS', 'R', 'WS', 'WS', 'ON', 'WS', 'R', 'L'], 0, [2, 5, 1]], [[0, 1], [2, 3, 4, 5, 6], [7]]), ('control layout', [[3, 2], ['WS', 'ON'], 0, [2]], [[0, 1]])], [('regression: paragraph-wide reordering', [[1, 1, 1, 1, 3, 3, 2, 1, 1, 2, 2], ['ON', 'L', 'L', 'R', 'ON', 'WS', 'WS', 'R', 'L', 'R', 'L'], 1, [1, 3, 2, 4, 1]], [[0], [3, 2, 1], [5, 4], [9, 8, 7, 6], [10]]), ('regression: paragraph-wide reordering', [[3, 2, 2, 2, 2, 1, 1, 1, 4, 4], ['R', 'WS', 'R', 'R', 'WS', 'WS', 'L', 'R', 'ON', 'L'], 1, [2, 3, 2, 2, 1]], [[1, 0], [4, 2, 3], [6, 5], [8, 7], [9]]), ('regression: paragraph-wide reordering', [[2, 2, 2, 2, 3, 3, 1], ['R', 'L', 'WS', 'WS', 'R', 'WS', 'WS'], 1, [4, 3]], [[3, 2, 0, 1], [6, 5, 4]]), ('regression: paragraph-wide reordering', [[2, 4, 4, 3, 3, 3, 3], ['WS', 'ON', 'R', 'ON', 'WS', 'R', 'WS'], 1, [5, 2]], [[4, 0, 3, 1, 2], [6, 5]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0, 0, 0, 0, 0, 2], ['L', 'R', 'L', 'R', 'WS', 'L', 'WS'], 0, [1, 3, 1, 2]], [[0], [1, 2, 3], [4], [5, 6]]), ('control layout', [[2, 2, 2, 2], ['L', 'L', 'ON', 'R'], 0, [2, 2]], [[0, 1], [2, 3]]), ('control layout', [[2, 0, 0, 0, 0, 1], ['L', 'ON', 'WS', 'R', 'L', 'R'], 1, [5, 1]], [[0, 1, 2, 3, 4], [5]])], [('regression: paragraph-wide reordering', [[3, 3, 3, 3, 2, 2, 2, 0, 2, 2, 2, 2, 1, 1], ['WS', 'L', 'R', 'ON', 'L', 'R', 'WS', 'R', 'WS', 'R', 'WS', 'R', 'R', 'L'], 1, [3, 5, 5, 1]], [[2, 1, 0], [3, 4, 5, 6, 7], [12, 8, 9, 10, 11], [13]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 1, 3, 0, 1], ['R', 'L', 'R', 'R', 'WS', 'L', 'R'], 0, [4, 1, 1, 1]], [[3, 2, 1, 0], [4], [5], [6]]), ('regression: paragraph-wide reordering', [[2, 2, 2, 1, 2, 4, 4, 4, 1, 1, 1, 1, 2, 2], ['R', 'L', 'R', 'WS', 'R', 'WS', 'WS', 'L', 'WS', 'L', 'WS', 'WS', 'ON', 'L'], 0, [2, 3, 2, 4, 3]], [[0, 1], [4, 3, 2], [5, 6], [9, 8, 7, 10], [12, 13, 11]]), ('regression: paragraph-wide reordering', [[1, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'R', 'L', 'WS', 'ON', 'WS', 'R', 'L', 'L', 'WS', 'L', 'WS'], 0, [4, 3, 4, 2]], [[3, 2, 1, 0], [4, 5, 6], [9, 8, 7, 10], [11, 12]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2], ['WS', 'L', 'ON'], 1, [3]], [[0, 1, 2]]), ('control layout', [[1, 1, 1], ['ON', 'R', 'R'], 0, [3]], [[2, 1, 0]]), ('control layout', [[1, 0, 0, 0, 0, 0, 0, 3], ['ON', 'WS', 'WS', 'L', 'R', 'WS', 'L', 'WS'], 1, [1, 1, 2, 2, 1, 1]], [[0], [1], [2, 3], [4, 5], [6], [7]])]]
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:
Lines show characters from other lines after wrapping RTL text.

Input [paragraph levels, classes, paragraph level, line lengths]. For each logical line: reset trailing WS to the paragraph level, then reorder that line alone by level reversal (lowest odd level of the line). Return per line the paragraph indices in visual order.

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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS' and start + ln == len(levels):
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append([idx[v] for v in local])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: trailing whitespace per line', [[2, 1, 2, 2, 2, 2], ['ON', 'WS', 'ON', 'WS', 'WS', 'R'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: trailing whitespace per line', [[4, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 1, 3], ['WS', 'R', 'L', 'ON', 'WS', 'WS', 'WS', 'L', 'ON', 'R', 'WS', 'L', 'R'], 1, [2, 5, 3, 1, 1, 1]], [[0, 1], [6, 5, 4, 2, 3], [7, 8, 9], [10], [11], [12]]), ('regression: trailing whitespace per line', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('control layout', [[1, 0, 0, 0, 0, 0, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'WS', 'ON', 'R', 'WS', 'WS'], 0, [4, 1, 4]], [[0, 1, 2, 3], [4], [5, 6, 7, 8]]), ('control layout', [[3, 0], ['ON', 'WS'], 1, [1, 1]], [[0], [1]]), ('control layout', [[0, 3, 3], ['L', 'L', 'WS'], 0, [1, 2]], [[0], [1, 2]]), ('control layout', [[4, 4, 4, 4, 2, 2, 2], ['L', 'R', 'L', 'WS', 'R', 'R', 'R'], 1, [2, 5]], [[0, 1], [2, 3, 4, 5, 6]])], [('regression: trailing whitespace per line', [[2, 3, 0, 0, 3, 2, 2, 2, 1, 1, 1], ['L', 'WS', 'WS', 'WS', 'WS', 'ON', 'R', 'L', 'L', 'R', 'R'], 1, [4, 5, 1, 1]], [[3, 2, 1, 0], [8, 4, 5, 6, 7], [9], [10]]), ('regression: trailing whitespace per line', [[3, 2, 2, 2, 2, 1, 1, 1, 4, 4], ['R', 'WS', 'R', 'R', 'WS', 'WS', 'L', 'R', 'ON', 'L'], 1, [2, 3, 2, 2, 1]], [[1, 0], [4, 2, 3], [6, 5], [8, 7], [9]]), ('partial-repair probe', [[0, 0, 0, 0, 1, 1, 3, 2, 2, 2, 2, 0, 0], ['WS', 'WS', 'R', 'R', 'R', 'R', 'L', 'WS', 'ON', 'L', 'R', 'WS', 'R'], 0, [5, 4, 4]], [[0, 1, 2, 3, 4], [6, 7, 8, 5], [9, 10, 11, 12]]), ('partial-repair probe', [[2, 2, 1, 1, 1, 1, 2, 2], ['WS', 'L', 'R', 'WS', 'ON', 'L', 'WS', 'L'], 0, [5, 1, 2]], [[4, 3, 2, 0, 1], [5], [6, 7]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2, 1, 2, 3, 3, 3, 2], ['L', 'ON', 'R', 'R', 'L', 'L', 'R', 'L', 'L'], 0, [5, 3, 1]], [[4, 3, 0, 1, 2], [7, 6, 5], [8]]), ('control layout', [[4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'R', 'R', 'R', 'R', 'L', 'L', 'R', 'WS', 'WS', 'WS', 'WS', 'ON'], 1, [4, 2, 3, 1, 1, 1, 1]], [[3, 0, 1, 2], [5, 4], [8, 7, 6], [9], [10], [11], [12]]), ('control layout', [[1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 1], ['L', 'L', 'ON', 'L', 'WS', 'L', 'WS', 'L', 'R', 'WS', 'WS', 'R'], 0, [4, 1, 5, 2]], [[1, 2, 3, 0], [4], [5, 6, 7, 8, 9], [11, 10]])], [('regression: trailing whitespace per line', [[4, 4, 4, 4, 3, 2, 2, 1], ['L', 'WS', 'R', 'L', 'WS', 'WS', 'WS', 'R'], 1, [3, 4, 1]], [[0, 1, 2], [6, 5, 4, 3], [7]]), ('regression: trailing whitespace per line', [[2, 2, 2, 2, 4, 4, 4, 3, 2, 2], ['WS', 'WS', 'WS', 'WS', 'L', 'WS', 'L', 'R', 'R', 'WS'], 1, [2, 2, 3, 2, 1]], [[1, 0], [3, 2], [4, 5, 6], [7, 8], [9]]), ('partial-repair probe', [[1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1], ['L', 'WS', 'ON', 'ON', 'WS', 'R', 'L', 'R', 'WS', 'L', 'WS'], 0, [3, 4, 1, 2, 1]], [[2, 1, 0], [6, 5, 4, 3], [7], [9, 8], [10]]), ('partial-repair probe', [[2, 2, 2, 2], ['L', 'R', 'ON', 'ON'], 1, [4]], [[0, 1, 2, 3]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 1, 1, 1, 2], ['R', 'L', 'R', 'L', 'ON'], 1, [2, 1, 2]], [[1, 0], [2], [4, 3]]), ('control layout', [[4, 4, 4, 4, 2, 2, 2, 2, 2], ['L', 'WS', 'R', 'R', 'L', 'ON', 'R', 'R', 'R'], 1, [4, 4, 1]], [[0, 1, 2, 3], [4, 5, 6, 7], [8]]), ('control layout', [[4, 4, 4, 4, 4, 1, 1], ['L', 'ON', 'WS', 'L', 'WS', 'WS', 'WS'], 0, [5, 2]], [[0, 1, 2, 3, 4], [5, 6]])], [('regression: trailing whitespace per line', [[2, 2, 2, 2, 2, 2, 2, 2, 2], ['R', 'R', 'L', 'L', 'L', 'WS', 'WS', 'R', 'L'], 1, [3, 3, 1, 1, 1]], [[0, 1, 2], [5, 3, 4], [6], [7], [8]]), ('regression: trailing whitespace per line', [[4, 4, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['R', 'WS', 'L', 'WS', 'WS', 'WS', 'L', 'WS', 'R', 'R', 'L', 'R'], 0, [4, 5, 2, 1]], [[2, 0, 1, 3], [8, 7, 6, 4, 5], [10, 9], [11]]), ('regression: trailing whitespace per line', [[1, 1, 1, 1, 3, 3, 3, 3], ['L', 'WS', 'ON', 'WS', 'WS', 'L', 'ON', 'R'], 0, [5, 3]], [[2, 1, 0, 3, 4], [7, 6, 5]]), ('partial-repair probe', [[3, 3, 1, 1, 1, 1, 4, 3, 1, 1, 4, 4, 4], ['WS', 'R', 'WS', 'WS', 'R', 'R', 'WS', 'L', 'WS', 'R', 'L', 'ON', 'WS'], 0, [5, 3, 5]], [[4, 3, 2, 1, 0], [7, 6, 5], [10, 11, 9, 8, 12]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3], ['L', 'ON', 'R', 'R'], 1, [3, 1]], [[2, 1, 0], [3]]), ('control layout', [[4, 1, 1, 3, 3, 2, 4, 2, 4, 4], ['R', 'R', 'WS', 'WS', 'L', 'WS', 'ON', 'L', 'L', 'L'], 1, [5, 3, 2]], [[4, 3, 2, 1, 0], [5, 6, 7], [8, 9]]), ('control layout', [[0, 0, 1, 1, 1, 2, 0], ['R', 'ON', 'R', 'R', 'R', 'WS', 'R'], 1, [3, 2, 1, 1]], [[0, 1, 2], [4, 3], [5], [6]])], [('regression: trailing whitespace per line', [[1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0], ['L', 'R', 'L', 'L', 'WS', 'L', 'R', 'ON', 'R', 'L', 'R', 'WS', 'L'], 1, [4, 4, 4, 1]], [[3, 2, 1, 0], [4, 5, 6, 7], [11, 10, 9, 8], [12]]), ('regression: trailing whitespace per line', [[1, 1, 4], ['R', 'WS', 'L'], 0, [2, 1]], [[0, 1], [2]]), ('regression: trailing whitespace per line', [[1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 2, 2, 2, 2], ['WS', 'ON', 'WS', 'L', 'L', 'R', 'L', 'R', 'R', 'WS', 'L', 'ON', 'R', 'WS'], 0, [3, 3, 1, 4, 3]], [[1, 0, 2], [3, 4, 5], [6], [7, 8, 9, 10], [11, 12, 13]]), ('partial-repair probe', [[1, 1, 1, 1, 4, 2, 2, 1, 2, 4, 4, 4], ['WS', 'L', 'R', 'WS', 'R', 'R', 'R', 'R', 'WS', 'WS', 'L', 'ON'], 1, [3, 5, 1, 3]], [[2, 1, 0], [7, 4, 5, 6, 3], [8], [9, 10, 11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0], ['R', 'L'], 1, [1, 1]], [[0], [1]]), ('control layout', [[1, 1], ['L', 'WS'], 0, [2]], [[0, 1]]), ('control layout', [[2, 2, 2, 2], ['R', 'L', 'WS', 'R'], 0, [2, 2]], [[0, 1], [2, 3]])]]
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):
    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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] in ('WS', 'ON'):
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append([idx[v] for v in local])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: trailing whitespace per line', [[2, 1, 2, 2, 2, 2], ['ON', 'WS', 'ON', 'WS', 'WS', 'R'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: trailing whitespace per line', [[4, 2, 2, 2, 2, 2, 2, 2, 4, 2, 2, 1, 3], ['WS', 'R', 'L', 'ON', 'WS', 'WS', 'WS', 'L', 'ON', 'R', 'WS', 'L', 'R'], 1, [2, 5, 3, 1, 1, 1]], [[0, 1], [6, 5, 4, 2, 3], [7, 8, 9], [10], [11], [12]]), ('regression: trailing whitespace per line', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('control layout', [[1, 0, 0, 0, 0, 0, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'WS', 'ON', 'R', 'WS', 'WS'], 0, [4, 1, 4]], [[0, 1, 2, 3], [4], [5, 6, 7, 8]]), ('control layout', [[3, 0], ['ON', 'WS'], 1, [1, 1]], [[0], [1]]), ('control layout', [[0, 3, 3], ['L', 'L', 'WS'], 0, [1, 2]], [[0], [1, 2]]), ('control layout', [[4, 4, 4, 4, 2, 2, 2], ['L', 'R', 'L', 'WS', 'R', 'R', 'R'], 1, [2, 5]], [[0, 1], [2, 3, 4, 5, 6]])], [('regression: trailing whitespace per line', [[2, 3, 0, 0, 3, 2, 2, 2, 1, 1, 1], ['L', 'WS', 'WS', 'WS', 'WS', 'ON', 'R', 'L', 'L', 'R', 'R'], 1, [4, 5, 1, 1]], [[3, 2, 1, 0], [8, 4, 5, 6, 7], [9], [10]]), ('regression: trailing whitespace per line', [[3, 2, 2, 2, 2, 1, 1, 1, 4, 4], ['R', 'WS', 'R', 'R', 'WS', 'WS', 'L', 'R', 'ON', 'L'], 1, [2, 3, 2, 2, 1]], [[1, 0], [4, 2, 3], [6, 5], [8, 7], [9]]), ('partial-repair probe', [[0, 0, 0, 0, 1, 1, 3, 2, 2, 2, 2, 0, 0], ['WS', 'WS', 'R', 'R', 'R', 'R', 'L', 'WS', 'ON', 'L', 'R', 'WS', 'R'], 0, [5, 4, 4]], [[0, 1, 2, 3, 4], [6, 7, 8, 5], [9, 10, 11, 12]]), ('partial-repair probe', [[2, 2, 1, 1, 1, 1, 2, 2], ['WS', 'L', 'R', 'WS', 'ON', 'L', 'WS', 'L'], 0, [5, 1, 2]], [[4, 3, 2, 0, 1], [5], [6, 7]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[2, 2, 2, 1, 2, 3, 3, 3, 2], ['L', 'ON', 'R', 'R', 'L', 'L', 'R', 'L', 'L'], 0, [5, 3, 1]], [[4, 3, 0, 1, 2], [7, 6, 5], [8]]), ('control layout', [[4, 4, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'R', 'R', 'R', 'R', 'L', 'L', 'R', 'WS', 'WS', 'WS', 'WS', 'ON'], 1, [4, 2, 3, 1, 1, 1, 1]], [[3, 0, 1, 2], [5, 4], [8, 7, 6], [9], [10], [11], [12]]), ('control layout', [[1, 2, 2, 2, 2, 2, 4, 4, 4, 4, 2, 1], ['L', 'L', 'ON', 'L', 'WS', 'L', 'WS', 'L', 'R', 'WS', 'WS', 'R'], 0, [4, 1, 5, 2]], [[1, 2, 3, 0], [4], [5, 6, 7, 8, 9], [11, 10]])], [('regression: trailing whitespace per line', [[4, 4, 4, 4, 3, 2, 2, 1], ['L', 'WS', 'R', 'L', 'WS', 'WS', 'WS', 'R'], 1, [3, 4, 1]], [[0, 1, 2], [6, 5, 4, 3], [7]]), ('regression: trailing whitespace per line', [[2, 2, 2, 2, 4, 4, 4, 3, 2, 2], ['WS', 'WS', 'WS', 'WS', 'L', 'WS', 'L', 'R', 'R', 'WS'], 1, [2, 2, 3, 2, 1]], [[1, 0], [3, 2], [4, 5, 6], [7, 8], [9]]), ('partial-repair probe', [[1, 1, 1, 1, 3, 3, 1, 1, 1, 1, 1], ['L', 'WS', 'ON', 'ON', 'WS', 'R', 'L', 'R', 'WS', 'L', 'WS'], 0, [3, 4, 1, 2, 1]], [[2, 1, 0], [6, 5, 4, 3], [7], [9, 8], [10]]), ('partial-repair probe', [[2, 2, 2, 2], ['L', 'R', 'ON', 'ON'], 1, [4]], [[0, 1, 2, 3]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 1, 1, 1, 2], ['R', 'L', 'R', 'L', 'ON'], 1, [2, 1, 2]], [[1, 0], [2], [4, 3]]), ('control layout', [[4, 4, 4, 4, 2, 2, 2, 2, 2], ['L', 'WS', 'R', 'R', 'L', 'ON', 'R', 'R', 'R'], 1, [4, 4, 1]], [[0, 1, 2, 3], [4, 5, 6, 7], [8]]), ('control layout', [[4, 4, 4, 4, 4, 1, 1], ['L', 'ON', 'WS', 'L', 'WS', 'WS', 'WS'], 0, [5, 2]], [[0, 1, 2, 3, 4], [5, 6]])], [('regression: trailing whitespace per line', [[2, 2, 2, 2, 2, 2, 2, 2, 2], ['R', 'R', 'L', 'L', 'L', 'WS', 'WS', 'R', 'L'], 1, [3, 3, 1, 1, 1]], [[0, 1, 2], [5, 3, 4], [6], [7], [8]]), ('regression: trailing whitespace per line', [[4, 4, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1], ['R', 'WS', 'L', 'WS', 'WS', 'WS', 'L', 'WS', 'R', 'R', 'L', 'R'], 0, [4, 5, 2, 1]], [[2, 0, 1, 3], [8, 7, 6, 4, 5], [10, 9], [11]]), ('regression: trailing whitespace per line', [[1, 1, 1, 1, 3, 3, 3, 3], ['L', 'WS', 'ON', 'WS', 'WS', 'L', 'ON', 'R'], 0, [5, 3]], [[2, 1, 0, 3, 4], [7, 6, 5]]), ('partial-repair probe', [[3, 3, 1, 1, 1, 1, 4, 3, 1, 1, 4, 4, 4], ['WS', 'R', 'WS', 'WS', 'R', 'R', 'WS', 'L', 'WS', 'R', 'L', 'ON', 'WS'], 0, [5, 3, 5]], [[4, 3, 2, 1, 0], [7, 6, 5], [10, 11, 9, 8, 12]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3], ['L', 'ON', 'R', 'R'], 1, [3, 1]], [[2, 1, 0], [3]]), ('control layout', [[4, 1, 1, 3, 3, 2, 4, 2, 4, 4], ['R', 'R', 'WS', 'WS', 'L', 'WS', 'ON', 'L', 'L', 'L'], 1, [5, 3, 2]], [[4, 3, 2, 1, 0], [5, 6, 7], [8, 9]]), ('control layout', [[0, 0, 1, 1, 1, 2, 0], ['R', 'ON', 'R', 'R', 'R', 'WS', 'R'], 1, [3, 2, 1, 1]], [[0, 1, 2], [4, 3], [5], [6]])], [('regression: trailing whitespace per line', [[1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0], ['L', 'R', 'L', 'L', 'WS', 'L', 'R', 'ON', 'R', 'L', 'R', 'WS', 'L'], 1, [4, 4, 4, 1]], [[3, 2, 1, 0], [4, 5, 6, 7], [11, 10, 9, 8], [12]]), ('regression: trailing whitespace per line', [[1, 1, 4], ['R', 'WS', 'L'], 0, [2, 1]], [[0, 1], [2]]), ('regression: trailing whitespace per line', [[1, 1, 2, 2, 2, 2, 2, 4, 4, 4, 2, 2, 2, 2], ['WS', 'ON', 'WS', 'L', 'L', 'R', 'L', 'R', 'R', 'WS', 'L', 'ON', 'R', 'WS'], 0, [3, 3, 1, 4, 3]], [[1, 0, 2], [3, 4, 5], [6], [7, 8, 9, 10], [11, 12, 13]]), ('partial-repair probe', [[1, 1, 1, 1, 4, 2, 2, 1, 2, 4, 4, 4], ['WS', 'L', 'R', 'WS', 'R', 'R', 'R', 'R', 'WS', 'WS', 'L', 'ON'], 1, [3, 5, 1, 3]], [[2, 1, 0], [7, 4, 5, 6, 3], [8], [9, 10, 11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[0, 0], ['R', 'L'], 1, [1, 1]], [[0], [1]]), ('control layout', [[1, 1], ['L', 'WS'], 0, [2]], [[0, 1]]), ('control layout', [[2, 2, 2, 2], ['R', 'L', 'WS', 'R'], 0, [2, 2]], [[0, 1], [2, 3]])]]
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:
Spaces at the end of wrapped lines jump to the far side.

Input [paragraph levels, classes, paragraph level, line lengths]. For each logical line: reset trailing WS to the paragraph level, then reorder that line alone by level reversal (lowest odd level of the line). Return per line the paragraph indices in visual order.

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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = 0
            j -= 1
        local = reorder(lv)
        out.append([idx[v] for v in local])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: whitespace reset level', [[1, 1, 1, 1, 4, 4, 4, 3, 3, 4], ['R', 'R', 'R', 'WS', 'ON', 'WS', 'R', 'R', 'R', 'WS'], 1, [3, 2, 2, 1, 2]], [[2, 1, 0], [4, 3], [5, 6], [7], [9, 8]]), ('regression: whitespace reset level', [[2, 2, 2, 2], ['R', 'R', 'R', 'WS'], 1, [2, 2]], [[0, 1], [3, 2]]), ('regression: whitespace reset level', [[1, 1, 2, 1, 1, 1, 3, 3, 3, 1, 3, 3, 2, 2], ['WS', 'WS', 'ON', 'R', 'R', 'R', 'WS', 'WS', 'WS', 'WS', 'L', 'R', 'WS', 'R'], 1, [2, 4, 1, 4, 2, 1]], [[1, 0], [5, 4, 3, 2], [6], [10, 9, 8, 7], [12, 11], [13]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2], ['R', 'R', 'WS', 'R', 'ON', 'WS', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 5, 3]], [[2, 1, 0], [3, 4, 7, 6, 5], [8, 9, 10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 3, 1, 1], ['R', 'L', 'WS', 'R', 'WS', 'R', 'WS'], 0, [4, 2, 1]], [[3, 2, 1, 0], [5, 4], [6]]), ('control layout', [[0, 0, 1, 1, 1, 2, 0], ['R', 'ON', 'R', 'R', 'R', 'WS', 'R'], 1, [3, 2, 1, 1]], [[0, 1, 2], [4, 3], [5], [6]]), ('control layout', [[1, 3, 3, 3, 3, 3, 3, 3], ['WS', 'R', 'WS', 'L', 'WS', 'R', 'L', 'WS'], 0, [4, 4]], [[3, 2, 1, 0], [6, 5, 4, 7]])], [('regression: whitespace reset level', [[2, 2, 2, 2, 2, 2], ['L', 'R', 'L', 'L', 'WS', 'WS'], 1, [2, 3, 1]], [[0, 1], [4, 2, 3], [5]]), ('regression: whitespace reset level', [[1, 3, 3, 3, 1, 1, 2, 2, 1, 2, 2, 2], ['WS', 'ON', 'L', 'L', 'ON', 'R', 'L', 'L', 'R', 'WS', 'L', 'WS'], 1, [1, 2, 3, 1, 1, 1, 3]], [[0], [2, 1], [5, 4, 3], [6], [7], [8], [11, 9, 10]]), ('regression: whitespace reset level', [[2, 2, 2, 2, 2, 1, 1, 1, 1, 4, 2, 2], ['R', 'R', 'WS', 'L', 'R', 'WS', 'L', 'R', 'R', 'WS', 'WS', 'WS'], 1, [1, 5, 1, 2, 3]], [[0], [5, 1, 2, 3, 4], [6], [8, 7], [11, 10, 9]]), ('regression: whitespace reset level', [[1, 1, 1, 3, 3, 3, 3, 0, 0, 0], ['L', 'WS', 'WS', 'WS', 'L', 'WS', 'WS', 'ON', 'R', 'L'], 1, [3, 4, 1, 1, 1]], [[2, 1, 0], [6, 5, 4, 3], [7], [8], [9]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 1], ['ON', 'WS'], 0, [2]], [[0, 1]]), ('control layout', [[1, 1, 3, 3, 0, 0, 3, 3, 3, 1, 1], ['L', 'R', 'R', 'WS', 'L', 'ON', 'WS', 'R', 'WS', 'L', 'R'], 0, [1, 2, 3, 5]], [[0], [2, 1], [3, 4, 5], [10, 9, 8, 7, 6]]), ('control layout', [[0, 0, 0, 0, 0, 0], ['WS', 'R', 'L', 'WS', 'L', 'ON'], 0, [4, 1, 1]], [[0, 1, 2, 3], [4], [5]])], [('regression: whitespace reset level', [[0, 0, 0, 0], ['L', 'R', 'WS', 'WS'], 1, [4]], [[0, 1, 3, 2]]), ('regression: whitespace reset level', [[1, 1], ['WS', 'WS'], 1, [2]], [[1, 0]]), ('partial-repair probe', [[3, 3, 3, 4, 4, 4, 1], ['R', 'R', 'WS', 'R', 'WS', 'WS', 'R'], 0, [1, 4, 2]], [[0], [3, 2, 1, 4], [6, 5]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'L', 'ON', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 1, 5, 1, 1]], [[2, 1, 0], [3], [7, 6, 5, 4, 8], [9], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 2, 2, 2, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0], ['L', 'WS', 'R', 'R', 'WS', 'R', 'L', 'WS', 'L', 'L', 'L', 'L', 'L', 'R'], 1, [1, 5, 2, 3, 3]], [[0], [1, 2, 3, 4, 5], [6, 7], [9, 8, 10], [11, 12, 13]]), ('control layout', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('control layout', [[0, 3, 3, 3, 0, 0, 0, 0, 0, 0], ['L', 'WS', 'R', 'L', 'L', 'L', 'L', 'L', 'L', 'WS'], 1, [1, 4, 4, 1]], [[0], [3, 2, 1, 4], [5, 6, 7, 8], [9]])], [('regression: whitespace reset level', [[2, 2, 2, 2, 1, 1, 1, 3, 3, 3, 3, 1, 2, 2], ['ON', 'R', 'WS', 'R', 'L', 'R', 'R', 'R', 'R', 'L', 'R', 'R', 'WS', 'WS'], 1, [1, 1, 2, 3, 1, 2, 1, 3]], [[0], [1], [2, 3], [6, 5, 4], [7], [9, 8], [10], [13, 12, 11]]), ('regression: whitespace reset level', [[2, 2, 2, 2, 3, 3, 1], ['R', 'L', 'WS', 'WS', 'R', 'WS', 'WS'], 1, [4, 3]], [[3, 2, 0, 1], [6, 5, 4]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'R', 'WS', 'WS', 'WS'], 1, [5, 1]], [[4, 3, 2, 1, 0], [5]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1], ['WS', 'R', 'WS', 'L', 'L'], 1, [3, 1, 1]], [[2, 1, 0], [3], [4]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 3, 3, 3, 3, 3, 0], ['WS', 'WS', 'L', 'WS', 'R', 'L', 'WS', 'WS', 'R', 'WS'], 0, [4, 2, 2, 1, 1]], [[2, 1, 0, 3], [5, 4], [6, 7], [8], [9]]), ('control layout', [[1, 1, 1, 1], ['L', 'R', 'WS', 'ON'], 0, [1, 3]], [[0], [3, 2, 1]]), ('control layout', [[0, 0, 0, 0, 0], ['R', 'L', 'L', 'WS', 'WS'], 0, [4, 1]], [[0, 1, 2, 3], [4]])], [('regression: whitespace reset level', [[2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1], ['L', 'WS', 'L', 'WS', 'ON', 'WS', 'R', 'R', 'WS', 'WS', 'R', 'R'], 1, [5, 5, 1, 1]], [[0, 1, 2, 4, 3], [9, 8, 7, 6, 5], [10], [11]]), ('regression: whitespace reset level', [[2, 4, 1, 1, 1, 3], ['ON', 'L', 'ON', 'L', 'WS', 'R'], 1, [5, 1]], [[4, 3, 2, 0, 1], [5]]), ('regression: whitespace reset level', [[1, 1, 1, 3, 1, 0, 0, 0, 0], ['R', 'WS', 'WS', 'WS', 'R', 'L', 'L', 'L', 'R'], 1, [3, 5, 1]], [[2, 1, 0], [4, 3, 5, 6, 7], [8]]), ('regression: whitespace reset level', [[1, 1], ['WS', 'WS'], 1, [2]], [[1, 0]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 1, 1, 0, 0], ['WS', 'WS', 'R', 'L', 'R', 'WS', 'R', 'ON'], 1, [3, 1, 1, 3]], [[2, 1, 0], [3], [4], [5, 6, 7]]), ('control layout', [[0, 0, 0, 0, 0], ['R', 'L', 'L', 'WS', 'WS'], 0, [4, 1]], [[0, 1, 2, 3], [4]]), ('control layout', [[2, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0], ['R', 'WS', 'L', 'WS', 'WS', 'L', 'WS', 'L', 'WS', 'R', 'WS', 'ON'], 1, [3, 5, 4]], [[0, 1, 2], [3, 5, 6, 7, 4], [8, 9, 10, 11]])]]
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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = lv[j] - lv[j] % 2
            j -= 1
        local = reorder(lv)
        out.append([idx[v] for v in local])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: whitespace reset level', [[1, 1, 1, 1, 4, 4, 4, 3, 3, 4], ['R', 'R', 'R', 'WS', 'ON', 'WS', 'R', 'R', 'R', 'WS'], 1, [3, 2, 2, 1, 2]], [[2, 1, 0], [4, 3], [5, 6], [7], [9, 8]]), ('regression: whitespace reset level', [[2, 2, 2, 2], ['R', 'R', 'R', 'WS'], 1, [2, 2]], [[0, 1], [3, 2]]), ('regression: whitespace reset level', [[1, 1, 2, 1, 1, 1, 3, 3, 3, 1, 3, 3, 2, 2], ['WS', 'WS', 'ON', 'R', 'R', 'R', 'WS', 'WS', 'WS', 'WS', 'L', 'R', 'WS', 'R'], 1, [2, 4, 1, 4, 2, 1]], [[1, 0], [5, 4, 3, 2], [6], [10, 9, 8, 7], [12, 11], [13]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 2], ['R', 'R', 'WS', 'R', 'ON', 'WS', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 5, 3]], [[2, 1, 0], [3, 4, 7, 6, 5], [8, 9, 10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 3, 1, 1], ['R', 'L', 'WS', 'R', 'WS', 'R', 'WS'], 0, [4, 2, 1]], [[3, 2, 1, 0], [5, 4], [6]]), ('control layout', [[0, 0, 1, 1, 1, 2, 0], ['R', 'ON', 'R', 'R', 'R', 'WS', 'R'], 1, [3, 2, 1, 1]], [[0, 1, 2], [4, 3], [5], [6]]), ('control layout', [[1, 3, 3, 3, 3, 3, 3, 3], ['WS', 'R', 'WS', 'L', 'WS', 'R', 'L', 'WS'], 0, [4, 4]], [[3, 2, 1, 0], [6, 5, 4, 7]])], [('regression: whitespace reset level', [[2, 2, 2, 2, 2, 2], ['L', 'R', 'L', 'L', 'WS', 'WS'], 1, [2, 3, 1]], [[0, 1], [4, 2, 3], [5]]), ('regression: whitespace reset level', [[1, 3, 3, 3, 1, 1, 2, 2, 1, 2, 2, 2], ['WS', 'ON', 'L', 'L', 'ON', 'R', 'L', 'L', 'R', 'WS', 'L', 'WS'], 1, [1, 2, 3, 1, 1, 1, 3]], [[0], [2, 1], [5, 4, 3], [6], [7], [8], [11, 9, 10]]), ('regression: whitespace reset level', [[2, 2, 2, 2, 2, 1, 1, 1, 1, 4, 2, 2], ['R', 'R', 'WS', 'L', 'R', 'WS', 'L', 'R', 'R', 'WS', 'WS', 'WS'], 1, [1, 5, 1, 2, 3]], [[0], [5, 1, 2, 3, 4], [6], [8, 7], [11, 10, 9]]), ('regression: whitespace reset level', [[1, 1, 1, 3, 3, 3, 3, 0, 0, 0], ['L', 'WS', 'WS', 'WS', 'L', 'WS', 'WS', 'ON', 'R', 'L'], 1, [3, 4, 1, 1, 1]], [[2, 1, 0], [6, 5, 4, 3], [7], [8], [9]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 1], ['ON', 'WS'], 0, [2]], [[0, 1]]), ('control layout', [[1, 1, 3, 3, 0, 0, 3, 3, 3, 1, 1], ['L', 'R', 'R', 'WS', 'L', 'ON', 'WS', 'R', 'WS', 'L', 'R'], 0, [1, 2, 3, 5]], [[0], [2, 1], [3, 4, 5], [10, 9, 8, 7, 6]]), ('control layout', [[0, 0, 0, 0, 0, 0], ['WS', 'R', 'L', 'WS', 'L', 'ON'], 0, [4, 1, 1]], [[0, 1, 2, 3], [4], [5]])], [('regression: whitespace reset level', [[0, 0, 0, 0], ['L', 'R', 'WS', 'WS'], 1, [4]], [[0, 1, 3, 2]]), ('regression: whitespace reset level', [[1, 1], ['WS', 'WS'], 1, [2]], [[1, 0]]), ('partial-repair probe', [[3, 3, 3, 4, 4, 4, 1], ['R', 'R', 'WS', 'R', 'WS', 'WS', 'R'], 0, [1, 4, 2]], [[0], [3, 2, 1, 4], [6, 5]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'L', 'ON', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 1, 5, 1, 1]], [[2, 1, 0], [3], [7, 6, 5, 4, 8], [9], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[1, 2, 2, 2, 2, 0, 0, 0, 1, 1, 0, 0, 0, 0], ['L', 'WS', 'R', 'R', 'WS', 'R', 'L', 'WS', 'L', 'L', 'L', 'L', 'L', 'R'], 1, [1, 5, 2, 3, 3]], [[0], [1, 2, 3, 4, 5], [6, 7], [9, 8, 10], [11, 12, 13]]), ('control layout', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('control layout', [[0, 3, 3, 3, 0, 0, 0, 0, 0, 0], ['L', 'WS', 'R', 'L', 'L', 'L', 'L', 'L', 'L', 'WS'], 1, [1, 4, 4, 1]], [[0], [3, 2, 1, 4], [5, 6, 7, 8], [9]])], [('regression: whitespace reset level', [[2, 2, 2, 2, 1, 1, 1, 3, 3, 3, 3, 1, 2, 2], ['ON', 'R', 'WS', 'R', 'L', 'R', 'R', 'R', 'R', 'L', 'R', 'R', 'WS', 'WS'], 1, [1, 1, 2, 3, 1, 2, 1, 3]], [[0], [1], [2, 3], [6, 5, 4], [7], [9, 8], [10], [13, 12, 11]]), ('regression: whitespace reset level', [[2, 2, 2, 2, 3, 3, 1], ['R', 'L', 'WS', 'WS', 'R', 'WS', 'WS'], 1, [4, 3]], [[3, 2, 0, 1], [6, 5, 4]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'R', 'WS', 'WS', 'WS'], 1, [5, 1]], [[4, 3, 2, 1, 0], [5]]), ('regression: whitespace reset level', [[1, 1, 1, 1, 1], ['WS', 'R', 'WS', 'L', 'L'], 1, [3, 1, 1]], [[2, 1, 0], [3], [4]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 3, 3, 3, 3, 3, 0], ['WS', 'WS', 'L', 'WS', 'R', 'L', 'WS', 'WS', 'R', 'WS'], 0, [4, 2, 2, 1, 1]], [[2, 1, 0, 3], [5, 4], [6, 7], [8], [9]]), ('control layout', [[1, 1, 1, 1], ['L', 'R', 'WS', 'ON'], 0, [1, 3]], [[0], [3, 2, 1]]), ('control layout', [[0, 0, 0, 0, 0], ['R', 'L', 'L', 'WS', 'WS'], 0, [4, 1]], [[0, 1, 2, 3], [4]])], [('regression: whitespace reset level', [[2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 1, 1], ['L', 'WS', 'L', 'WS', 'ON', 'WS', 'R', 'R', 'WS', 'WS', 'R', 'R'], 1, [5, 5, 1, 1]], [[0, 1, 2, 4, 3], [9, 8, 7, 6, 5], [10], [11]]), ('regression: whitespace reset level', [[2, 4, 1, 1, 1, 3], ['ON', 'L', 'ON', 'L', 'WS', 'R'], 1, [5, 1]], [[4, 3, 2, 0, 1], [5]]), ('regression: whitespace reset level', [[1, 1, 1, 3, 1, 0, 0, 0, 0], ['R', 'WS', 'WS', 'WS', 'R', 'L', 'L', 'L', 'R'], 1, [3, 5, 1]], [[2, 1, 0], [4, 3, 5, 6, 7], [8]]), ('regression: whitespace reset level', [[1, 1], ['WS', 'WS'], 1, [2]], [[1, 0]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('control layout', [[3, 3, 3, 3, 1, 1, 0, 0], ['WS', 'WS', 'R', 'L', 'R', 'WS', 'R', 'ON'], 1, [3, 1, 1, 3]], [[2, 1, 0], [3], [4], [5, 6, 7]]), ('control layout', [[0, 0, 0, 0, 0], ['R', 'L', 'L', 'WS', 'WS'], 0, [4, 1]], [[0, 1, 2, 3], [4]]), ('control layout', [[2, 0, 0, 0, 1, 2, 2, 2, 0, 0, 0, 0], ['R', 'WS', 'L', 'WS', 'WS', 'L', 'WS', 'L', 'WS', 'R', 'WS', 'ON'], 1, [3, 5, 4]], [[0, 1, 2], [3, 5, 6, 7, 4], [8, 9, 10, 11]])]]
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:
Trailing spaces on RTL paragraphs are reset to level 0.

Input [paragraph levels, classes, paragraph level, line lengths]. For each logical line: reset trailing WS to the paragraph level, then reorder that line alone by level reversal (lowest odd level of the line). Return per line the paragraph indices in visual order.

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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append(local)
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[4, 4, 1, 1, 2, 2, 2, 1, 1, 1, 1, 4, 4, 4], ['L', 'L', 'ON', 'R', 'WS', 'WS', 'WS', 'R', 'R', 'R', 'R', 'WS', 'L', 'WS'], 1, [3, 2, 5, 2, 1, 1]], [[2, 0, 1], [4, 3], [9, 8, 7, 5, 6], [11, 10], [12], [13]]), ('partial-repair probe', [[0, 0], ['L', 'L'], 0, [2]], [[0, 1]]), ('regression: line-local indices', [[1, 4, 4, 4, 3, 3, 3, 3, 2, 2], ['WS', 'R', 'R', 'ON', 'R', 'R', 'R', 'WS', 'WS', 'R'], 0, [3, 3, 2, 1, 1]], [[1, 2, 0], [5, 4, 3], [6, 7], [8], [9]]), ('regression: line-local indices', [[2, 2], ['L', 'L'], 1, [1, 1]], [[0], [1]]), ('regression: line-local indices', [[2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'ON', 'R', 'R', 'R', 'R', 'R', 'L', 'WS', 'WS'], 1, [4, 1, 5, 1]], [[3, 0, 1, 2], [4], [9, 8, 7, 6, 5], [10]]), ('regression: line-local indices', [[2, 2, 2, 2, 3, 3, 3, 3], ['R', 'WS', 'L', 'WS', 'R', 'R', 'L', 'L'], 1, [5, 1, 2]], [[0, 1, 2, 3, 4], [5], [7, 6]]), ('regression: line-local indices', [[3, 3, 2], ['WS', 'L', 'R'], 1, [2, 1]], [[1, 0], [2]])], [('regression: line-local indices', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('regression: line-local indices', [[0, 0, 0, 2, 2, 2, 2], ['R', 'L', 'ON', 'WS', 'L', 'L', 'R'], 0, [5, 2]], [[0, 1, 2, 3, 4], [5, 6]]), ('regression: line-local indices', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: line-local indices', [[2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'ON', 'R', 'R', 'R', 'R', 'R', 'L', 'WS', 'WS'], 1, [4, 1, 5, 1]], [[3, 0, 1, 2], [4], [9, 8, 7, 6, 5], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[3, 0], ['ON', 'WS'], 1, [1, 1]], [[0], [1]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4], ['WS', 'R', 'L', 'R', 'WS', 'L', 'ON', 'ON', 'R', 'ON', 'WS'], 0, [4, 2, 1, 1, 1, 2]], [[3, 2, 1, 0], [5, 4], [6], [7], [8], [9, 10]]), ('regression: line-local indices', [[0, 0, 0, 0, 0, 0], ['ON', 'L', 'L', 'L', 'WS', 'L'], 0, [4, 2]], [[0, 1, 2, 3], [4, 5]])], [('regression: line-local indices', [[2, 2, 1, 0, 0], ['WS', 'WS', 'L', 'WS', 'L'], 0, [3, 1, 1]], [[2, 0, 1], [3], [4]]), ('regression: line-local indices', [[1, 2, 2, 2, 2, 2, 2, 2], ['WS', 'R', 'R', 'L', 'L', 'R', 'R', 'R'], 1, [1, 1, 5, 1]], [[0], [1], [2, 3, 4, 5, 6], [7]]), ('regression: line-local indices', [[1, 1, 1, 1, 0], ['R', 'L', 'R', 'L', 'L'], 1, [4, 1]], [[3, 2, 1, 0], [4]]), ('regression: line-local indices', [[2, 4, 4, 4, 2, 1, 1, 3, 1, 1, 1, 1], ['R', 'L', 'ON', 'L', 'L', 'L', 'R', 'ON', 'WS', 'R', 'R', 'L'], 0, [4, 1, 2, 4, 1]], [[0, 1, 2, 3], [4], [6, 5], [10, 9, 8, 7], [11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[4, 4, 2, 2, 1, 1, 1, 3, 3], ['R', 'ON', 'L', 'R', 'L', 'R', 'WS', 'R', 'WS'], 0, [3, 1, 2, 1, 1, 1]], [[0, 1, 2], [3], [5, 4], [6], [7], [8]]), ('regression: line-local indices', [[1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 3, 3], ['WS', 'WS', 'L', 'WS', 'L', 'WS', 'L', 'R', 'WS', 'WS', 'WS', 'WS'], 0, [5, 5, 2]], [[4, 3, 2, 1, 0], [5, 6, 7, 8, 9], [10, 11]]), ('regression: line-local indices', [[0, 0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 1], ['L', 'R', 'WS', 'R', 'WS', 'L', 'R', 'WS', 'WS', 'L', 'L', 'ON', 'R'], 1, [3, 5, 5]], [[0, 1, 2], [7, 6, 5, 4, 3], [8, 9, 10, 11, 12]])], [('regression: line-local indices', [[4, 3, 1, 1, 1, 3, 3, 3, 2], ['L', 'L', 'R', 'ON', 'ON', 'R', 'L', 'WS', 'WS'], 0, [2, 3, 2, 2]], [[1, 0], [4, 3, 2], [6, 5], [7, 8]]), ('regression: line-local indices', [[1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 0, 0], ['L', 'L', 'R', 'L', 'L', 'R', 'L', 'L', 'L', 'L', 'R', 'L', 'R'], 0, [1, 1, 3, 3, 1, 4]], [[0], [1], [2, 3, 4], [7, 6, 5], [8], [10, 9, 11, 12]]), ('regression: line-local indices', [[0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 3], ['L', 'WS', 'L', 'L', 'L', 'L', 'R', 'L', 'R', 'WS', 'L', 'WS', 'L', 'L'], 0, [3, 2, 2, 2, 5]], [[0, 1, 2], [4, 3], [6, 5], [8, 7], [13, 12, 9, 10, 11]]), ('regression: line-local indices', [[0, 0, 0, 0, 1, 1, 1, 2, 2], ['L', 'R', 'R', 'ON', 'WS', 'R', 'R', 'WS', 'L'], 1, [5, 2, 1, 1]], [[0, 1, 2, 3, 4], [6, 5], [7], [8]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 3, 3, 2, 2], ['L', 'R', 'L', 'R', 'L', 'L', 'L', 'WS', 'ON'], 1, [3, 1, 1, 3, 1]], [[2, 1, 0], [3], [4], [7, 6, 5], [8]]), ('regression: line-local indices', [[1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3], ['WS', 'R', 'L', 'ON', 'R', 'R', 'R', 'L', 'L', 'L', 'R', 'L', 'WS', 'R'], 1, [1, 2, 2, 4, 4, 1]], [[0], [2, 1], [3, 4], [8, 7, 6, 5], [12, 11, 10, 9], [13]])], [('regression: line-local indices', [[1, 1, 1, 1, 1, 1], ['L', 'L', 'R', 'ON', 'ON', 'WS'], 1, [5, 1]], [[4, 3, 2, 1, 0], [5]]), ('regression: line-local indices', [[3, 3, 3, 3, 2, 2, 2, 2, 2], ['L', 'R', 'L', 'R', 'ON', 'L', 'ON', 'L', 'WS'], 1, [1, 1, 5, 1, 1]], [[0], [1], [3, 2, 4, 5, 6], [7], [8]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'L', 'ON', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 1, 5, 1, 1]], [[2, 1, 0], [3], [7, 6, 5, 4, 8], [9], [10]]), ('regression: line-local indices', [[2, 2, 3, 3, 3, 3, 4, 4, 4, 1, 1, 1], ['WS', 'L', 'R', 'WS', 'R', 'L', 'WS', 'L', 'R', 'R', 'R', 'R'], 0, [2, 4, 2, 2, 1, 1]], [[0, 1], [5, 4, 3, 2], [6, 7], [9, 8], [10], [11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[1, 1, 1, 3, 3, 3, 2, 2], ['ON', 'WS', 'ON', 'WS', 'WS', 'L', 'R', 'R'], 1, [2, 4, 1, 1]], [[1, 0], [5, 4, 3, 2], [6], [7]]), ('regression: line-local indices', [[0, 0, 0, 0], ['L', 'R', 'WS', 'R'], 0, [3, 1]], [[0, 1, 2], [3]]), ('regression: line-local indices', [[4, 4, 4, 1, 1, 2, 2, 2, 3, 3], ['WS', 'R', 'L', 'ON', 'R', 'R', 'R', 'L', 'R', 'R'], 1, [4, 3, 2, 1]], [[3, 0, 1, 2], [5, 6, 4], [7, 8], [9]])]]
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, classes, para, line_lengths = x
    out = []
    start = 0
    for ln in line_lengths:
        idx = list(range(start, start + ln))
        lv = [levels[i] for i in idx]
        j = ln - 1
        while j >= 0 and classes[idx[j]] == 'WS':
            lv[j] = para
            j -= 1
        local = reorder(lv)
        out.append([v + ln for v in local])
        start += ln
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[4, 4, 1, 1, 2, 2, 2, 1, 1, 1, 1, 4, 4, 4], ['L', 'L', 'ON', 'R', 'WS', 'WS', 'WS', 'R', 'R', 'R', 'R', 'WS', 'L', 'WS'], 1, [3, 2, 5, 2, 1, 1]], [[2, 0, 1], [4, 3], [9, 8, 7, 5, 6], [11, 10], [12], [13]]), ('partial-repair probe', [[0, 0], ['L', 'L'], 0, [2]], [[0, 1]]), ('regression: line-local indices', [[1, 4, 4, 4, 3, 3, 3, 3, 2, 2], ['WS', 'R', 'R', 'ON', 'R', 'R', 'R', 'WS', 'WS', 'R'], 0, [3, 3, 2, 1, 1]], [[1, 2, 0], [5, 4, 3], [6, 7], [8], [9]]), ('regression: line-local indices', [[2, 2], ['L', 'L'], 1, [1, 1]], [[0], [1]]), ('regression: line-local indices', [[2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'ON', 'R', 'R', 'R', 'R', 'R', 'L', 'WS', 'WS'], 1, [4, 1, 5, 1]], [[3, 0, 1, 2], [4], [9, 8, 7, 6, 5], [10]]), ('regression: line-local indices', [[2, 2, 2, 2, 3, 3, 3, 3], ['R', 'WS', 'L', 'WS', 'R', 'R', 'L', 'L'], 1, [5, 1, 2]], [[0, 1, 2, 3, 4], [5], [7, 6]]), ('regression: line-local indices', [[3, 3, 2], ['WS', 'L', 'R'], 1, [2, 1]], [[1, 0], [2]])], [('regression: line-local indices', [[2, 4, 4, 2, 2, 2, 1], ['R', 'WS', 'WS', 'WS', 'WS', 'ON', 'L'], 1, [4, 2, 1]], [[3, 2, 1, 0], [4, 5], [6]]), ('regression: line-local indices', [[0, 0, 0, 2, 2, 2, 2], ['R', 'L', 'ON', 'WS', 'L', 'L', 'R'], 0, [5, 2]], [[0, 1, 2, 3, 4], [5, 6]]), ('regression: line-local indices', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: line-local indices', [[2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1], ['WS', 'L', 'ON', 'R', 'R', 'R', 'R', 'R', 'L', 'WS', 'WS'], 1, [4, 1, 5, 1]], [[3, 0, 1, 2], [4], [9, 8, 7, 6, 5], [10]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[3, 0], ['ON', 'WS'], 1, [1, 1]], [[0], [1]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4], ['WS', 'R', 'L', 'R', 'WS', 'L', 'ON', 'ON', 'R', 'ON', 'WS'], 0, [4, 2, 1, 1, 1, 2]], [[3, 2, 1, 0], [5, 4], [6], [7], [8], [9, 10]]), ('regression: line-local indices', [[0, 0, 0, 0, 0, 0], ['ON', 'L', 'L', 'L', 'WS', 'L'], 0, [4, 2]], [[0, 1, 2, 3], [4, 5]])], [('regression: line-local indices', [[2, 2, 1, 0, 0], ['WS', 'WS', 'L', 'WS', 'L'], 0, [3, 1, 1]], [[2, 0, 1], [3], [4]]), ('regression: line-local indices', [[1, 2, 2, 2, 2, 2, 2, 2], ['WS', 'R', 'R', 'L', 'L', 'R', 'R', 'R'], 1, [1, 1, 5, 1]], [[0], [1], [2, 3, 4, 5, 6], [7]]), ('regression: line-local indices', [[1, 1, 1, 1, 0], ['R', 'L', 'R', 'L', 'L'], 1, [4, 1]], [[3, 2, 1, 0], [4]]), ('regression: line-local indices', [[2, 4, 4, 4, 2, 1, 1, 3, 1, 1, 1, 1], ['R', 'L', 'ON', 'L', 'L', 'L', 'R', 'ON', 'WS', 'R', 'R', 'L'], 0, [4, 1, 2, 4, 1]], [[0, 1, 2, 3], [4], [6, 5], [10, 9, 8, 7], [11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[4, 4, 2, 2, 1, 1, 1, 3, 3], ['R', 'ON', 'L', 'R', 'L', 'R', 'WS', 'R', 'WS'], 0, [3, 1, 2, 1, 1, 1]], [[0, 1, 2], [3], [5, 4], [6], [7], [8]]), ('regression: line-local indices', [[1, 1, 1, 1, 2, 0, 0, 0, 0, 0, 3, 3], ['WS', 'WS', 'L', 'WS', 'L', 'WS', 'L', 'R', 'WS', 'WS', 'WS', 'WS'], 0, [5, 5, 2]], [[4, 3, 2, 1, 0], [5, 6, 7, 8, 9], [10, 11]]), ('regression: line-local indices', [[0, 0, 0, 3, 3, 3, 3, 3, 0, 0, 0, 0, 1], ['L', 'R', 'WS', 'R', 'WS', 'L', 'R', 'WS', 'WS', 'L', 'L', 'ON', 'R'], 1, [3, 5, 5]], [[0, 1, 2], [7, 6, 5, 4, 3], [8, 9, 10, 11, 12]])], [('regression: line-local indices', [[4, 3, 1, 1, 1, 3, 3, 3, 2], ['L', 'L', 'R', 'ON', 'ON', 'R', 'L', 'WS', 'WS'], 0, [2, 3, 2, 2]], [[1, 0], [4, 3, 2], [6, 5], [7, 8]]), ('regression: line-local indices', [[1, 2, 2, 2, 3, 3, 3, 1, 1, 1, 1, 0, 0], ['L', 'L', 'R', 'L', 'L', 'R', 'L', 'L', 'L', 'L', 'R', 'L', 'R'], 0, [1, 1, 3, 3, 1, 4]], [[0], [1], [2, 3, 4], [7, 6, 5], [8], [10, 9, 11, 12]]), ('regression: line-local indices', [[0, 0, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 1, 3], ['L', 'WS', 'L', 'L', 'L', 'L', 'R', 'L', 'R', 'WS', 'L', 'WS', 'L', 'L'], 0, [3, 2, 2, 2, 5]], [[0, 1, 2], [4, 3], [6, 5], [8, 7], [13, 12, 9, 10, 11]]), ('regression: line-local indices', [[0, 0, 0, 0, 1, 1, 1, 2, 2], ['L', 'R', 'R', 'ON', 'WS', 'R', 'R', 'WS', 'L'], 1, [5, 2, 1, 1]], [[0, 1, 2, 3, 4], [6, 5], [7], [8]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[2, 2, 2, 2, 2, 2], ['ON', 'WS', 'R', 'L', 'ON', 'WS'], 0, [2, 2, 1, 1]], [[0, 1], [2, 3], [4], [5]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 3, 3, 2, 2], ['L', 'R', 'L', 'R', 'L', 'L', 'L', 'WS', 'ON'], 1, [3, 1, 1, 3, 1]], [[2, 1, 0], [3], [4], [7, 6, 5], [8]]), ('regression: line-local indices', [[1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 1, 3, 3, 3], ['WS', 'R', 'L', 'ON', 'R', 'R', 'R', 'L', 'L', 'L', 'R', 'L', 'WS', 'R'], 1, [1, 2, 2, 4, 4, 1]], [[0], [2, 1], [3, 4], [8, 7, 6, 5], [12, 11, 10, 9], [13]])], [('regression: line-local indices', [[1, 1, 1, 1, 1, 1], ['L', 'L', 'R', 'ON', 'ON', 'WS'], 1, [5, 1]], [[4, 3, 2, 1, 0], [5]]), ('regression: line-local indices', [[3, 3, 3, 3, 2, 2, 2, 2, 2], ['L', 'R', 'L', 'R', 'ON', 'L', 'ON', 'L', 'WS'], 1, [1, 1, 5, 1, 1]], [[0], [1], [3, 2, 4, 5, 6], [7], [8]]), ('regression: line-local indices', [[1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0], ['R', 'R', 'WS', 'WS', 'L', 'ON', 'WS', 'WS', 'L', 'R', 'L'], 1, [3, 1, 5, 1, 1]], [[2, 1, 0], [3], [7, 6, 5, 4, 8], [9], [10]]), ('regression: line-local indices', [[2, 2, 3, 3, 3, 3, 4, 4, 4, 1, 1, 1], ['WS', 'L', 'R', 'WS', 'R', 'L', 'WS', 'L', 'R', 'R', 'R', 'R'], 0, [2, 4, 2, 2, 1, 1]], [[0, 1], [5, 4, 3, 2], [6, 7], [9, 8], [10], [11]]), ('RTL run split across lines', [[1, 1, 1, 1, 1, 1], ['R', 'R', 'WS', 'R', 'R', 'R'], 0, [3, 3]], [[1, 0, 2], [5, 4, 3]]), ('regression: line-local indices', [[1, 1, 1, 3, 3, 3, 2, 2], ['ON', 'WS', 'ON', 'WS', 'WS', 'L', 'R', 'R'], 1, [2, 4, 1, 1]], [[1, 0], [5, 4, 3, 2], [6], [7]]), ('regression: line-local indices', [[0, 0, 0, 0], ['L', 'R', 'WS', 'R'], 0, [3, 1]], [[0, 1, 2], [3]]), ('regression: line-local indices', [[4, 4, 4, 1, 1, 2, 2, 2, 3, 3], ['WS', 'R', 'L', 'ON', 'R', 'R', 'R', 'L', 'R', 'R'], 1, [4, 3, 2, 1]], [[3, 0, 1, 2], [5, 6, 4], [7, 8], [9]])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Visual orders for later lines refer to the first line.

Input [paragraph levels, classes, paragraph level, line lengths]. For each logical line: reset trailing WS to the paragraph level, then reorder that line alone by level reversal (lowest odd level of the line). Return per line the paragraph indices in visual order.

split: open-access


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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL end is left', [20, 5, 'end', True], 0), ('regression: end alignment mapping', [36, 28, 'end', True], 0), ('partial-repair probe', [16, 28, 'end', False], -12), ('partial-repair probe', [21, 46, 'end', False], -25), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [17, 39, 'center', True], -22), ('control layout', [37, 31, 'justify', True], 6)], [('regression: end alignment mapping', [37, 25, 'end', True], 0), ('regression: end alignment mapping', [22, 30, 'end', True], 0), ('regression: end alignment mapping', [11, 26, 'end', True], 0), ('partial-repair probe', [13, 1, 'end', False], 12), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [23, 3, 'justify', True], 20), ('control layout', [29, 29, 'justify', False], 0)], [('regression: end alignment mapping', [18, 8, 'end', True], 0), ('regression: end alignment mapping', [36, 9, 'end', True], 0), ('partial-repair probe', [38, 19, 'end', False], 19), ('partial-repair probe', [27, 20, 'end', False], 7), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [20, 40, 'start', False], 0), ('control layout', [15, 28, 'center', False], 0)], [('regression: end alignment mapping', [11, 34, 'end', True], 0), ('regression: end alignment mapping', [37, 25, 'end', True], 0), ('regression: end alignment mapping', [21, 8, 'end', True], 0), ('partial-repair probe', [33, 31, 'end', False], 2), ('odd slack RTL center', [20, 5, 'center', True], 8), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [35, 16, 'right', True], 19), ('control layout', [21, 20, 'left', False], 0)], [('regression: end alignment mapping', [35, 50, 'end', True], 0), ('regression: end alignment mapping', [20, 7, 'end', True], 0), ('partial-repair probe', [33, 31, 'end', False], 2), ('regression: end alignment mapping', [32, 30, 'end', True], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [10, 37, 'start', True], -27), ('control layout', [35, 3, 'center', True], 16)]]
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):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'right' if rtl else 'left'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('RTL end is left', [20, 5, 'end', True], 0), ('regression: end alignment mapping', [36, 28, 'end', True], 0), ('partial-repair probe', [16, 28, 'end', False], -12), ('partial-repair probe', [21, 46, 'end', False], -25), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [17, 39, 'center', True], -22), ('control layout', [37, 31, 'justify', True], 6)], [('regression: end alignment mapping', [37, 25, 'end', True], 0), ('regression: end alignment mapping', [22, 30, 'end', True], 0), ('regression: end alignment mapping', [11, 26, 'end', True], 0), ('partial-repair probe', [13, 1, 'end', False], 12), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [23, 3, 'justify', True], 20), ('control layout', [29, 29, 'justify', False], 0)], [('regression: end alignment mapping', [18, 8, 'end', True], 0), ('regression: end alignment mapping', [36, 9, 'end', True], 0), ('partial-repair probe', [38, 19, 'end', False], 19), ('partial-repair probe', [27, 20, 'end', False], 7), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [20, 40, 'start', False], 0), ('control layout', [15, 28, 'center', False], 0)], [('regression: end alignment mapping', [11, 34, 'end', True], 0), ('regression: end alignment mapping', [37, 25, 'end', True], 0), ('regression: end alignment mapping', [21, 8, 'end', True], 0), ('partial-repair probe', [33, 31, 'end', False], 2), ('odd slack RTL center', [20, 5, 'center', True], 8), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [35, 16, 'right', True], 19), ('control layout', [21, 20, 'left', False], 0)], [('regression: end alignment mapping', [35, 50, 'end', True], 0), ('regression: end alignment mapping', [20, 7, 'end', True], 0), ('partial-repair probe', [33, 31, 'end', False], 2), ('regression: end alignment mapping', [32, 30, 'end', True], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [10, 37, 'start', True], -27), ('control layout', [35, 3, 'center', True], 16)]]
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:
End-aligned RTL text sits on the right.

Input [line width, content width, align, rtl]. justify (last line) behaves as start. start = left for LTR, right for RTL; end is the opposite. Overflowing centered content aligns to start. center offset is floor(slack/2) for LTR and ceil(slack/2) for RTL. Return the x offset of the content.

split: open-access


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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: start alignment mapping', [37, 31, 'justify', True], 6), ('regression: start alignment mapping', [10, 24, 'start', True], -14), ('partial-repair probe', [21, 5, 'justify', False], 0), ('regression: start alignment mapping', [22, 44, 'justify', True], -22), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [37, 2, 'right', True], 35), ('control layout', [36, 43, 'center', True], -7)], [('regression: start alignment mapping', [36, 5, 'start', True], 31), ('regression: start alignment mapping', [28, 33, 'start', True], -5), ('partial-repair probe', [11, 47, 'justify', False], 0), ('partial-repair probe', [34, 47, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 43, 'end', False], -10), ('control layout', [14, 28, 'end', False], -14)], [('regression: start alignment mapping', [28, 41, 'start', True], -13), ('regression: start alignment mapping', [22, 27, 'justify', True], -5), ('partial-repair probe', [15, 28, 'start', False], 0), ('regression: start alignment mapping', [21, 43, 'justify', True], -22), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [16, 22, 'right', True], -6), ('control layout', [12, 33, 'right', True], -21)], [('regression: start alignment mapping', [18, 12, 'justify', True], 6), ('regression: start alignment mapping', [16, 37, 'justify', True], -21), ('partial-repair probe', [35, 1, 'start', False], 0), ('regression: start alignment mapping', [38, 35, 'justify', True], 3), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [34, 34, 'right', False], 0), ('control layout', [11, 26, 'end', True], 0)], [('regression: start alignment mapping', [37, 26, 'start', True], 11), ('regression: start alignment mapping', [18, 41, 'justify', True], -23), ('partial-repair probe', [20, 46, 'start', False], 0), ('partial-repair probe', [29, 49, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [27, 32, 'center', False], 0), ('control layout', [36, 1, 'left', True], 0)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if not rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: start alignment mapping', [37, 31, 'justify', True], 6), ('regression: start alignment mapping', [10, 24, 'start', True], -14), ('partial-repair probe', [21, 5, 'justify', False], 0), ('regression: start alignment mapping', [22, 44, 'justify', True], -22), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [37, 2, 'right', True], 35), ('control layout', [36, 43, 'center', True], -7)], [('regression: start alignment mapping', [36, 5, 'start', True], 31), ('regression: start alignment mapping', [28, 33, 'start', True], -5), ('partial-repair probe', [11, 47, 'justify', False], 0), ('partial-repair probe', [34, 47, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 43, 'end', False], -10), ('control layout', [14, 28, 'end', False], -14)], [('regression: start alignment mapping', [28, 41, 'start', True], -13), ('regression: start alignment mapping', [22, 27, 'justify', True], -5), ('partial-repair probe', [15, 28, 'start', False], 0), ('regression: start alignment mapping', [21, 43, 'justify', True], -22), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [16, 22, 'right', True], -6), ('control layout', [12, 33, 'right', True], -21)], [('regression: start alignment mapping', [18, 12, 'justify', True], 6), ('regression: start alignment mapping', [16, 37, 'justify', True], -21), ('partial-repair probe', [35, 1, 'start', False], 0), ('regression: start alignment mapping', [38, 35, 'justify', True], 3), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [34, 34, 'right', False], 0), ('control layout', [11, 26, 'end', True], 0)], [('regression: start alignment mapping', [37, 26, 'start', True], 11), ('regression: start alignment mapping', [18, 41, 'justify', True], -23), ('partial-repair probe', [20, 46, 'start', False], 0), ('partial-repair probe', [29, 49, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [27, 32, 'center', False], 0), ('control layout', [36, 1, 'left', True], 0)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Start-aligned RTL paragraphs are left aligned.

Input [line width, content width, align, rtl]. justify (last line) behaves as start. start = left for LTR, right for RTL; end is the opposite. Overflowing centered content aligns to start. center offset is floor(slack/2) for LTR and ceil(slack/2) for RTL. Return the x offset of the content.

split: open-access


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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'start':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('overflowing center RTL', [10, 15, 'center', True], -5), ('regression: overflowing center fallback', [17, 50, 'center', False], 0), ('regression: overflowing center fallback', [17, 45, 'center', True], -28), ('regression: overflowing center fallback', [29, 32, 'center', True], -3), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [37, 42, 'start', False], 0), ('control layout', [36, 39, 'right', False], -3)], [('regression: overflowing center fallback', [13, 20, 'center', False], 0), ('regression: overflowing center fallback', [26, 42, 'center', False], 0), ('regression: overflowing center fallback', [37, 38, 'center', True], -1), ('regression: overflowing center fallback', [12, 13, 'center', True], -1), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [29, 31, 'left', False], 0), ('control layout', [37, 29, 'left', True], 0)], [('regression: overflowing center fallback', [10, 49, 'center', True], -39), ('overflowing center RTL', [10, 15, 'center', True], -5), ('regression: overflowing center fallback', [19, 31, 'center', True], -12), ('regression: overflowing center fallback', [24, 25, 'center', True], -1), ('odd slack RTL center', [20, 5, 'center', True], 8), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [26, 13, 'left', True], 0), ('control layout', [36, 2, 'left', False], 0)], [('regression: overflowing center fallback', [37, 38, 'center', True], -1), ('regression: overflowing center fallback', [13, 28, 'center', False], 0), ('regression: overflowing center fallback', [32, 42, 'center', True], -10), ('regression: overflowing center fallback', [21, 22, 'center', True], -1), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [22, 27, 'justify', True], -5), ('control layout', [10, 19, 'left', False], 0)], [('regression: overflowing center fallback', [24, 33, 'center', False], 0), ('regression: overflowing center fallback', [15, 28, 'center', False], 0), ('regression: overflowing center fallback', [17, 28, 'center', True], -11), ('regression: overflowing center fallback', [19, 25, 'center', True], -6), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [17, 26, 'end', True], 0), ('control layout', [23, 9, 'start', True], 14)]]
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):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('overflowing center RTL', [10, 15, 'center', True], -5), ('regression: overflowing center fallback', [17, 50, 'center', False], 0), ('regression: overflowing center fallback', [17, 45, 'center', True], -28), ('regression: overflowing center fallback', [29, 32, 'center', True], -3), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [37, 42, 'start', False], 0), ('control layout', [36, 39, 'right', False], -3)], [('regression: overflowing center fallback', [13, 20, 'center', False], 0), ('regression: overflowing center fallback', [26, 42, 'center', False], 0), ('regression: overflowing center fallback', [37, 38, 'center', True], -1), ('regression: overflowing center fallback', [12, 13, 'center', True], -1), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [29, 31, 'left', False], 0), ('control layout', [37, 29, 'left', True], 0)], [('regression: overflowing center fallback', [10, 49, 'center', True], -39), ('overflowing center RTL', [10, 15, 'center', True], -5), ('regression: overflowing center fallback', [19, 31, 'center', True], -12), ('regression: overflowing center fallback', [24, 25, 'center', True], -1), ('odd slack RTL center', [20, 5, 'center', True], 8), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [26, 13, 'left', True], 0), ('control layout', [36, 2, 'left', False], 0)], [('regression: overflowing center fallback', [37, 38, 'center', True], -1), ('regression: overflowing center fallback', [13, 28, 'center', False], 0), ('regression: overflowing center fallback', [32, 42, 'center', True], -10), ('regression: overflowing center fallback', [21, 22, 'center', True], -1), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [22, 27, 'justify', True], -5), ('control layout', [10, 19, 'left', False], 0)], [('regression: overflowing center fallback', [24, 33, 'center', False], 0), ('regression: overflowing center fallback', [15, 28, 'center', False], 0), ('regression: overflowing center fallback', [17, 28, 'center', True], -11), ('regression: overflowing center fallback', [19, 25, 'center', True], -6), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [17, 26, 'end', True], 0), ('control layout', [23, 9, 'start', True], 14)]]
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:
Overflowing centered lines hang off both edges.

Input [line width, content width, align, rtl]. justify (last line) behaves as start. start = left for LTR, right for RTL; end is the opposite. Overflowing centered content aligns to start. center offset is floor(slack/2) for LTR and ceil(slack/2) for RTL. Return the x offset of the content.

split: open-access


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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('odd slack RTL center', [20, 5, 'center', True], 8), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [22, 5, 'center', False], 8), ('partial-repair probe', [39, 34, 'center', False], 2), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [23, 19, 'left', True], 0), ('control layout', [28, 30, 'left', False], 0)], [('regression: center rounding by direction', [33, 4, 'center', True], 15), ('regression: center rounding by direction', [23, 12, 'center', True], 6), ('partial-repair probe', [39, 34, 'center', False], 2), ('partial-repair probe', [37, 14, 'center', False], 11), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [23, 29, 'left', True], 0), ('control layout', [31, 48, 'right', False], -17)], [('regression: center rounding by direction', [40, 5, 'center', True], 18), ('regression: center rounding by direction', [33, 4, 'center', True], 15), ('partial-repair probe', [29, 18, 'center', False], 5), ('partial-repair probe', [22, 5, 'center', False], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 1, 'justify', False], 0), ('control layout', [25, 40, 'left', False], 0)], [('odd slack RTL center', [20, 5, 'center', True], 8), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [19, 4, 'center', False], 7), ('partial-repair probe', [22, 5, 'center', False], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [26, 12, 'right', False], 14), ('control layout', [20, 47, 'right', True], -27)], [('regression: center rounding by direction', [17, 8, 'center', True], 5), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [39, 34, 'center', False], 2), ('partial-repair probe', [29, 18, 'center', False], 5), ('overflowing center RTL', [10, 15, 'center', True], -5), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 4, 'left', False], 0), ('control layout', [17, 4, 'end', True], 0)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'start'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('odd slack RTL center', [20, 5, 'center', True], 8), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [22, 5, 'center', False], 8), ('partial-repair probe', [39, 34, 'center', False], 2), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [23, 19, 'left', True], 0), ('control layout', [28, 30, 'left', False], 0)], [('regression: center rounding by direction', [33, 4, 'center', True], 15), ('regression: center rounding by direction', [23, 12, 'center', True], 6), ('partial-repair probe', [39, 34, 'center', False], 2), ('partial-repair probe', [37, 14, 'center', False], 11), ('RTL end is left', [20, 5, 'end', True], 0), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [23, 29, 'left', True], 0), ('control layout', [31, 48, 'right', False], -17)], [('regression: center rounding by direction', [40, 5, 'center', True], 18), ('regression: center rounding by direction', [33, 4, 'center', True], 15), ('partial-repair probe', [29, 18, 'center', False], 5), ('partial-repair probe', [22, 5, 'center', False], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 1, 'justify', False], 0), ('control layout', [25, 40, 'left', False], 0)], [('odd slack RTL center', [20, 5, 'center', True], 8), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [19, 4, 'center', False], 7), ('partial-repair probe', [22, 5, 'center', False], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('RTL end is left', [20, 5, 'end', True], 0), ('control layout', [26, 12, 'right', False], 14), ('control layout', [20, 47, 'right', True], -27)], [('regression: center rounding by direction', [17, 8, 'center', True], 5), ('regression: center rounding by direction', [35, 32, 'center', True], 2), ('partial-repair probe', [39, 34, 'center', False], 2), ('partial-repair probe', [29, 18, 'center', False], 5), ('overflowing center RTL', [10, 15, 'center', True], -5), ('odd slack RTL center', [20, 5, 'center', True], 8), ('control layout', [33, 4, 'left', False], 0), ('control layout', [17, 4, 'end', True], 0)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Odd slack in RTL lines is split toward the wrong side.

Input [line width, content width, align, rtl]. justify (last line) behaves as start. start = left for LTR, right for RTL; end is the opposite. Overflowing centered content aligns to start. center offset is floor(slack/2) for LTR and ceil(slack/2) for RTL. Return the x offset of the content.

split: open-access


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

N = 1
observations = []
def solve(x):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'left'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: justified last line', [13, 48, 'justify', True], -35), ('regression: justified last line', [22, 44, 'justify', True], -22), ('regression: justified last line', [18, 12, 'justify', True], 6), ('regression: justified last line', [33, 4, 'justify', True], 29), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [36, 2, 'center', True], 17), ('control layout', [17, 15, 'left', True], 0)], [('regression: justified last line', [27, 25, 'justify', True], 2), ('regression: justified last line', [21, 48, 'justify', True], -27), ('partial-repair probe', [36, 23, 'justify', False], 0), ('partial-repair probe', [31, 16, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [30, 30, 'end', True], 0), ('control layout', [30, 34, 'justify', False], 0)], [('regression: justified last line', [37, 31, 'justify', True], 6), ('regression: justified last line', [12, 31, 'justify', True], -19), ('regression: justified last line', [17, 9, 'justify', True], 8), ('regression: justified last line', [38, 35, 'justify', True], 3), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [22, 21, 'right', False], 1), ('control layout', [16, 28, 'start', False], 0)], [('regression: justified last line', [23, 3, 'justify', True], 20), ('regression: justified last line', [29, 38, 'justify', True], -9), ('partial-repair probe', [26, 19, 'justify', False], 0), ('regression: justified last line', [38, 31, 'justify', True], 7), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [17, 15, 'left', True], 0), ('control layout', [21, 8, 'end', True], 0)], [('regression: justified last line', [38, 10, 'justify', True], 28), ('regression: justified last line', [38, 31, 'justify', True], 7), ('regression: justified last line', [17, 9, 'justify', True], 8), ('regression: justified last line', [37, 31, 'justify', True], 6), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [24, 27, 'start', True], -3), ('control layout', [24, 46, 'start', True], -22)]]
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):
    lw, cw, align, rtl = x
    if align == 'justify':
        align = 'center'
    if align == 'start':
        align = 'right' if rtl else 'left'
    elif align == 'end':
        align = 'left' if rtl else 'right'
    if cw > lw and align == 'center':
        align = 'left' if not rtl else 'right'
    if align == 'left':
        return 0
    if align == 'right':
        return lw - cw
    slack = lw - cw
    return (slack + 1) // 2 if rtl else slack // 2
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: justified last line', [13, 48, 'justify', True], -35), ('regression: justified last line', [22, 44, 'justify', True], -22), ('regression: justified last line', [18, 12, 'justify', True], 6), ('regression: justified last line', [33, 4, 'justify', True], 29), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [36, 2, 'center', True], 17), ('control layout', [17, 15, 'left', True], 0)], [('regression: justified last line', [27, 25, 'justify', True], 2), ('regression: justified last line', [21, 48, 'justify', True], -27), ('partial-repair probe', [36, 23, 'justify', False], 0), ('partial-repair probe', [31, 16, 'justify', False], 0), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [30, 30, 'end', True], 0), ('control layout', [30, 34, 'justify', False], 0)], [('regression: justified last line', [37, 31, 'justify', True], 6), ('regression: justified last line', [12, 31, 'justify', True], -19), ('regression: justified last line', [17, 9, 'justify', True], 8), ('regression: justified last line', [38, 35, 'justify', True], 3), ('RTL end is left', [20, 5, 'end', True], 0), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [22, 21, 'right', False], 1), ('control layout', [16, 28, 'start', False], 0)], [('regression: justified last line', [23, 3, 'justify', True], 20), ('regression: justified last line', [29, 38, 'justify', True], -9), ('partial-repair probe', [26, 19, 'justify', False], 0), ('regression: justified last line', [38, 31, 'justify', True], 7), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [17, 15, 'left', True], 0), ('control layout', [21, 8, 'end', True], 0)], [('regression: justified last line', [38, 10, 'justify', True], 28), ('regression: justified last line', [38, 31, 'justify', True], 7), ('regression: justified last line', [17, 9, 'justify', True], 8), ('regression: justified last line', [37, 31, 'justify', True], 6), ('odd slack RTL center', [20, 5, 'center', True], 8), ('overflowing center RTL', [10, 15, 'center', True], -5), ('control layout', [24, 27, 'start', True], -3), ('control layout', [24, 46, 'start', True], -22)]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The last line of a justified RTL paragraph sits on the left.

Input [line width, content width, align, rtl]. justify (last line) behaves as start. start = left for LTR, right for RTL; end is the opposite. Overflowing centered content aligns to start. center offset is floor(slack/2) for LTR and ceil(slack/2) for RTL. Return the x offset of the content.

split: open-access