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

N = 1
observations = []
def solve(x):
    words, width, cap_lines, cap_width, gap, indent = x
    lens = list(words)
    if cap_lines > 0:
        lens[0] = lens[0] - 1
    def avail(li):
        base = width
        if li < cap_lines:
            base -= cap_width + gap
        elif cap_lines == 0 and li == 0:
            base -= indent
        return base
    lines = []
    cur = []
    used = 0
    for w in lens:
        li = len(lines)
        need = w if not cur else used + 1 + w
        if need <= avail(li):
            cur.append(w)
            used = need
        else:
            lines.append([len(cur), used])
            cur = [w]
            used = w
    if cur:
        lines.append([len(cur), used])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[6, 6, 2, 2, 1], 12, 3, 5, 1, 3], [[1, 5], [1, 6], [2, 5], [1, 1]]), ('partial-repair probe', [[2, 2, 3, 7, 1, 1, 4, 6, 2, 2], 22, 0, 3, 1, 1], [[6, 21], [4, 17]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[5, 5, 3, 1], 24, 0, 5, 1, 4], [[4, 17]]), ('control layout', [[1, 6, 1, 5], 22, 0, 3, 2, 1], [[4, 16]])], [('regression: overwide word placement', [[8, 7, 4, 7, 5, 3, 4], 12, 2, 4, 2, 4], [[1, 7], [1, 7], [2, 12], [2, 9], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[6, 6, 4, 3, 1, 1, 6, 2, 3], 21, 3, 3, 1, 2], [[3, 17], [5, 17], [1, 3]]), ('partial-repair probe', [[2, 3, 3, 8, 8, 2, 7, 8, 7, 6, 1], 15, 0, 2, 1, 1], [[3, 10], [1, 8], [2, 11], [1, 7], [1, 8], [2, 14], [1, 1]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('control layout', [[7, 3, 6, 1], 23, 0, 4, 1, 1], [[4, 20]]), ('control layout', [[3, 2, 3, 6, 1], 22, 0, 4, 1, 3], [[5, 19]])], [('regression: overwide word placement', [[8, 7, 4, 7, 5, 3, 4], 12, 2, 4, 2, 4], [[1, 7], [1, 7], [2, 12], [2, 9], [1, 4]]), ('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('partial-repair probe', [[2, 2, 2, 3, 8, 4, 3, 5, 4, 4], 15, 0, 2, 1, 2], [[4, 12], [2, 13], [3, 14], [1, 4]]), ('partial-repair probe', [[2, 2, 3, 7, 3, 4], 13, 2, 4, 1, 4], [[3, 8], [1, 7], [2, 8]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[6, 5, 1, 3], 24, 2, 5, 1, 4], [[4, 17]]), ('control layout', [[4, 1, 2, 2, 2, 1], 21, 0, 5, 2, 3], [[6, 17]])], [('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[7, 5, 7, 4, 5, 4, 7], 14, 2, 4, 2, 2], [[1, 6], [1, 5], [2, 12], [2, 10], [1, 7]]), ('partial-repair probe', [[3, 6, 7, 8, 3, 7], 14, 3, 2, 2, 1], [[2, 9], [1, 7], [1, 8], [2, 11]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[3, 1, 1, 3, 4], 18, 0, 3, 1, 2], [[5, 16]]), ('control layout', [[5, 5, 3, 1], 24, 0, 5, 1, 4], [[4, 17]])], [('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('partial-repair probe', [[6, 2, 7, 1, 8, 8], 17, 0, 5, 2, 1], [[2, 9], [2, 9], [2, 17]]), ('partial-repair probe', [[8, 3, 1, 2, 7, 3, 4, 3, 4, 5], 15, 0, 2, 1, 1], [[3, 14], [3, 14], [3, 13], [1, 5]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[1, 4, 6, 3], 21, 0, 5, 1, 2], [[4, 17]]), ('control layout', [[8, 4, 1, 2, 1], 23, 0, 3, 1, 1], [[5, 20]])]]
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):
    words, width, cap_lines, cap_width, gap, indent = x
    lens = list(words)
    if cap_lines > 0:
        lens[0] = lens[0] - 1
    def avail(li):
        base = width
        if li < cap_lines:
            base -= cap_width + gap
        elif cap_lines == 0 and li == 0:
            base -= indent
        return base
    lines = []
    cur = []
    used = 0
    for w in lens:
        li = len(lines)
        need = w if not cur else used + 1 + w
        if need <= avail(li) or len(lines) == 0:
            cur.append(w)
            used = need
        else:
            lines.append([len(cur), used])
            cur = [w]
            used = w
    if cur:
        lines.append([len(cur), used])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[6, 6, 2, 2, 1], 12, 3, 5, 1, 3], [[1, 5], [1, 6], [2, 5], [1, 1]]), ('partial-repair probe', [[2, 2, 3, 7, 1, 1, 4, 6, 2, 2], 22, 0, 3, 1, 1], [[6, 21], [4, 17]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[5, 5, 3, 1], 24, 0, 5, 1, 4], [[4, 17]]), ('control layout', [[1, 6, 1, 5], 22, 0, 3, 2, 1], [[4, 16]])], [('regression: overwide word placement', [[8, 7, 4, 7, 5, 3, 4], 12, 2, 4, 2, 4], [[1, 7], [1, 7], [2, 12], [2, 9], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[6, 6, 4, 3, 1, 1, 6, 2, 3], 21, 3, 3, 1, 2], [[3, 17], [5, 17], [1, 3]]), ('partial-repair probe', [[2, 3, 3, 8, 8, 2, 7, 8, 7, 6, 1], 15, 0, 2, 1, 1], [[3, 10], [1, 8], [2, 11], [1, 7], [1, 8], [2, 14], [1, 1]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('control layout', [[7, 3, 6, 1], 23, 0, 4, 1, 1], [[4, 20]]), ('control layout', [[3, 2, 3, 6, 1], 22, 0, 4, 1, 3], [[5, 19]])], [('regression: overwide word placement', [[8, 7, 4, 7, 5, 3, 4], 12, 2, 4, 2, 4], [[1, 7], [1, 7], [2, 12], [2, 9], [1, 4]]), ('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('partial-repair probe', [[2, 2, 2, 3, 8, 4, 3, 5, 4, 4], 15, 0, 2, 1, 2], [[4, 12], [2, 13], [3, 14], [1, 4]]), ('partial-repair probe', [[2, 2, 3, 7, 3, 4], 13, 2, 4, 1, 4], [[3, 8], [1, 7], [2, 8]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[6, 5, 1, 3], 24, 2, 5, 1, 4], [[4, 17]]), ('control layout', [[4, 1, 2, 2, 2, 1], 21, 0, 5, 2, 3], [[6, 17]])], [('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('partial-repair probe', [[7, 5, 7, 4, 5, 4, 7], 14, 2, 4, 2, 2], [[1, 6], [1, 5], [2, 12], [2, 10], [1, 7]]), ('partial-repair probe', [[3, 6, 7, 8, 3, 7], 14, 3, 2, 2, 1], [[2, 9], [1, 7], [1, 8], [2, 11]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[3, 1, 1, 3, 4], 18, 0, 3, 1, 2], [[5, 16]]), ('control layout', [[5, 5, 3, 1], 24, 0, 5, 1, 4], [[4, 17]])], [('regression: overwide word placement', [[8, 1, 1, 8, 3], 12, 3, 4, 2, 2], [[1, 7], [2, 3], [1, 8], [1, 3]]), ('regression: overwide word placement', [[8, 7, 3, 8, 8, 6, 3, 4], 12, 3, 5, 1, 2], [[1, 7], [1, 7], [1, 3], [1, 8], [1, 8], [2, 10], [1, 4]]), ('partial-repair probe', [[6, 2, 7, 1, 8, 8], 17, 0, 5, 2, 1], [[2, 9], [2, 9], [2, 17]]), ('partial-repair probe', [[8, 3, 1, 2, 7, 3, 4, 3, 4, 5], 15, 0, 2, 1, 1], [[3, 14], [3, 14], [3, 13], [1, 5]]), ('three-line drop cap', [[5, 3, 4, 6, 2, 5, 4, 3], 14, 3, 3, 1, 2], [[2, 8], [1, 4], [2, 9], [3, 14]]), ('no cap uses indent', [[5, 3, 4, 6, 2], 12, 0, 3, 1, 3], [[2, 9], [2, 11], [1, 2]]), ('control layout', [[1, 4, 6, 3], 21, 0, 5, 1, 2], [[4, 17]]), ('control layout', [[8, 4, 1, 2, 1], 23, 0, 3, 1, 1], [[5, 20]])]]
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:
Empty lines are emitted before words wider than the shaped measure.

Input [word widths, width, cap lines, cap width, gap, indent]. With a drop cap its first letter moves into the cap (first word loses one unit) and the first cap-lines lines are narrowed by cap width + gap; the first-line indent applies only without a cap. Greedy fill with unit spaces; a word too wide for an empty line is set alone. Return [[words on line, used width]].

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) <= min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: runt length threshold', [['the', 'is', 'grid', 'at', 'at', 'a', 'is'], 18, 7], ['the is grid at', 'at a is']), ('regression: runt length threshold', [['kerning', 'a', 'a', 'ink', 'x', 'is', 'on', 'type', 'grid', 'of', 'x'], 11, 4], ['kerning a a', 'ink x is on', 'type grid', 'of x']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['a', 'of', 'is', 'a', 'at', 'on', 'at', 'baseline', 'at', 'on'], 13, 3], ['a of is a at', 'on at', 'baseline', 'at on']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['measure', 'kerning', 'is', 'a', 'is', 'of', 'at', 'of', 'a', 'of'], 7, 4], ['measure', 'kerning', 'is a is', 'of at', 'of a of']), ('control layout', [['type', 'the', 'baseline', 'a', 'grid', 'x', 'ink', 'of'], 20, 9], ['type the baseline a', 'grid x ink of'])], [('regression: runt length threshold', [['a', 'at', 'is', 'on', 'baseline'], 17, 8], ['a at is on', 'baseline']), ('regression: runt length threshold', [['serif', 'baseline', 'x', 'ink', 'at', 'on', 'on', 'a', 'at'], 16, 4], ['serif baseline x', 'ink at on on', 'a at']), ('partial-repair probe', [['grid', 'the', 'baseline', 'at', 'the', 'is'], 16, 7], ['grid the', 'baseline', 'at the is']), ('partial-repair probe', [['type', 'type', 'grid'], 12, 5], ['type', 'type grid']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'ink', 'of', 'x', 'x', 'serif', 'at', 'kerning', 'type'], 15, 5], ['is ink of x x', 'serif at', 'kerning type']), ('control layout', [['the', 'measure', 'grid', 'on', 'a', 'measure', 'at'], 11, 7], ['the measure', 'grid on a', 'measure at'])], [('regression: runt length threshold', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('regression: runt length threshold', [['grid', 'a', 'the', 'a', 'the', 'kerning', 'a', 'of', 'a', 'at', 'on'], 9, 5], ['grid a', 'the a the', 'kerning a', 'of a', 'at on']), ('partial-repair probe', [['a', 'of', 'is', 'a', 'at', 'on', 'at', 'baseline', 'at', 'on'], 13, 3], ['a of is a at', 'on at', 'baseline', 'at on']), ('partial-repair probe', [['baseline', 'kerning', 'grid', 'baseline', 'on', 'x', 'of', 'grid', 'kerning', 'a', 'of'], 19, 3], ['baseline kerning', 'grid baseline on x', 'of grid kerning', 'a of']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'on', 'serif', 'grid', 'serif', 'grid', 'of', 'measure', 'of', 'of'], 15, 7], ['is on serif', 'grid serif grid', 'of', 'measure of of']), ('control layout', [['ink', 'kerning', 'ink'], 8, 9], ['ink', 'kerning', 'ink'])], [('regression: runt length threshold', [['grid', 'kerning', 'a', 'measure', 'serif', 'type'], 14, 4], ['grid kerning a', 'measure serif', 'type']), ('regression: runt length threshold', [['kerning', 'a', 'a', 'ink', 'x', 'is', 'on', 'type', 'grid', 'of', 'x'], 11, 4], ['kerning a a', 'ink x is on', 'type grid', 'of x']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['a', 'baseline', 'grid', 'on', 'at', 'on', 'kerning', 'a', 'type', 'serif', 'on', 'ink'], 8, 4], ['a', 'baseline', 'grid on', 'at on', 'kerning', 'a type', 'serif', 'on ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['grid', 'serif', 'a', 'serif', 'is'], 11, 5], ['grid serif', 'a serif is']), ('control layout', [['grid', 'a', 'type'], 10, 6], ['grid', 'a type'])], [('regression: runt length threshold', [['a', 'kerning', 'serif', 'is', 'a'], 16, 4], ['a kerning serif', 'is a']), ('regression: runt length threshold', [['is', 'grid', 'measure', 'of', 'type'], 12, 4], ['is grid', 'measure of', 'type']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['x', 'on', 'is', 'the', 'is', 'is', 'ink'], 18, 7], ['x on is the', 'is is ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('control layout', [['of', 'measure', 'on', 'ink'], 9, 8], ['of', 'measure', 'on ink'])]]
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):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last - 1:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: runt length threshold', [['the', 'is', 'grid', 'at', 'at', 'a', 'is'], 18, 7], ['the is grid at', 'at a is']), ('regression: runt length threshold', [['kerning', 'a', 'a', 'ink', 'x', 'is', 'on', 'type', 'grid', 'of', 'x'], 11, 4], ['kerning a a', 'ink x is on', 'type grid', 'of x']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['a', 'of', 'is', 'a', 'at', 'on', 'at', 'baseline', 'at', 'on'], 13, 3], ['a of is a at', 'on at', 'baseline', 'at on']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['measure', 'kerning', 'is', 'a', 'is', 'of', 'at', 'of', 'a', 'of'], 7, 4], ['measure', 'kerning', 'is a is', 'of at', 'of a of']), ('control layout', [['type', 'the', 'baseline', 'a', 'grid', 'x', 'ink', 'of'], 20, 9], ['type the baseline a', 'grid x ink of'])], [('regression: runt length threshold', [['a', 'at', 'is', 'on', 'baseline'], 17, 8], ['a at is on', 'baseline']), ('regression: runt length threshold', [['serif', 'baseline', 'x', 'ink', 'at', 'on', 'on', 'a', 'at'], 16, 4], ['serif baseline x', 'ink at on on', 'a at']), ('partial-repair probe', [['grid', 'the', 'baseline', 'at', 'the', 'is'], 16, 7], ['grid the', 'baseline', 'at the is']), ('partial-repair probe', [['type', 'type', 'grid'], 12, 5], ['type', 'type grid']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'ink', 'of', 'x', 'x', 'serif', 'at', 'kerning', 'type'], 15, 5], ['is ink of x x', 'serif at', 'kerning type']), ('control layout', [['the', 'measure', 'grid', 'on', 'a', 'measure', 'at'], 11, 7], ['the measure', 'grid on a', 'measure at'])], [('regression: runt length threshold', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('regression: runt length threshold', [['grid', 'a', 'the', 'a', 'the', 'kerning', 'a', 'of', 'a', 'at', 'on'], 9, 5], ['grid a', 'the a the', 'kerning a', 'of a', 'at on']), ('partial-repair probe', [['a', 'of', 'is', 'a', 'at', 'on', 'at', 'baseline', 'at', 'on'], 13, 3], ['a of is a at', 'on at', 'baseline', 'at on']), ('partial-repair probe', [['baseline', 'kerning', 'grid', 'baseline', 'on', 'x', 'of', 'grid', 'kerning', 'a', 'of'], 19, 3], ['baseline kerning', 'grid baseline on x', 'of grid kerning', 'a of']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'on', 'serif', 'grid', 'serif', 'grid', 'of', 'measure', 'of', 'of'], 15, 7], ['is on serif', 'grid serif grid', 'of', 'measure of of']), ('control layout', [['ink', 'kerning', 'ink'], 8, 9], ['ink', 'kerning', 'ink'])], [('regression: runt length threshold', [['grid', 'kerning', 'a', 'measure', 'serif', 'type'], 14, 4], ['grid kerning a', 'measure serif', 'type']), ('regression: runt length threshold', [['kerning', 'a', 'a', 'ink', 'x', 'is', 'on', 'type', 'grid', 'of', 'x'], 11, 4], ['kerning a a', 'ink x is on', 'type grid', 'of x']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['a', 'baseline', 'grid', 'on', 'at', 'on', 'kerning', 'a', 'type', 'serif', 'on', 'ink'], 8, 4], ['a', 'baseline', 'grid on', 'at on', 'kerning', 'a type', 'serif', 'on ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['grid', 'serif', 'a', 'serif', 'is'], 11, 5], ['grid serif', 'a serif is']), ('control layout', [['grid', 'a', 'type'], 10, 6], ['grid', 'a type'])], [('regression: runt length threshold', [['a', 'kerning', 'serif', 'is', 'a'], 16, 4], ['a kerning serif', 'is a']), ('regression: runt length threshold', [['is', 'grid', 'measure', 'of', 'type'], 12, 4], ['is grid', 'measure of', 'type']), ('partial-repair probe', [['of', 'measure', 'at', 'x', 'baseline', 'serif', 'the', 'of', 'the'], 13, 7], ['of measure at', 'x baseline', 'serif', 'the of the']), ('partial-repair probe', [['x', 'on', 'is', 'the', 'is', 'is', 'ink'], 18, 7], ['x on is the', 'is is ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('control layout', [['of', 'measure', 'on', 'ink'], 9, 8], ['of', 'measure', 'on ink'])]]
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:
Last lines of exactly the minimum length are still rebalanced.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
        break
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: repeated rebalancing', [['the', 'the', 'is', 'ink', 'serif', 'on', 'on', 'ink'], 14, 8], ['the the is ink', 'serif', 'on on ink']), ('regression: repeated rebalancing', [['the', 'measure', 'a', 'x'], 14, 6], ['the', 'measure a x']), ('regression: repeated rebalancing', [['measure', 'is', 'on', 'grid', 'is', 'measure', 'ink', 'ink', 'serif', 'x', 'on'], 13, 9], ['measure is on', 'grid is', 'measure ink', 'ink', 'serif x on']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['on', 'ink', 'type', 'a'], 12, 4], ['on ink', 'type a']), ('control layout', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif'])], [('regression: repeated rebalancing', [['x', 'on', 'is', 'the', 'is', 'is', 'ink'], 18, 7], ['x on is the', 'is is ink']), ('regression: repeated rebalancing', [['is', 'serif', 'on', 'a', 'type'], 14, 7], ['is serif', 'on a type']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('regression: repeated rebalancing', [['is', 'baseline', 'measure', 'the', 'baseline', 'a', 'the', 'x'], 15, 8], ['is baseline', 'measure the', 'baseline', 'a the x']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['serif', 'measure', 'type'], 11, 5], ['serif', 'measure', 'type']), ('control layout', [['of', 'at', 'x', 'on', 'measure', 'type', 'a', 'measure'], 19, 4], ['of at x on measure', 'type a measure'])], [('regression: repeated rebalancing', [['serif', 'the', 'is', 'is', 'of', 'grid', 'at', 'is', 'a'], 10, 7], ['serif the', 'is is of', 'grid', 'at is a']), ('regression: repeated rebalancing', [['measure', 'is', 'on', 'grid', 'is', 'measure', 'ink', 'ink', 'serif', 'x', 'on'], 13, 9], ['measure is on', 'grid is', 'measure ink', 'ink', 'serif x on']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('regression: repeated rebalancing', [['measure', 'type', 'is', 'serif', 'at', 'the'], 13, 8], ['measure type', 'is', 'serif at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['serif', 'is', 'a', 'x', 'kerning', 'kerning'], 7, 4], ['serif', 'is a x', 'kerning', 'kerning']), ('control layout', [['grid', 'kerning', 'is', 'serif', 'ink'], 17, 7], ['grid kerning is', 'serif ink'])], [('regression: repeated rebalancing', [['the', 'on', 'a', 'a', 'of'], 10, 8], ['the', 'on a a of']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('regression: repeated rebalancing', [['the', 'measure', 'a', 'x'], 14, 6], ['the', 'measure a x']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['on', 'of', 'at', 'ink', 'measure'], 12, 5], ['on of at ink', 'measure']), ('control layout', [['kerning', 'is', 'type', 'of', 'type', 'of', 'baseline', 'a', 'baseline'], 14, 8], ['kerning is', 'type of type', 'of baseline a', 'baseline'])], [('regression: repeated rebalancing', [['the', 'on', 'a', 'a', 'of'], 10, 8], ['the', 'on a a of']), ('regression: repeated rebalancing', [['the', 'the', 'is', 'ink', 'serif', 'on', 'on', 'ink'], 14, 8], ['the the is ink', 'serif', 'on on ink']), ('regression: repeated rebalancing', [['is', 'baseline', 'measure', 'the', 'baseline', 'a', 'the', 'x'], 15, 8], ['is baseline', 'measure the', 'baseline', 'a the x']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['type', 'ink', 'the', 'ink', 'the', 'of', 'kerning', 'a', 'of'], 13, 9], ['type ink the', 'ink the of', 'kerning a of']), ('control layout', [['x', 'baseline', 'grid', 'is', 'baseline', 'on', 'on', 'at', 'on', 'grid', 'grid'], 20, 3], ['x baseline grid is', 'baseline on on at on', 'grid grid'])]]
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):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
        if len(prev) < 4:
            break
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: repeated rebalancing', [['the', 'the', 'is', 'ink', 'serif', 'on', 'on', 'ink'], 14, 8], ['the the is ink', 'serif', 'on on ink']), ('regression: repeated rebalancing', [['the', 'measure', 'a', 'x'], 14, 6], ['the', 'measure a x']), ('regression: repeated rebalancing', [['measure', 'is', 'on', 'grid', 'is', 'measure', 'ink', 'ink', 'serif', 'x', 'on'], 13, 9], ['measure is on', 'grid is', 'measure ink', 'ink', 'serif x on']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['on', 'ink', 'type', 'a'], 12, 4], ['on ink', 'type a']), ('control layout', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif'])], [('regression: repeated rebalancing', [['x', 'on', 'is', 'the', 'is', 'is', 'ink'], 18, 7], ['x on is the', 'is is ink']), ('regression: repeated rebalancing', [['is', 'serif', 'on', 'a', 'type'], 14, 7], ['is serif', 'on a type']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('regression: repeated rebalancing', [['is', 'baseline', 'measure', 'the', 'baseline', 'a', 'the', 'x'], 15, 8], ['is baseline', 'measure the', 'baseline', 'a the x']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['serif', 'measure', 'type'], 11, 5], ['serif', 'measure', 'type']), ('control layout', [['of', 'at', 'x', 'on', 'measure', 'type', 'a', 'measure'], 19, 4], ['of at x on measure', 'type a measure'])], [('regression: repeated rebalancing', [['serif', 'the', 'is', 'is', 'of', 'grid', 'at', 'is', 'a'], 10, 7], ['serif the', 'is is of', 'grid', 'at is a']), ('regression: repeated rebalancing', [['measure', 'is', 'on', 'grid', 'is', 'measure', 'ink', 'ink', 'serif', 'x', 'on'], 13, 9], ['measure is on', 'grid is', 'measure ink', 'ink', 'serif x on']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('regression: repeated rebalancing', [['measure', 'type', 'is', 'serif', 'at', 'the'], 13, 8], ['measure type', 'is', 'serif at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['serif', 'is', 'a', 'x', 'kerning', 'kerning'], 7, 4], ['serif', 'is a x', 'kerning', 'kerning']), ('control layout', [['grid', 'kerning', 'is', 'serif', 'ink'], 17, 7], ['grid kerning is', 'serif ink'])], [('regression: repeated rebalancing', [['the', 'on', 'a', 'a', 'of'], 10, 8], ['the', 'on a a of']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('regression: repeated rebalancing', [['the', 'measure', 'a', 'x'], 14, 6], ['the', 'measure a x']), ('regression: repeated rebalancing', [['ink', 'the', 'of', 'of', 'of', 'on', 'at'], 10, 9], ['ink the of', 'of', 'of on at']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['on', 'of', 'at', 'ink', 'measure'], 12, 5], ['on of at ink', 'measure']), ('control layout', [['kerning', 'is', 'type', 'of', 'type', 'of', 'baseline', 'a', 'baseline'], 14, 8], ['kerning is', 'type of type', 'of baseline a', 'baseline'])], [('regression: repeated rebalancing', [['the', 'on', 'a', 'a', 'of'], 10, 8], ['the', 'on a a of']), ('regression: repeated rebalancing', [['the', 'the', 'is', 'ink', 'serif', 'on', 'on', 'ink'], 14, 8], ['the the is ink', 'serif', 'on on ink']), ('regression: repeated rebalancing', [['is', 'baseline', 'measure', 'the', 'baseline', 'a', 'the', 'x'], 15, 8], ['is baseline', 'measure the', 'baseline', 'a the x']), ('regression: repeated rebalancing', [['grid', 'the', 'the', 'kerning', 'kerning', 'x', 'a'], 17, 7], ['grid the the', 'kerning', 'kerning x a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['type', 'ink', 'the', 'ink', 'the', 'of', 'kerning', 'a', 'of'], 13, 9], ['type ink the', 'ink the of', 'kerning a of']), ('control layout', [['x', 'baseline', 'grid', 'is', 'baseline', 'on', 'on', 'at', 'on', 'grid', 'grid'], 20, 3], ['x baseline grid is', 'baseline on on at on', 'grid grid'])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A runt is only partly fixed when one moved word is not enough.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(lines[-1]) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: moved line fit', [['serif', 'measure', 'measure', 'a', 'baseline', 'measure', 'at', 'at', 'serif', 'serif'], 10, 8], ['serif', 'measure', 'measure a', 'baseline', 'measure at', 'at serif', 'serif']), ('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('partial-repair probe', [['type', 'the', 'kerning', 'the', 'the', 'kerning', 'the'], 11, 8], ['type the', 'kerning the', 'the', 'kerning the']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['kerning', 'on', 'baseline', 'ink', 'of', 'on', 'serif', 'on', 'type', 'ink', 'at'], 16, 4], ['kerning on', 'baseline ink of', 'on serif on type', 'ink at']), ('control layout', [['is', 'of', 'the', 'of', 'is', 'of'], 18, 8], ['is of the of is of'])], [('regression: moved line fit', [['serif', 'the', 'baseline', 'serif', 'serif', 'a', 'on', 'ink', 'x', 'ink', 'the', 'is'], 7, 8], ['serif', 'the', 'baseline', 'serif', 'serif a', 'on ink', 'x ink', 'the is']), ('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('partial-repair probe', [['grid', 'is', 'x', 'baseline', 'the', 'serif', 'a', 'serif', 'of', 'kerning', 'at', 'the'], 14, 9], ['grid is x', 'baseline the', 'serif a serif', 'of', 'kerning at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('control layout', [['kerning', 'type', 'baseline', 'on', 'type', 'a', 'is'], 16, 7], ['kerning type', 'baseline on', 'type a is'])], [('regression: moved line fit', [['ink', 'the', 'a', 'ink', 'measure'], 8, 9], ['ink the', 'a ink', 'measure']), ('regression: moved line fit', [['grid', 'type', 'measure', 'grid', 'x', 'baseline'], 8, 9], ['grid', 'type', 'measure', 'grid x', 'baseline']), ('partial-repair probe', [['x', 'at', 'kerning', 'baseline'], 16, 9], ['x at', 'kerning baseline']), ('partial-repair probe', [['ink', 'at', 'baseline', 'measure'], 16, 9], ['ink at', 'baseline measure']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'measure', 'grid', 'grid', 'ink', 'kerning', 'measure', 'of'], 16, 8], ['the measure grid', 'grid ink kerning', 'measure of']), ('control layout', [['on', 'ink', 'at', 'on', 'at', 'ink', 'the'], 16, 3], ['on ink at on at', 'ink the'])], [('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('regression: moved line fit', [['serif', 'the', 'baseline', 'serif', 'serif', 'a', 'on', 'ink', 'x', 'ink', 'the', 'is'], 7, 8], ['serif', 'the', 'baseline', 'serif', 'serif a', 'on ink', 'x ink', 'the is']), ('partial-repair probe', [['x', 'at', 'kerning', 'baseline'], 16, 9], ['x at', 'kerning baseline']), ('partial-repair probe', [['on', 'kerning', 'type', 'ink', 'kerning', 'of', 'serif', 'the'], 9, 8], ['on', 'kerning', 'type ink', 'kerning', 'of', 'serif the']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['at', 'kerning', 'ink', 'kerning', 'serif', 'a'], 15, 5], ['at kerning ink', 'kerning serif a']), ('control layout', [['baseline', 'of', 'on', 'on', 'ink', 'measure', 'ink', 'x'], 16, 7], ['baseline of on', 'on ink', 'measure ink x'])], [('regression: moved line fit', [['grid', 'type', 'measure', 'grid', 'x', 'baseline'], 8, 9], ['grid', 'type', 'measure', 'grid x', 'baseline']), ('regression: moved line fit', [['at', 'on', 'serif'], 7, 7], ['at on', 'serif']), ('partial-repair probe', [['on', 'kerning', 'type', 'ink', 'kerning', 'of', 'serif', 'the'], 9, 8], ['on', 'kerning', 'type ink', 'kerning', 'of', 'serif the']), ('partial-repair probe', [['grid', 'is', 'x', 'baseline', 'the', 'serif', 'a', 'serif', 'of', 'kerning', 'at', 'the'], 14, 9], ['grid is x', 'baseline the', 'serif a serif', 'of', 'kerning at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'kerning', 'on', 'baseline'], 19, 8], ['the kerning on', 'baseline']), ('control layout', [['the', 'x', 'is', 'of', 'serif'], 19, 8], ['the x is of serif'])]]
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):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) >= width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: moved line fit', [['serif', 'measure', 'measure', 'a', 'baseline', 'measure', 'at', 'at', 'serif', 'serif'], 10, 8], ['serif', 'measure', 'measure a', 'baseline', 'measure at', 'at serif', 'serif']), ('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('partial-repair probe', [['type', 'the', 'kerning', 'the', 'the', 'kerning', 'the'], 11, 8], ['type the', 'kerning the', 'the', 'kerning the']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['kerning', 'on', 'baseline', 'ink', 'of', 'on', 'serif', 'on', 'type', 'ink', 'at'], 16, 4], ['kerning on', 'baseline ink of', 'on serif on type', 'ink at']), ('control layout', [['is', 'of', 'the', 'of', 'is', 'of'], 18, 8], ['is of the of is of'])], [('regression: moved line fit', [['serif', 'the', 'baseline', 'serif', 'serif', 'a', 'on', 'ink', 'x', 'ink', 'the', 'is'], 7, 8], ['serif', 'the', 'baseline', 'serif', 'serif a', 'on ink', 'x ink', 'the is']), ('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('partial-repair probe', [['grid', 'is', 'x', 'baseline', 'the', 'serif', 'a', 'serif', 'of', 'kerning', 'at', 'the'], 14, 9], ['grid is x', 'baseline the', 'serif a serif', 'of', 'kerning at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('control layout', [['kerning', 'type', 'baseline', 'on', 'type', 'a', 'is'], 16, 7], ['kerning type', 'baseline on', 'type a is'])], [('regression: moved line fit', [['ink', 'the', 'a', 'ink', 'measure'], 8, 9], ['ink the', 'a ink', 'measure']), ('regression: moved line fit', [['grid', 'type', 'measure', 'grid', 'x', 'baseline'], 8, 9], ['grid', 'type', 'measure', 'grid x', 'baseline']), ('partial-repair probe', [['x', 'at', 'kerning', 'baseline'], 16, 9], ['x at', 'kerning baseline']), ('partial-repair probe', [['ink', 'at', 'baseline', 'measure'], 16, 9], ['ink at', 'baseline measure']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'measure', 'grid', 'grid', 'ink', 'kerning', 'measure', 'of'], 16, 8], ['the measure grid', 'grid ink kerning', 'measure of']), ('control layout', [['on', 'ink', 'at', 'on', 'at', 'ink', 'the'], 16, 3], ['on ink at on at', 'ink the'])], [('regression: moved line fit', [['kerning', 'a', 'a', 'type', 'x', 'x', 'is', 'grid', 'on'], 8, 8], ['kerning', 'a a type', 'x x is', 'grid on']), ('regression: moved line fit', [['serif', 'the', 'baseline', 'serif', 'serif', 'a', 'on', 'ink', 'x', 'ink', 'the', 'is'], 7, 8], ['serif', 'the', 'baseline', 'serif', 'serif a', 'on ink', 'x ink', 'the is']), ('partial-repair probe', [['x', 'at', 'kerning', 'baseline'], 16, 9], ['x at', 'kerning baseline']), ('partial-repair probe', [['on', 'kerning', 'type', 'ink', 'kerning', 'of', 'serif', 'the'], 9, 8], ['on', 'kerning', 'type ink', 'kerning', 'of', 'serif the']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['at', 'kerning', 'ink', 'kerning', 'serif', 'a'], 15, 5], ['at kerning ink', 'kerning serif a']), ('control layout', [['baseline', 'of', 'on', 'on', 'ink', 'measure', 'ink', 'x'], 16, 7], ['baseline of on', 'on ink', 'measure ink x'])], [('regression: moved line fit', [['grid', 'type', 'measure', 'grid', 'x', 'baseline'], 8, 9], ['grid', 'type', 'measure', 'grid x', 'baseline']), ('regression: moved line fit', [['at', 'on', 'serif'], 7, 7], ['at on', 'serif']), ('partial-repair probe', [['on', 'kerning', 'type', 'ink', 'kerning', 'of', 'serif', 'the'], 9, 8], ['on', 'kerning', 'type ink', 'kerning', 'of', 'serif the']), ('partial-repair probe', [['grid', 'is', 'x', 'baseline', 'the', 'serif', 'a', 'serif', 'of', 'kerning', 'at', 'the'], 14, 9], ['grid is x', 'baseline the', 'serif a serif', 'of', 'kerning at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'kerning', 'on', 'baseline'], 19, 8], ['the kerning on', 'baseline']), ('control layout', [['the', 'x', 'is', 'of', 'serif'], 19, 8], ['the x is of serif'])]]
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:
Rebalancing produces an overfull last line.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1].split(' ')) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: runt measure unit', [['grid', 'the', 'grid', 'baseline', 'serif'], 20, 8], ['grid the grid', 'baseline serif']), ('regression: runt measure unit', [['type', 'the', 'baseline', 'a', 'grid', 'x', 'ink', 'of'], 20, 9], ['type the baseline a', 'grid x ink of']), ('regression: runt measure unit', [['kerning', 'grid', 'of', 'is', 'serif', 'the', 'serif', 'ink', 'serif', 'type', 'a', 'the'], 17, 9], ['kerning grid of', 'is serif the', 'serif ink serif', 'type a the']), ('regression: runt measure unit', [['baseline', 'type', 'is', 'of', 'serif', 'x', 'grid', 'on', 'x'], 14, 8], ['baseline type', 'is of serif x', 'grid on x']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['ink', 'on', 'kerning', 'ink', 'is', 'measure'], 14, 7], ['ink on kerning', 'ink is measure']), ('control layout', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif'])], [('regression: runt measure unit', [['baseline', 'x', 'at', 'the', 'measure', 'serif', 'ink', 'grid', 'a', 'the', 'on', 'kerning'], 13, 3], ['baseline x at', 'the measure', 'serif ink', 'grid a the on', 'kerning']), ('regression: runt measure unit', [['x', 'x', 'measure', 'baseline', 'of', 'on', 'on', 'serif', 'serif', 'type', 'type'], 15, 9], ['x x measure', 'baseline of on', 'on serif serif', 'type type']), ('regression: runt measure unit', [['serif', 'baseline', 'x', 'ink', 'at', 'on', 'on', 'a', 'at'], 16, 4], ['serif baseline x', 'ink at on on', 'a at']), ('regression: runt measure unit', [['kerning', 'at', 'at', 'is', 'is'], 15, 8], ['kerning at', 'at is is']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['serif', 'of', 'ink', 'grid', 'the', 'kerning', 'is', 'type', 'x', 'is'], 5, 9], ['serif', 'of', 'ink', 'grid', 'the', 'kerning', 'is', 'type', 'x is']), ('control layout', [['x', 'a', 'baseline', 'is', 'measure'], 10, 8], ['x a', 'baseline', 'is measure'])], [('regression: runt measure unit', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('regression: runt measure unit', [['measure', 'x', 'x', 'the', 'a', 'the', 'at', 'baseline', 'ink', 'is', 'grid', 'measure'], 20, 9], ['measure x x the a', 'the at baseline ink', 'is grid measure']), ('regression: runt measure unit', [['at', 'measure', 'grid', 'serif', 'on', 'is', 'measure', 'at', 'at', 'at', 'the'], 15, 9], ['at measure grid', 'serif on is', 'measure at', 'at at the']), ('regression: runt measure unit', [['the', 'a', 'ink', 'on', 'at'], 11, 5], ['the a ink', 'on at']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['on', 'is', 'type', 'grid', 'ink', 'at', 'the', 'is', 'type'], 6, 6], ['on is', 'type', 'grid', 'ink at', 'the is', 'type']), ('control layout', [['a', 'the', 'of', 'measure', 'at', 'type', 'kerning', 'the', 'ink'], 15, 7], ['a the of', 'measure at type', 'kerning the ink'])], [('regression: runt measure unit', [['at', 'measure', 'kerning', 'of', 'is', 'type'], 17, 3], ['at measure', 'kerning of is', 'type']), ('regression: runt measure unit', [['at', 'of', 'measure', 'x', 'of', 'on', 'grid', 'of', 'x', 'is', 'serif'], 10, 8], ['at of', 'measure x', 'of on grid', 'of x', 'is serif']), ('regression: runt measure unit', [['the', 'is', 'grid', 'at', 'at', 'a', 'is'], 18, 7], ['the is grid at', 'at a is']), ('regression: runt measure unit', [['a', 'kerning', 'serif', 'is', 'a'], 16, 4], ['a kerning serif', 'is a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['baseline', 'serif', 'is', 'baseline', 'x', 'x', 'at', 'is', 'serif', 'at', 'at'], 6, 9], ['baseline', 'serif', 'is', 'baseline', 'x x at', 'is', 'serif', 'at at']), ('control layout', [['of', 'the', 'kerning'], 6, 4], ['of the', 'kerning'])], [('regression: runt measure unit', [['grid', 'kerning', 'ink', 'baseline', 'at', 'type', 'grid', 'of', 'baseline', 'type', 'ink'], 18, 7], ['grid kerning ink', 'baseline at type', 'grid of baseline', 'type ink']), ('regression: runt measure unit', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('regression: runt measure unit', [['a', 'serif', 'grid', 'ink', 'measure', 'the', 'type', 'type', 'at', 'a', 'of'], 13, 7], ['a serif grid', 'ink measure', 'the type type', 'at a of']), ('regression: runt measure unit', [['at', 'measure', 'grid', 'serif', 'on', 'is', 'measure', 'at', 'at', 'at', 'the'], 15, 9], ['at measure grid', 'serif on is', 'measure at', 'at at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'on', 'is', 'at', 'baseline', 'the', 'a', 'at', 'ink', 'type'], 9, 5], ['the on is', 'at', 'baseline', 'the a at', 'ink type']), ('control layout', [['on', 'serif', 'on'], 7, 6], ['on', 'serif', 'on'])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1].replace(' ', '')) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: runt measure unit', [['grid', 'the', 'grid', 'baseline', 'serif'], 20, 8], ['grid the grid', 'baseline serif']), ('regression: runt measure unit', [['type', 'the', 'baseline', 'a', 'grid', 'x', 'ink', 'of'], 20, 9], ['type the baseline a', 'grid x ink of']), ('regression: runt measure unit', [['kerning', 'grid', 'of', 'is', 'serif', 'the', 'serif', 'ink', 'serif', 'type', 'a', 'the'], 17, 9], ['kerning grid of', 'is serif the', 'serif ink serif', 'type a the']), ('regression: runt measure unit', [['baseline', 'type', 'is', 'of', 'serif', 'x', 'grid', 'on', 'x'], 14, 8], ['baseline type', 'is of serif x', 'grid on x']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['ink', 'on', 'kerning', 'ink', 'is', 'measure'], 14, 7], ['ink on kerning', 'ink is measure']), ('control layout', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif'])], [('regression: runt measure unit', [['baseline', 'x', 'at', 'the', 'measure', 'serif', 'ink', 'grid', 'a', 'the', 'on', 'kerning'], 13, 3], ['baseline x at', 'the measure', 'serif ink', 'grid a the on', 'kerning']), ('regression: runt measure unit', [['x', 'x', 'measure', 'baseline', 'of', 'on', 'on', 'serif', 'serif', 'type', 'type'], 15, 9], ['x x measure', 'baseline of on', 'on serif serif', 'type type']), ('regression: runt measure unit', [['serif', 'baseline', 'x', 'ink', 'at', 'on', 'on', 'a', 'at'], 16, 4], ['serif baseline x', 'ink at on on', 'a at']), ('regression: runt measure unit', [['kerning', 'at', 'at', 'is', 'is'], 15, 8], ['kerning at', 'at is is']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['serif', 'of', 'ink', 'grid', 'the', 'kerning', 'is', 'type', 'x', 'is'], 5, 9], ['serif', 'of', 'ink', 'grid', 'the', 'kerning', 'is', 'type', 'x is']), ('control layout', [['x', 'a', 'baseline', 'is', 'measure'], 10, 8], ['x a', 'baseline', 'is measure'])], [('regression: runt measure unit', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('regression: runt measure unit', [['measure', 'x', 'x', 'the', 'a', 'the', 'at', 'baseline', 'ink', 'is', 'grid', 'measure'], 20, 9], ['measure x x the a', 'the at baseline ink', 'is grid measure']), ('regression: runt measure unit', [['at', 'measure', 'grid', 'serif', 'on', 'is', 'measure', 'at', 'at', 'at', 'the'], 15, 9], ['at measure grid', 'serif on is', 'measure at', 'at at the']), ('regression: runt measure unit', [['the', 'a', 'ink', 'on', 'at'], 11, 5], ['the a ink', 'on at']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['on', 'is', 'type', 'grid', 'ink', 'at', 'the', 'is', 'type'], 6, 6], ['on is', 'type', 'grid', 'ink at', 'the is', 'type']), ('control layout', [['a', 'the', 'of', 'measure', 'at', 'type', 'kerning', 'the', 'ink'], 15, 7], ['a the of', 'measure at type', 'kerning the ink'])], [('regression: runt measure unit', [['at', 'measure', 'kerning', 'of', 'is', 'type'], 17, 3], ['at measure', 'kerning of is', 'type']), ('regression: runt measure unit', [['at', 'of', 'measure', 'x', 'of', 'on', 'grid', 'of', 'x', 'is', 'serif'], 10, 8], ['at of', 'measure x', 'of on grid', 'of x', 'is serif']), ('regression: runt measure unit', [['the', 'is', 'grid', 'at', 'at', 'a', 'is'], 18, 7], ['the is grid at', 'at a is']), ('regression: runt measure unit', [['a', 'kerning', 'serif', 'is', 'a'], 16, 4], ['a kerning serif', 'is a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['baseline', 'serif', 'is', 'baseline', 'x', 'x', 'at', 'is', 'serif', 'at', 'at'], 6, 9], ['baseline', 'serif', 'is', 'baseline', 'x x at', 'is', 'serif', 'at at']), ('control layout', [['of', 'the', 'kerning'], 6, 4], ['of the', 'kerning'])], [('regression: runt measure unit', [['grid', 'kerning', 'ink', 'baseline', 'at', 'type', 'grid', 'of', 'baseline', 'type', 'ink'], 18, 7], ['grid kerning ink', 'baseline at type', 'grid of baseline', 'type ink']), ('regression: runt measure unit', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('regression: runt measure unit', [['a', 'serif', 'grid', 'ink', 'measure', 'the', 'type', 'type', 'at', 'a', 'of'], 13, 7], ['a serif grid', 'ink measure', 'the type type', 'at a of']), ('regression: runt measure unit', [['at', 'measure', 'grid', 'serif', 'on', 'is', 'measure', 'at', 'at', 'at', 'the'], 15, 9], ['at measure grid', 'serif on is', 'measure at', 'at at the']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['the', 'on', 'is', 'at', 'baseline', 'the', 'a', 'at', 'ink', 'type'], 9, 5], ['the on is', 'at', 'baseline', 'the a at', 'ink type']), ('control layout', [['on', 'serif', 'on'], 7, 6], ['on', 'serif', 'on'])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Short last lines with several short words are left as runts.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) < width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('regression: greedy fill boundary', [['a', 'the', 'of', 'measure', 'at', 'type', 'kerning', 'the', 'ink'], 15, 7], ['a the of', 'measure at type', 'kerning the ink']), ('regression: greedy fill boundary', [['baseline', 'type', 'is', 'serif', 'on', 'at', 'baseline'], 7, 3], ['baseline', 'type is', 'serif', 'on at', 'baseline']), ('partial-repair probe', [['a', 'is', 'grid', 'at', 'the', 'kerning', 'kerning', 'type', 'measure', 'ink', 'on'], 18, 5], ['a is grid at the', 'kerning kerning', 'type measure', 'ink on']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['a', 'x', 'is'], 15, 7], ['a x is']), ('control layout', [['grid', 'serif', 'at', 'measure'], 15, 3], ['grid serif at', 'measure']), ('control layout', [['serif', 'the', 'is', 'is', 'of', 'grid', 'at', 'is', 'a'], 10, 7], ['serif the', 'is is of', 'grid', 'at is a'])], [('regression: greedy fill boundary', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('regression: greedy fill boundary', [['baseline', 'is', 'grid', 'x', 'x', 'the', 'a', 'is', 'grid', 'at', 'kerning'], 18, 8], ['baseline is grid x', 'x the a is grid', 'at kerning']), ('regression: greedy fill boundary', [['ink', 'the', 'is', 'ink', 'baseline', 'is', 'grid', 'grid', 'x'], 6, 5], ['ink', 'the is', 'ink', 'baseline', 'is', 'grid', 'grid x']), ('regression: greedy fill boundary', [['baseline', 'x', 'at', 'the', 'measure', 'serif', 'ink', 'grid', 'a', 'the', 'on', 'kerning'], 13, 3], ['baseline x at', 'the measure', 'serif ink', 'grid a the on', 'kerning']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['x', 'measure', 'ink', 'measure', 'is'], 14, 4], ['x measure ink', 'measure is']), ('control layout', [['the', 'measure', 'ink', 'the', 'type', 'x', 'is', 'x'], 9, 3], ['the', 'measure', 'ink the', 'type x', 'is x'])], [('regression: greedy fill boundary', [['measure', 'of', 'baseline', 'a', 'on', 'at', 'kerning', 'measure', 'serif', 'grid', 'baseline', 'is'], 13, 8], ['measure of', 'baseline a on', 'at kerning', 'measure serif', 'grid', 'baseline is']), ('regression: greedy fill boundary', [['a', 'baseline', 'the', 'a', 'type', 'kerning', 'is', 'serif', 'a', 'grid', 'the'], 6, 3], ['a', 'baseline', 'the a', 'type', 'kerning', 'is', 'serif', 'a grid', 'the']), ('regression: greedy fill boundary', [['grid', 'measure', 'kerning', 'kerning', 'is', 'the', 'grid', 'is', 'measure', 'is', 'serif'], 14, 5], ['grid measure', 'kerning', 'kerning is the', 'grid is', 'measure is', 'serif']), ('regression: greedy fill boundary', [['baseline', 'of', 'of', 'type', 'serif', 'at', 'measure', 'serif', 'x', 'type', 'ink'], 11, 3], ['baseline of', 'of type', 'serif at', 'measure', 'serif x', 'type ink']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'of', 'of', 'is', 'type', 'kerning', 'the'], 9, 5], ['is of of', 'is type', 'kerning', 'the']), ('control layout', [['serif', 'grid', 'at', 'ink'], 19, 3], ['serif grid at ink'])], [('regression: greedy fill boundary', [['ink', 'on', 'kerning', 'ink', 'is', 'measure'], 14, 7], ['ink on kerning', 'ink is measure']), ('regression: greedy fill boundary', [['grid', 'ink', 'is', 'a', 'x', 'on', 'serif', 'serif', 'on', 'x', 'x', 'a'], 20, 5], ['grid ink is a x on', 'serif serif on x x a']), ('partial-repair probe', [['kerning', 'on', 'kerning'], 17, 7], ['kerning on', 'kerning']), ('partial-repair probe', [['of', 'on', 'ink', 'kerning', 'serif', 'x', 'grid', 'baseline'], 20, 8], ['of on ink kerning', 'serif x grid', 'baseline']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['kerning', 'type', 'baseline', 'on', 'type', 'a', 'is'], 16, 7], ['kerning type', 'baseline on', 'type a is']), ('control layout', [['serif', 'serif', 'grid', 'measure'], 7, 7], ['serif', 'serif', 'grid', 'measure'])], [('regression: greedy fill boundary', [['grid', 'grid', 'measure', 'on', 'serif', 'serif', 'grid', 'the', 'grid', 'is', 'type'], 12, 8], ['grid grid', 'measure on', 'serif serif', 'grid the', 'grid is type']), ('regression: greedy fill boundary', [['type', 'baseline', 'x', 'grid', 'type', 'grid'], 13, 7], ['type baseline', 'x grid', 'type grid']), ('partial-repair probe', [['grid', 'the', 'ink', 'measure', 'x', 'serif', 'kerning', 'type', 'measure', 'measure', 'on'], 19, 9], ['grid the ink', 'measure x serif', 'kerning type', 'measure measure on']), ('partial-repair probe', [['serif', 'of', 'of', 'serif', 'kerning', 'is', 'grid'], 6, 9], ['serif', 'of of', 'serif', 'kerning', 'is', 'grid']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['the', 'ink', 'kerning', 'of', 'at'], 8, 6], ['the ink', 'kerning', 'of at']), ('control layout', [['baseline', 'x', 'grid'], 7, 3], ['baseline', 'x grid'])]]
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):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width + 1 or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('regression: greedy fill boundary', [['a', 'the', 'of', 'measure', 'at', 'type', 'kerning', 'the', 'ink'], 15, 7], ['a the of', 'measure at type', 'kerning the ink']), ('regression: greedy fill boundary', [['baseline', 'type', 'is', 'serif', 'on', 'at', 'baseline'], 7, 3], ['baseline', 'type is', 'serif', 'on at', 'baseline']), ('partial-repair probe', [['a', 'is', 'grid', 'at', 'the', 'kerning', 'kerning', 'type', 'measure', 'ink', 'on'], 18, 5], ['a is grid at the', 'kerning kerning', 'type measure', 'ink on']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['a', 'x', 'is'], 15, 7], ['a x is']), ('control layout', [['grid', 'serif', 'at', 'measure'], 15, 3], ['grid serif at', 'measure']), ('control layout', [['serif', 'the', 'is', 'is', 'of', 'grid', 'at', 'is', 'a'], 10, 7], ['serif the', 'is is of', 'grid', 'at is a'])], [('regression: greedy fill boundary', [['is', 'the', 'serif', 'grid', 'serif', 'of', 'serif'], 13, 3], ['is the serif', 'grid serif of', 'serif']), ('regression: greedy fill boundary', [['baseline', 'is', 'grid', 'x', 'x', 'the', 'a', 'is', 'grid', 'at', 'kerning'], 18, 8], ['baseline is grid x', 'x the a is grid', 'at kerning']), ('regression: greedy fill boundary', [['ink', 'the', 'is', 'ink', 'baseline', 'is', 'grid', 'grid', 'x'], 6, 5], ['ink', 'the is', 'ink', 'baseline', 'is', 'grid', 'grid x']), ('regression: greedy fill boundary', [['baseline', 'x', 'at', 'the', 'measure', 'serif', 'ink', 'grid', 'a', 'the', 'on', 'kerning'], 13, 3], ['baseline x at', 'the measure', 'serif ink', 'grid a the on', 'kerning']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['x', 'measure', 'ink', 'measure', 'is'], 14, 4], ['x measure ink', 'measure is']), ('control layout', [['the', 'measure', 'ink', 'the', 'type', 'x', 'is', 'x'], 9, 3], ['the', 'measure', 'ink the', 'type x', 'is x'])], [('regression: greedy fill boundary', [['measure', 'of', 'baseline', 'a', 'on', 'at', 'kerning', 'measure', 'serif', 'grid', 'baseline', 'is'], 13, 8], ['measure of', 'baseline a on', 'at kerning', 'measure serif', 'grid', 'baseline is']), ('regression: greedy fill boundary', [['a', 'baseline', 'the', 'a', 'type', 'kerning', 'is', 'serif', 'a', 'grid', 'the'], 6, 3], ['a', 'baseline', 'the a', 'type', 'kerning', 'is', 'serif', 'a grid', 'the']), ('regression: greedy fill boundary', [['grid', 'measure', 'kerning', 'kerning', 'is', 'the', 'grid', 'is', 'measure', 'is', 'serif'], 14, 5], ['grid measure', 'kerning', 'kerning is the', 'grid is', 'measure is', 'serif']), ('regression: greedy fill boundary', [['baseline', 'of', 'of', 'type', 'serif', 'at', 'measure', 'serif', 'x', 'type', 'ink'], 11, 3], ['baseline of', 'of type', 'serif at', 'measure', 'serif x', 'type ink']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'of', 'of', 'is', 'type', 'kerning', 'the'], 9, 5], ['is of of', 'is type', 'kerning', 'the']), ('control layout', [['serif', 'grid', 'at', 'ink'], 19, 3], ['serif grid at ink'])], [('regression: greedy fill boundary', [['ink', 'on', 'kerning', 'ink', 'is', 'measure'], 14, 7], ['ink on kerning', 'ink is measure']), ('regression: greedy fill boundary', [['grid', 'ink', 'is', 'a', 'x', 'on', 'serif', 'serif', 'on', 'x', 'x', 'a'], 20, 5], ['grid ink is a x on', 'serif serif on x x a']), ('partial-repair probe', [['kerning', 'on', 'kerning'], 17, 7], ['kerning on', 'kerning']), ('partial-repair probe', [['of', 'on', 'ink', 'kerning', 'serif', 'x', 'grid', 'baseline'], 20, 8], ['of on ink kerning', 'serif x grid', 'baseline']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['kerning', 'type', 'baseline', 'on', 'type', 'a', 'is'], 16, 7], ['kerning type', 'baseline on', 'type a is']), ('control layout', [['serif', 'serif', 'grid', 'measure'], 7, 7], ['serif', 'serif', 'grid', 'measure'])], [('regression: greedy fill boundary', [['grid', 'grid', 'measure', 'on', 'serif', 'serif', 'grid', 'the', 'grid', 'is', 'type'], 12, 8], ['grid grid', 'measure on', 'serif serif', 'grid the', 'grid is type']), ('regression: greedy fill boundary', [['type', 'baseline', 'x', 'grid', 'type', 'grid'], 13, 7], ['type baseline', 'x grid', 'type grid']), ('partial-repair probe', [['grid', 'the', 'ink', 'measure', 'x', 'serif', 'kerning', 'type', 'measure', 'measure', 'on'], 19, 9], ['grid the ink', 'measure x serif', 'kerning type', 'measure measure on']), ('partial-repair probe', [['serif', 'of', 'of', 'serif', 'kerning', 'is', 'grid'], 6, 9], ['serif', 'of of', 'serif', 'kerning', 'is', 'grid']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['the', 'ink', 'kerning', 'of', 'at'], 8, 6], ['the ink', 'kerning', 'of at']), ('control layout', [['baseline', 'x', 'grid'], 7, 3], ['baseline', 'x grid'])]]
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:
Words that exactly fill the measure are wrapped.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or len(w) > width:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: overwide word isolation', [['on', 'ink', 'x', 'at', 'a', 'kerning', 'serif', 'measure', 'at', 'baseline'], 7, 3], ['on ink', 'x at a', 'kerning', 'serif', 'measure', 'at', 'baseline']), ('regression: overwide word isolation', [['on', 'of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure'], 5, 4], ['on of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure']), ('partial-repair probe', [['a', 'ink', 'measure', 'serif', 'baseline'], 14, 5], ['a ink measure', 'serif baseline']), ('partial-repair probe', [['baseline', 'on', 'grid', 'kerning', 'a', 'ink', 'baseline', 'measure', 'a'], 10, 3], ['baseline', 'on grid', 'kerning a', 'ink', 'baseline', 'measure a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['a', 'x', 'grid'], 13, 9], ['a x grid']), ('control layout', [['serif', 'a', 'the'], 17, 4], ['serif a the'])], [('regression: overwide word isolation', [['serif', 'baseline', 'grid', 'of', 'is', 'kerning'], 5, 8], ['serif', 'baseline', 'grid', 'of is', 'kerning']), ('regression: overwide word isolation', [['a', 'baseline', 'the', 'a', 'type', 'kerning', 'is', 'serif', 'a', 'grid', 'the'], 6, 3], ['a', 'baseline', 'the a', 'type', 'kerning', 'is', 'serif', 'a grid', 'the']), ('partial-repair probe', [['on', 'ink', 'type', 'a'], 12, 4], ['on ink', 'type a']), ('regression: overwide word isolation', [['baseline', 'a', 'a', 'is', 'a', 'type', 'on', 'baseline', 'serif', 'baseline', 'serif'], 7, 8], ['baseline', 'a a is', 'a type', 'on', 'baseline', 'serif', 'baseline', 'serif']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['on', 'is', 'at'], 17, 3], ['on is at']), ('control layout', [['x', 'on', 'serif'], 18, 4], ['x on serif'])], [('regression: overwide word isolation', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif']), ('regression: overwide word isolation', [['on', 'at', 'grid', 'measure', 'type', 'measure', 'on', 'x', 'at', 'a', 'baseline'], 7, 5], ['on at', 'grid', 'measure', 'type', 'measure', 'on x at', 'a', 'baseline']), ('partial-repair probe', [['grid', 'serif', 'at', 'measure'], 15, 3], ['grid serif at', 'measure']), ('partial-repair probe', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'measure', 'at'], 19, 4], ['is measure at']), ('control layout', [['measure', 'on', 'x', 'ink'], 16, 7], ['measure on x ink'])], [('regression: overwide word isolation', [['the', 'serif', 'baseline', 'x', 'a', 'baseline', 'measure', 'ink', 'of', 'measure', 'the'], 6, 8], ['the', 'serif', 'baseline', 'x a', 'baseline', 'measure', 'ink of', 'measure', 'the']), ('regression: overwide word isolation', [['on', 'of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure'], 5, 4], ['on of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure']), ('partial-repair probe', [['at', 'a', 'on', 'grid', 'grid', 'measure', 'the', 'x', 'at', 'at', 'type'], 20, 8], ['at a on grid grid', 'measure the x', 'at at type']), ('regression: overwide word isolation', [['serif', 'of', 'at', 'the', 'measure', 'ink', 'ink', 'a', 'x'], 5, 8], ['serif', 'of at', 'the', 'measure', 'ink', 'ink', 'a x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['x', 'on', 'serif'], 18, 4], ['x on serif']), ('control layout', [['is', 'the', 'serif'], 12, 9], ['is the serif'])], [('regression: overwide word isolation', [['baseline', 'serif', 'is', 'baseline', 'x', 'x', 'at', 'is', 'serif', 'at', 'at'], 6, 9], ['baseline', 'serif', 'is', 'baseline', 'x x at', 'is', 'serif', 'at at']), ('regression: overwide word isolation', [['on', 'at', 'a', 'at', 'type', 'of', 'a', 'baseline', 'ink', 'ink', 'serif', 'baseline'], 5, 7], ['on at', 'a at', 'type', 'of a', 'baseline', 'ink', 'ink', 'serif', 'baseline']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('partial-repair probe', [['ink', 'kerning', 'ink'], 8, 9], ['ink', 'kerning', 'ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['type', 'the', 'a'], 18, 3], ['type the a']), ('control layout', [['a', 'is', 'serif'], 11, 3], ['a is serif'])]]
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):
    words, width, min_last = x
    lines = []
    cur = ''
    for w in words:
        cand = w if not cur else cur + ' ' + w
        if len(cand) <= width or not lines:
            cur = cand
        else:
            lines.append(cur)
            cur = w
    lines.append(cur)
    while len(lines) >= 2 and len(lines[-1]) < min_last:
        prev = lines[-2].split(' ')
        if len(prev) < 2:
            break
        moved = prev[-1] + ' ' + lines[-1]
        if len(moved) > width:
            break
        lines[-2] = ' '.join(prev[:-1])
        lines[-1] = moved
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: overwide word isolation', [['on', 'ink', 'x', 'at', 'a', 'kerning', 'serif', 'measure', 'at', 'baseline'], 7, 3], ['on ink', 'x at a', 'kerning', 'serif', 'measure', 'at', 'baseline']), ('regression: overwide word isolation', [['on', 'of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure'], 5, 4], ['on of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure']), ('partial-repair probe', [['a', 'ink', 'measure', 'serif', 'baseline'], 14, 5], ['a ink measure', 'serif baseline']), ('partial-repair probe', [['baseline', 'on', 'grid', 'kerning', 'a', 'ink', 'baseline', 'measure', 'a'], 10, 3], ['baseline', 'on grid', 'kerning a', 'ink', 'baseline', 'measure a']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['a', 'x', 'grid'], 13, 9], ['a x grid']), ('control layout', [['serif', 'a', 'the'], 17, 4], ['serif a the'])], [('regression: overwide word isolation', [['serif', 'baseline', 'grid', 'of', 'is', 'kerning'], 5, 8], ['serif', 'baseline', 'grid', 'of is', 'kerning']), ('regression: overwide word isolation', [['a', 'baseline', 'the', 'a', 'type', 'kerning', 'is', 'serif', 'a', 'grid', 'the'], 6, 3], ['a', 'baseline', 'the a', 'type', 'kerning', 'is', 'serif', 'a grid', 'the']), ('partial-repair probe', [['on', 'ink', 'type', 'a'], 12, 4], ['on ink', 'type a']), ('regression: overwide word isolation', [['baseline', 'a', 'a', 'is', 'a', 'type', 'on', 'baseline', 'serif', 'baseline', 'serif'], 7, 8], ['baseline', 'a a is', 'a type', 'on', 'baseline', 'serif', 'baseline', 'serif']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['on', 'is', 'at'], 17, 3], ['on is at']), ('control layout', [['x', 'on', 'serif'], 18, 4], ['x on serif'])], [('regression: overwide word isolation', [['kerning', 'grid', 'a', 'kerning', 'serif', 'is', 'the', 'kerning', 'baseline', 'serif'], 7, 4], ['kerning', 'grid a', 'kerning', 'serif', 'is the', 'kerning', 'baseline', 'serif']), ('regression: overwide word isolation', [['on', 'at', 'grid', 'measure', 'type', 'measure', 'on', 'x', 'at', 'a', 'baseline'], 7, 5], ['on at', 'grid', 'measure', 'type', 'measure', 'on x at', 'a', 'baseline']), ('partial-repair probe', [['grid', 'serif', 'at', 'measure'], 15, 3], ['grid serif at', 'measure']), ('partial-repair probe', [['ink', 'the', 'kerning', 'x', 'kerning'], 18, 7], ['ink the kerning x', 'kerning']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['is', 'measure', 'at'], 19, 4], ['is measure at']), ('control layout', [['measure', 'on', 'x', 'ink'], 16, 7], ['measure on x ink'])], [('regression: overwide word isolation', [['the', 'serif', 'baseline', 'x', 'a', 'baseline', 'measure', 'ink', 'of', 'measure', 'the'], 6, 8], ['the', 'serif', 'baseline', 'x a', 'baseline', 'measure', 'ink of', 'measure', 'the']), ('regression: overwide word isolation', [['on', 'of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure'], 5, 4], ['on of', 'kerning', 'on', 'kerning', 'grid', 'baseline', 'grid', 'a', 'measure', 'at', 'measure']), ('partial-repair probe', [['at', 'a', 'on', 'grid', 'grid', 'measure', 'the', 'x', 'at', 'at', 'type'], 20, 8], ['at a on grid grid', 'measure the x', 'at at type']), ('regression: overwide word isolation', [['serif', 'of', 'at', 'the', 'measure', 'ink', 'ink', 'a', 'x'], 5, 8], ['serif', 'of at', 'the', 'measure', 'ink', 'ink', 'a x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('control layout', [['x', 'on', 'serif'], 18, 4], ['x on serif']), ('control layout', [['is', 'the', 'serif'], 12, 9], ['is the serif'])], [('regression: overwide word isolation', [['baseline', 'serif', 'is', 'baseline', 'x', 'x', 'at', 'is', 'serif', 'at', 'at'], 6, 9], ['baseline', 'serif', 'is', 'baseline', 'x x at', 'is', 'serif', 'at at']), ('regression: overwide word isolation', [['on', 'at', 'a', 'at', 'type', 'of', 'a', 'baseline', 'ink', 'ink', 'serif', 'baseline'], 5, 7], ['on at', 'a at', 'type', 'of a', 'baseline', 'ink', 'ink', 'serif', 'baseline']), ('partial-repair probe', [['of', 'at', 'ink', 'measure', 'at', 'the', 'measure', 'ink'], 11, 6], ['of at ink', 'measure at', 'the', 'measure ink']), ('partial-repair probe', [['ink', 'kerning', 'ink'], 8, 9], ['ink', 'kerning', 'ink']), ('runt limited by width', [['kerning', 'baseline', 'x'], 10, 4], ['kerning', 'baseline x']), ('one-word runt pulled down', [['the', 'type', 'grid', 'is', 'a'], 12, 5], ['the type', 'grid is a']), ('control layout', [['type', 'the', 'a'], 18, 3], ['type the a']), ('control layout', [['a', 'is', 'serif'], 11, 3], ['a is serif'])]]
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:
Overwide words are attached to the current line or leave empty lines.

Input [words, width, minimum last-line length]. Greedy fill (unit spaces; overwide word alone). Then while there are at least two lines and the last line string is shorter than the minimum, move the last word of the previous line down if that line keeps at least one word and the new last line fits. Return lines.

split: open-access