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

N = 1
observations = []
def solve(x):
    words, width = x
    SINGLE = {'a', 'i', 'o', 'u', 'v', 'z', 'k', 's', 'w'}
    UNITS = {'kg', 'km', 'cm', 'mm', '%', 'pt', 'px'}
    groups = []
    k = 0
    while k < len(words):
        grp = [words[k]]
        while True:
            last = grp[-1]
            nxt = k + len(grp)
            if nxt >= len(words):
                break
            number = last.replace('.', '').replace(',', '').isdigit()
            if last.lower() in SINGLE or (words[nxt].replace('.', '').isdigit() and last in UNITS):
                grp.append(words[nxt])
            else:
                break
        groups.append(' '.join(grp))
        k += len(grp)
    lines, cur = [], ''
    for g in groups:
        cand = g if not cur else cur + ' ' + g
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = g
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: unit binding direction', [['domě', 'pt', 'a', 'a', 'z', 'sazba', 'v', 'domě', 'cm', 'a', '10', 'km'], 15], ['domě pt', 'a a z sazba', 'v domě cm', 'a 10 km']), ('partial-repair probe', [['i', 'i', '2.5', 'domě', 'V', 'o', 'km', 'V'], 11], ['i i 2.5', 'domě V o km', 'V']), ('regression: unit binding direction', [['1,5', 'kg', 's', 'sazba', '2.5', 'km', '10', 'pt', 'cm'], 6], ['1,5 kg', 's sazba', '2.5 km', '10 pt', 'cm']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['10', 'V', 'o', 'i', 'v', 'a'], 12], ['10 V o i v a']), ('control layout', [['o', 'k', 'řádek', 'V'], 9], ['o k řádek', 'V'])], [('regression: unit binding direction', [['domě', 'pt', 'a', 'a', 'z', 'sazba', 'v', 'domě', 'cm', 'a', '10', 'km'], 15], ['domě pt', 'a a z sazba', 'v domě cm', 'a 10 km']), ('regression: unit binding direction', [['o', 'km', 'pt', '10', 'kg', 'Praze', 'text', 'k', 'sazba'], 7], ['o km pt', '10 kg', 'Praze', 'text', 'k sazba']), ('partial-repair probe', [['V', '10', '2.5', 'v', 'o', 'Praze', 'text', '2.5'], 9], ['V 10 2.5', 'v o Praze', 'text 2.5']), ('partial-repair probe', [['V', 'o', 'km', 'pt', 'k', 'a', 's', '1,5', 'domě', '1,5'], 13], ['V o km pt', 'k a s 1,5', 'domě 1,5']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['sazba', 'kg', 'sazba', 'domě', 'domě', 'Praze', 'domě'], 13], ['sazba kg', 'sazba domě', 'domě Praze', 'domě']), ('control layout', [['km', 'a', 'text', 'i', 'km', 'z'], 8], ['km', 'a text', 'i km z'])], [('regression: unit binding direction', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: unit binding direction', [['řádek', 'km', '10', 'cm', 'text', 'v', 'sazba', 'domě', 'pt'], 9], ['řádek km', '10 cm', 'text', 'v sazba', 'domě pt']), ('partial-repair probe', [['pt', 'cm', 'pt', 'domě', 'Praze', 'k', 'V', 'text', 'z'], 7], ['pt cm', 'pt domě', 'Praze', 'k V text', 'z']), ('regression: unit binding direction', [['o', 'a', 's', 's', '1,5', 'cm', 'pt'], 7], ['o a s s 1,5 cm', 'pt']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['v', 'i', 'pt', 'V', 'V', 'km', 'v', 'pt', 'sazba', 'k', 's', 'pt'], 7], ['v i pt', 'V V km', 'v pt', 'sazba', 'k s pt']), ('control layout', [['k', 'sazba', 's', 'kg', 'i', 'řádek', 'řádek', 'řádek', 'V', 'text'], 16], ['k sazba s kg', 'i řádek řádek', 'řádek V text'])], [('regression: unit binding direction', [['10', 'i', 'a', 'pt', '10', 'domě', 'o', 'a', 'cm', 'a', 'sazba'], 9], ['10 i a pt', '10 domě', 'o a cm', 'a sazba']), ('regression: unit binding direction', [['text', 'km', '1,5', 'kg', 'km', 's', 'kg', '1,5', 'domě', 'cm'], 12], ['text km', '1,5 kg km', 's kg 1,5', 'domě cm']), ('partial-repair probe', [['cm', 'text', '1,5', '2.5'], 13], ['cm text 1,5', '2.5']), ('regression: unit binding direction', [['i', '2.5', 'pt', 'o', '1,5', 'o', 'i', 'V', 'pt', 'text', '2.5', 'km'], 7], ['i 2.5 pt', 'o 1,5', 'o i V pt', 'text', '2.5 km']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['i', 'km', 'a', 'sazba', 'Praze', 'cm', 'z', 'kg'], 12], ['i km a sazba', 'Praze cm', 'z kg']), ('control layout', [['kg', 'řádek', 'v', 'kg', 'o', 'i', 'km', 's', 'domě', 'kg', 'V', '1,5'], 12], ['kg řádek', 'v kg o i km', 's domě kg', 'V 1,5'])], [('regression: unit binding direction', [['v', 'km', 'a', '10', 'pt', 'i', '1,5'], 11], ['v km', 'a 10 pt', 'i 1,5']), ('regression: unit binding direction', [['Praze', 'sazba', 'text', 'sazba', 'V', 'domě', '2.5', 'km', 'domě'], 12], ['Praze sazba', 'text sazba', 'V domě', '2.5 km domě']), ('partial-repair probe', [['V', '1,5', 'k', 'text', 'řádek', '2.5', 'v', 'kg'], 12], ['V 1,5 k text', 'řádek 2.5', 'v kg']), ('partial-repair probe', [['sazba', '1,5', 'a', 'sazba', '1,5'], 9], ['sazba 1,5', 'a sazba', '1,5']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('control layout', [['kg', 'a', 'sazba', 'domě', 'cm', 'řádek'], 9], ['kg', 'a sazba', 'domě cm', 'řádek']), ('control layout', [['i', 'a', 'i', 's', 'v', 'řádek', 'Praze'], 11], ['i a i s v řádek', 'Praze'])]]
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 = x
    SINGLE = {'a', 'i', 'o', 'u', 'v', 'z', 'k', 's', 'w'}
    UNITS = {'kg', 'km', 'cm', 'mm', '%', 'pt', 'px'}
    groups = []
    k = 0
    while k < len(words):
        grp = [words[k]]
        while True:
            last = grp[-1]
            nxt = k + len(grp)
            if nxt >= len(words):
                break
            number = last.replace('.', '').replace(',', '').isdigit()
            if last.lower() in SINGLE or (number or words[nxt] in UNITS):
                grp.append(words[nxt])
            else:
                break
        groups.append(' '.join(grp))
        k += len(grp)
    lines, cur = [], ''
    for g in groups:
        cand = g if not cur else cur + ' ' + g
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = g
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: unit binding direction', [['domě', 'pt', 'a', 'a', 'z', 'sazba', 'v', 'domě', 'cm', 'a', '10', 'km'], 15], ['domě pt', 'a a z sazba', 'v domě cm', 'a 10 km']), ('partial-repair probe', [['i', 'i', '2.5', 'domě', 'V', 'o', 'km', 'V'], 11], ['i i 2.5', 'domě V o km', 'V']), ('regression: unit binding direction', [['1,5', 'kg', 's', 'sazba', '2.5', 'km', '10', 'pt', 'cm'], 6], ['1,5 kg', 's sazba', '2.5 km', '10 pt', 'cm']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['10', 'V', 'o', 'i', 'v', 'a'], 12], ['10 V o i v a']), ('control layout', [['o', 'k', 'řádek', 'V'], 9], ['o k řádek', 'V'])], [('regression: unit binding direction', [['domě', 'pt', 'a', 'a', 'z', 'sazba', 'v', 'domě', 'cm', 'a', '10', 'km'], 15], ['domě pt', 'a a z sazba', 'v domě cm', 'a 10 km']), ('regression: unit binding direction', [['o', 'km', 'pt', '10', 'kg', 'Praze', 'text', 'k', 'sazba'], 7], ['o km pt', '10 kg', 'Praze', 'text', 'k sazba']), ('partial-repair probe', [['V', '10', '2.5', 'v', 'o', 'Praze', 'text', '2.5'], 9], ['V 10 2.5', 'v o Praze', 'text 2.5']), ('partial-repair probe', [['V', 'o', 'km', 'pt', 'k', 'a', 's', '1,5', 'domě', '1,5'], 13], ['V o km pt', 'k a s 1,5', 'domě 1,5']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['sazba', 'kg', 'sazba', 'domě', 'domě', 'Praze', 'domě'], 13], ['sazba kg', 'sazba domě', 'domě Praze', 'domě']), ('control layout', [['km', 'a', 'text', 'i', 'km', 'z'], 8], ['km', 'a text', 'i km z'])], [('regression: unit binding direction', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: unit binding direction', [['řádek', 'km', '10', 'cm', 'text', 'v', 'sazba', 'domě', 'pt'], 9], ['řádek km', '10 cm', 'text', 'v sazba', 'domě pt']), ('partial-repair probe', [['pt', 'cm', 'pt', 'domě', 'Praze', 'k', 'V', 'text', 'z'], 7], ['pt cm', 'pt domě', 'Praze', 'k V text', 'z']), ('regression: unit binding direction', [['o', 'a', 's', 's', '1,5', 'cm', 'pt'], 7], ['o a s s 1,5 cm', 'pt']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['v', 'i', 'pt', 'V', 'V', 'km', 'v', 'pt', 'sazba', 'k', 's', 'pt'], 7], ['v i pt', 'V V km', 'v pt', 'sazba', 'k s pt']), ('control layout', [['k', 'sazba', 's', 'kg', 'i', 'řádek', 'řádek', 'řádek', 'V', 'text'], 16], ['k sazba s kg', 'i řádek řádek', 'řádek V text'])], [('regression: unit binding direction', [['10', 'i', 'a', 'pt', '10', 'domě', 'o', 'a', 'cm', 'a', 'sazba'], 9], ['10 i a pt', '10 domě', 'o a cm', 'a sazba']), ('regression: unit binding direction', [['text', 'km', '1,5', 'kg', 'km', 's', 'kg', '1,5', 'domě', 'cm'], 12], ['text km', '1,5 kg km', 's kg 1,5', 'domě cm']), ('partial-repair probe', [['cm', 'text', '1,5', '2.5'], 13], ['cm text 1,5', '2.5']), ('regression: unit binding direction', [['i', '2.5', 'pt', 'o', '1,5', 'o', 'i', 'V', 'pt', 'text', '2.5', 'km'], 7], ['i 2.5 pt', 'o 1,5', 'o i V pt', 'text', '2.5 km']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['i', 'km', 'a', 'sazba', 'Praze', 'cm', 'z', 'kg'], 12], ['i km a sazba', 'Praze cm', 'z kg']), ('control layout', [['kg', 'řádek', 'v', 'kg', 'o', 'i', 'km', 's', 'domě', 'kg', 'V', '1,5'], 12], ['kg řádek', 'v kg o i km', 's domě kg', 'V 1,5'])], [('regression: unit binding direction', [['v', 'km', 'a', '10', 'pt', 'i', '1,5'], 11], ['v km', 'a 10 pt', 'i 1,5']), ('regression: unit binding direction', [['Praze', 'sazba', 'text', 'sazba', 'V', 'domě', '2.5', 'km', 'domě'], 12], ['Praze sazba', 'text sazba', 'V domě', '2.5 km domě']), ('partial-repair probe', [['V', '1,5', 'k', 'text', 'řádek', '2.5', 'v', 'kg'], 12], ['V 1,5 k text', 'řádek 2.5', 'v kg']), ('partial-repair probe', [['sazba', '1,5', 'a', 'sazba', '1,5'], 9], ['sazba 1,5', 'a sazba', '1,5']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('control layout', [['kg', 'a', 'sazba', 'domě', 'cm', 'řádek'], 9], ['kg', 'a sazba', 'domě cm', 'řádek']), ('control layout', [['i', 'a', 'i', 's', 'v', 'řádek', 'Praze'], 11], ['i a i s v řádek', 'Praze'])]]
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:
Units are separated from their numbers.

Input [words, width]. A single-letter preposition/conjunction (a i o u v z k s w, any case) is bound to the following word, chaining; a number (digits with optional . or , decimal) is bound to a following unit (kg km cm mm % pt px). Bound groups are unbreakable; greedy fill of groups with unit spaces; an overwide group is set alone. 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 = x
    SINGLE = {'a', 'i', 'o', 'u', 'v', 'z', 'k', 's', 'w'}
    UNITS = {'kg', 'km', 'cm', 'mm', '%', 'pt', 'px'}
    groups = []
    k = 0
    while k < len(words):
        grp = [words[k]]
        while True:
            last = grp[-1]
            nxt = k + len(grp)
            if nxt >= len(words):
                break
            number = last.isdigit()
            if last.lower() in SINGLE or (number and words[nxt] in UNITS):
                grp.append(words[nxt])
            else:
                break
        groups.append(' '.join(grp))
        k += len(grp)
    lines, cur = [], ''
    for g in groups:
        cand = g if not cur else cur + ' ' + g
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = g
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('regression: decimal number detection', [['z', 'text', 'domě', '1,5', 'kg', 'cm', 'cm', '10', 'o', 'kg', 'pt'], 16], ['z text domě', '1,5 kg cm cm 10', 'o kg pt']), ('regression: decimal number detection', [['v', 'Praze', 'Praze', 'v', '1,5', 'cm', 'k', 'V', '10'], 12], ['v Praze', 'Praze', 'v 1,5 cm', 'k V 10']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['kg', 'km', 's', '2.5', 'z', 'i', 'pt'], 6], ['kg km', 's 2.5', 'z i pt']), ('control layout', [['a', 'a', 's', 'V', 'sazba', 'z'], 8], ['a a s V sazba', 'z'])], [('regression: decimal number detection', [['z', 'domě', 's', 'V', 's', 's', 's', 'k', '2.5', 'cm', '10', 'k'], 8], ['z domě', 's V s s s k 2.5 cm', '10 k']), ('regression: decimal number detection', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: decimal number detection', [['pt', 'cm', 'z', 'km', 'domě', '1,5', 'km', 'pt', 'v', 'v', '1,5', 'z'], 10], ['pt cm z km', 'domě', '1,5 km pt', 'v v 1,5 z']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['sazba', 'V', '1,5', 's', 'z'], 8], ['sazba', 'V 1,5', 's z']), ('control layout', [['řádek', 'pt', 'v', 's', 'V', 'pt', 'v'], 14], ['řádek pt', 'v s V pt v'])], [('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: decimal number detection', [['text', '1,5', 'V', 'k', 'sazba', '1,5', 'km', 'text', 'i'], 15], ['text 1,5', 'V k sazba', '1,5 km text i']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('regression: decimal number detection', [['v', 'Praze', 'Praze', 'v', '1,5', 'cm', 'k', 'V', '10'], 12], ['v Praze', 'Praze', 'v 1,5 cm', 'k V 10']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['V', 'V', 'Praze', 'řádek', '2.5', 'domě'], 13], ['V V Praze', 'řádek 2.5', 'domě']), ('control layout', [['s', 'text', 'a', 'text', 'sazba', '10', 'v', 'text', '10', '10'], 6], ['s text', 'a text', 'sazba', '10', 'v text', '10 10'])], [('regression: decimal number detection', [['text', '1,5', 'V', 'k', 'sazba', '1,5', 'km', 'text', 'i'], 15], ['text 1,5', 'V k sazba', '1,5 km text i']), ('regression: decimal number detection', [['i', '2.5', 'pt', 'o', '1,5', 'o', 'i', 'V', 'pt', 'text', '2.5', 'km'], 7], ['i 2.5 pt', 'o 1,5', 'o i V pt', 'text', '2.5 km']), ('regression: decimal number detection', [['pt', 'cm', 'z', 'km', 'domě', '1,5', 'km', 'pt', 'v', 'v', '1,5', 'z'], 10], ['pt cm z km', 'domě', '1,5 km pt', 'v v 1,5 z']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['v', 's', 'k', 'domě', 'k', 'k'], 6], ['v s k domě', 'k k']), ('control layout', [['s', 'text', 'a', 'text', 'sazba', '10', 'v', 'text', '10', '10'], 6], ['s text', 'a text', 'sazba', '10', 'v text', '10 10'])], [('regression: decimal number detection', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: decimal number detection', [['o', 'a', 's', 's', '1,5', 'cm', 'pt'], 7], ['o a s s 1,5 cm', 'pt']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['V', '1,5', 'pt', '1,5', 'z', 'o', 'i'], 11], ['V 1,5 pt', '1,5 z o i']), ('control layout', [['1,5', 'km', 'domě', 'sazba', 'kg', 'V', 'v', 'text', 'domě', '1,5'], 11], ['1,5 km domě', 'sazba kg', 'V v text', 'domě 1,5'])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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 = x
    SINGLE = {'a', 'i', 'o', 'u', 'v', 'z', 'k', 's', 'w'}
    UNITS = {'kg', 'km', 'cm', 'mm', '%', 'pt', 'px'}
    groups = []
    k = 0
    while k < len(words):
        grp = [words[k]]
        while True:
            last = grp[-1]
            nxt = k + len(grp)
            if nxt >= len(words):
                break
            number = last.replace('.', '').isdigit()
            if last.lower() in SINGLE or (number and words[nxt] in UNITS):
                grp.append(words[nxt])
            else:
                break
        groups.append(' '.join(grp))
        k += len(grp)
    lines, cur = [], ''
    for g in groups:
        cand = g if not cur else cur + ' ' + g
        if len(cand) <= width or not cur:
            cur = cand
        else:
            lines.append(cur)
            cur = g
    if cur:
        lines.append(cur)
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('regression: decimal number detection', [['z', 'text', 'domě', '1,5', 'kg', 'cm', 'cm', '10', 'o', 'kg', 'pt'], 16], ['z text domě', '1,5 kg cm cm 10', 'o kg pt']), ('regression: decimal number detection', [['v', 'Praze', 'Praze', 'v', '1,5', 'cm', 'k', 'V', '10'], 12], ['v Praze', 'Praze', 'v 1,5 cm', 'k V 10']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['kg', 'km', 's', '2.5', 'z', 'i', 'pt'], 6], ['kg km', 's 2.5', 'z i pt']), ('control layout', [['a', 'a', 's', 'V', 'sazba', 'z'], 8], ['a a s V sazba', 'z'])], [('regression: decimal number detection', [['z', 'domě', 's', 'V', 's', 's', 's', 'k', '2.5', 'cm', '10', 'k'], 8], ['z domě', 's V s s s k 2.5 cm', '10 k']), ('regression: decimal number detection', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: decimal number detection', [['pt', 'cm', 'z', 'km', 'domě', '1,5', 'km', 'pt', 'v', 'v', '1,5', 'z'], 10], ['pt cm z km', 'domě', '1,5 km pt', 'v v 1,5 z']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['sazba', 'V', '1,5', 's', 'z'], 8], ['sazba', 'V 1,5', 's z']), ('control layout', [['řádek', 'pt', 'v', 's', 'V', 'pt', 'v'], 14], ['řádek pt', 'v s V pt v'])], [('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('regression: decimal number detection', [['text', '1,5', 'V', 'k', 'sazba', '1,5', 'km', 'text', 'i'], 15], ['text 1,5', 'V k sazba', '1,5 km text i']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('regression: decimal number detection', [['v', 'Praze', 'Praze', 'v', '1,5', 'cm', 'k', 'V', '10'], 12], ['v Praze', 'Praze', 'v 1,5 cm', 'k V 10']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['V', 'V', 'Praze', 'řádek', '2.5', 'domě'], 13], ['V V Praze', 'řádek 2.5', 'domě']), ('control layout', [['s', 'text', 'a', 'text', 'sazba', '10', 'v', 'text', '10', '10'], 6], ['s text', 'a text', 'sazba', '10', 'v text', '10 10'])], [('regression: decimal number detection', [['text', '1,5', 'V', 'k', 'sazba', '1,5', 'km', 'text', 'i'], 15], ['text 1,5', 'V k sazba', '1,5 km text i']), ('regression: decimal number detection', [['i', '2.5', 'pt', 'o', '1,5', 'o', 'i', 'V', 'pt', 'text', '2.5', 'km'], 7], ['i 2.5 pt', 'o 1,5', 'o i V pt', 'text', '2.5 km']), ('regression: decimal number detection', [['pt', 'cm', 'z', 'km', 'domě', '1,5', 'km', 'pt', 'v', 'v', '1,5', 'z'], 10], ['pt cm z km', 'domě', '1,5 km pt', 'v v 1,5 z']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('control layout', [['v', 's', 'k', 'domě', 'k', 'k'], 6], ['v s k domě', 'k k']), ('control layout', [['s', 'text', 'a', 'text', 'sazba', '10', 'v', 'text', '10', '10'], 6], ['s text', 'a text', 'sazba', '10', 'v text', '10 10'])], [('regression: decimal number detection', [['o', 'i', 'k', 'V', 'text', 'kg', '2.5', 'kg', 'cm'], 7], ['o i k V text', 'kg', '2.5 kg', 'cm']), ('regression: decimal number detection', [['o', 'a', 's', 's', '1,5', 'cm', 'pt'], 7], ['o a s s 1,5 cm', 'pt']), ('regression: decimal number detection', [['text', 's', 'sazba', 'i', 'i', 'text', 'pt', '1,5', 'km', 'domě', 'i'], 16], ['text s sazba', 'i i text pt', '1,5 km domě i']), ('decimal comma with unit', [['sazba', '1,5', 'cm', 'text'], 9], ['sazba', '1,5 cm', 'text']), ('sentence-initial V binds', [['V', 'Praze', 'text', 'sazba'], 8], ['V Praze', 'text', 'sazba']), ('chained singles', [['text', 'v', 'a', 'k', 'domě'], 10], ['text', 'v a k domě']), ('control layout', [['V', '1,5', 'pt', '1,5', 'z', 'o', 'i'], 11], ['V 1,5 pt', '1,5 z o i']), ('control layout', [['1,5', 'km', 'domě', 'sazba', 'kg', 'V', 'v', 'text', 'domě', '1,5'], 11], ['1,5 km domě', 'sazba kg', 'V v text', 'domě 1,5'])]]
for label, args, expected in fixtures[N - 1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Decimal quantities like 2.5 or 1,5 are split from their units.

Input [words, width]. A single-letter preposition/conjunction (a i o u v z k s w, any case) is bound to the following word, chaining; a number (digits with optional . or , decimal) is bound to a following unit (kg km cm mm % pt px). Bound groups are unbreakable; greedy fill of groups with unit spaces; an overwide group is set alone. 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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c < target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: line count target', [['the', 'type', 'of', 'kerning', 'at', 'measure', 'ink', 'of', 'grid'], 17], ['the type of', 'kerning at', 'measure ink', 'of grid']), ('partial-repair probe', [['grid', 'serif', 'type', 'on', 'on'], 26], ['grid serif type on on']), ('partial-repair probe', [['measure', 'measure', 'of', 'a', 'on', 'at'], 11], ['measure', 'measure of', 'a on at']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'baseline'], 13], ['kerning', 'baseline']), ('control layout', [['grid', 'baseline', 'at', 'type', 'at', 'is', 'baseline', 'is'], 6], ['overflow']), ('control layout', [['grid', 'is', 'kerning', 'grid'], 10], ['grid is', 'kerning', 'grid'])], [('regression: line count target', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: line count target', [['grid', 'baseline', 'grid', 'measure', 'x', 'is'], 20], ['grid baseline', 'grid measure x is']), ('partial-repair probe', [['grid', 'is', 'type', 'ink', 'serif', 'x'], 25], ['grid is type ink serif x']), ('partial-repair probe', [['a', 'of', 'is'], 27], ['a of is']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'kerning', 'ink', 'is', 'baseline'], 10], ['baseline', 'kerning', 'ink is', 'baseline']), ('control layout', [['x', 'of', 'kerning', 'ink'], 9], ['x of', 'kerning', 'ink'])], [('regression: line count target', [['a', 'measure', 'serif', 'x', 'at', 'a', 'serif'], 23], ['a measure serif', 'x at a serif']), ('regression: line count target', [['measure', 'serif', 'at', 'at', 'kerning', 'x'], 23], ['measure serif', 'at at kerning x']), ('regression: line count target', [['grid', 'grid', 'on', 'measure', 'measure', 'of', 'grid'], 19], ['grid grid on', 'measure measure', 'of grid']), ('regression: line count target', [['type', 'measure', 'baseline', 'type', 'a'], 25], ['type measure', 'baseline type a']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('control layout', [['kerning', 'on', 'the'], 9], ['kerning', 'on the'])], [('regression: line count target', [['grid', 'grid', 'measure', 'x'], 18], ['grid grid', 'measure x']), ('regression: line count target', [['is', 'measure', 'the', 'grid', 'is', 'a', 'kerning', 'baseline'], 22], ['is measure the grid', 'is a kerning baseline']), ('partial-repair probe', [['at', 'a', 'ink', 'at', 'ink'], 20], ['at a ink at ink']), ('partial-repair probe', [['baseline', 'grid', 'type', 'kerning', 'grid'], 10], ['baseline', 'grid type', 'kerning', 'grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'baseline', 'a', 'measure', 'is'], 5], ['overflow']), ('control layout', [['is', 'ink', 'on', 'serif', 'serif', 'baseline', 'ink', 'baseline', 'baseline'], 5], ['overflow'])], [('regression: line count target', [['ink', 'grid', 'serif', 'type', 'baseline', 'the', 'baseline'], 29], ['ink grid serif type', 'baseline the baseline']), ('regression: line count target', [['x', 'on', 'measure', 'baseline', 'on', 'the'], 23], ['x on measure', 'baseline on the']), ('partial-repair probe', [['the', 'of', 'ink', 'ink', 'grid', 'is'], 27], ['the of ink ink grid is']), ('partial-repair probe', [['the', 'of', 'the', 'baseline'], 28], ['the of the baseline']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('control layout', [['kerning', 'the'], 6], ['overflow'])]]
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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target + 1:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: line count target', [['the', 'type', 'of', 'kerning', 'at', 'measure', 'ink', 'of', 'grid'], 17], ['the type of', 'kerning at', 'measure ink', 'of grid']), ('partial-repair probe', [['grid', 'serif', 'type', 'on', 'on'], 26], ['grid serif type on on']), ('partial-repair probe', [['measure', 'measure', 'of', 'a', 'on', 'at'], 11], ['measure', 'measure of', 'a on at']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'baseline'], 13], ['kerning', 'baseline']), ('control layout', [['grid', 'baseline', 'at', 'type', 'at', 'is', 'baseline', 'is'], 6], ['overflow']), ('control layout', [['grid', 'is', 'kerning', 'grid'], 10], ['grid is', 'kerning', 'grid'])], [('regression: line count target', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: line count target', [['grid', 'baseline', 'grid', 'measure', 'x', 'is'], 20], ['grid baseline', 'grid measure x is']), ('partial-repair probe', [['grid', 'is', 'type', 'ink', 'serif', 'x'], 25], ['grid is type ink serif x']), ('partial-repair probe', [['a', 'of', 'is'], 27], ['a of is']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'kerning', 'ink', 'is', 'baseline'], 10], ['baseline', 'kerning', 'ink is', 'baseline']), ('control layout', [['x', 'of', 'kerning', 'ink'], 9], ['x of', 'kerning', 'ink'])], [('regression: line count target', [['a', 'measure', 'serif', 'x', 'at', 'a', 'serif'], 23], ['a measure serif', 'x at a serif']), ('regression: line count target', [['measure', 'serif', 'at', 'at', 'kerning', 'x'], 23], ['measure serif', 'at at kerning x']), ('regression: line count target', [['grid', 'grid', 'on', 'measure', 'measure', 'of', 'grid'], 19], ['grid grid on', 'measure measure', 'of grid']), ('regression: line count target', [['type', 'measure', 'baseline', 'type', 'a'], 25], ['type measure', 'baseline type a']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('control layout', [['kerning', 'on', 'the'], 9], ['kerning', 'on the'])], [('regression: line count target', [['grid', 'grid', 'measure', 'x'], 18], ['grid grid', 'measure x']), ('regression: line count target', [['is', 'measure', 'the', 'grid', 'is', 'a', 'kerning', 'baseline'], 22], ['is measure the grid', 'is a kerning baseline']), ('partial-repair probe', [['at', 'a', 'ink', 'at', 'ink'], 20], ['at a ink at ink']), ('partial-repair probe', [['baseline', 'grid', 'type', 'kerning', 'grid'], 10], ['baseline', 'grid type', 'kerning', 'grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'baseline', 'a', 'measure', 'is'], 5], ['overflow']), ('control layout', [['is', 'ink', 'on', 'serif', 'serif', 'baseline', 'ink', 'baseline', 'baseline'], 5], ['overflow'])], [('regression: line count target', [['ink', 'grid', 'serif', 'type', 'baseline', 'the', 'baseline'], 29], ['ink grid serif type', 'baseline the baseline']), ('regression: line count target', [['x', 'on', 'measure', 'baseline', 'on', 'the'], 23], ['x on measure', 'baseline on the']), ('partial-repair probe', [['the', 'of', 'ink', 'ink', 'grid', 'is'], 27], ['the of ink ink grid is']), ('partial-repair probe', [['the', 'of', 'the', 'baseline'], 28], ['the of the baseline']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('control layout', [['kerning', 'the'], 6], ['overflow'])]]
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:
Balancing collapses to the original ragged wrap.

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) >= w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('partial-repair probe', [['is', 'ink', 'serif', 'kerning', 'x', 'baseline', 'kerning', 'of', 'is'], 7], ['overflow']), ('partial-repair probe', [['a', 'serif', 'baseline', 'ink', 'at', 'measure', 'baseline'], 7], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['ink', 'x'], 28], ['ink x']), ('control layout', [['measure', 'on', 'kerning'], 13], ['measure on', 'kerning']), ('control layout', [['kerning', 'x', 'is'], 27], ['kerning x is'])], [('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: overwide word test', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('partial-repair probe', [['type', 'the', 'measure', 'kerning', 'is'], 6], ['overflow']), ('partial-repair probe', [['a', 'serif', 'baseline', 'ink', 'at', 'measure', 'baseline'], 7], ['overflow']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'x'], 11], ['x x']), ('control layout', [['on', 'serif', 'measure', 'ink', 'a', 'on', 'x', 'of'], 22], ['on serif measure', 'ink a on x of'])], [('regression: overwide word test', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: overwide word test', [['x', 'baseline', 'is', 'ink', 'type', 'measure', 'on', 'a', 'the'], 8], ['x', 'baseline', 'is ink', 'type', 'measure', 'on a the']), ('partial-repair probe', [['on', 'a', 'grid', 'ink', 'baseline'], 7], ['overflow']), ('partial-repair probe', [['measure', 'x'], 6], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'a', 'on', 'x', 'of', 'the', 'a', 'grid'], 16], ['kerning a on x', 'of the a grid']), ('control layout', [['at', 'baseline', 'baseline', 'grid', 'a', 'on', 'baseline', 'of'], 21], ['at baseline baseline', 'grid a on baseline of'])], [('regression: overwide word test', [['is', 'is', 'the', 'serif'], 5], ['is is', 'the', 'serif']), ('regression: overwide word test', [['on', 'baseline'], 8], ['on', 'baseline']), ('partial-repair probe', [['is', 'ink', 'serif', 'kerning', 'x', 'baseline', 'kerning', 'of', 'is'], 7], ['overflow']), ('partial-repair probe', [['at', 'type', 'is', 'baseline', 'ink'], 7], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'ink', 'a'], 19], ['the ink a']), ('control layout', [['a', 'baseline', 'type'], 6], ['overflow'])], [('regression: overwide word test', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('partial-repair probe', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('partial-repair probe', [['kerning', 'serif', 'of', 'grid', 'serif', 'on', 'grid', 'x'], 6], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'on', 'at', 'of', 'is', 'measure', 'type'], 10], ['the on', 'at of is', 'measure', 'type']), ('control layout', [['type', 'at', 'ink', 'of', 'measure'], 14], ['type at ink', 'of measure'])]]
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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w + 1:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('partial-repair probe', [['is', 'ink', 'serif', 'kerning', 'x', 'baseline', 'kerning', 'of', 'is'], 7], ['overflow']), ('partial-repair probe', [['a', 'serif', 'baseline', 'ink', 'at', 'measure', 'baseline'], 7], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['ink', 'x'], 28], ['ink x']), ('control layout', [['measure', 'on', 'kerning'], 13], ['measure on', 'kerning']), ('control layout', [['kerning', 'x', 'is'], 27], ['kerning x is'])], [('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: overwide word test', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('partial-repair probe', [['type', 'the', 'measure', 'kerning', 'is'], 6], ['overflow']), ('partial-repair probe', [['a', 'serif', 'baseline', 'ink', 'at', 'measure', 'baseline'], 7], ['overflow']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'x'], 11], ['x x']), ('control layout', [['on', 'serif', 'measure', 'ink', 'a', 'on', 'x', 'of'], 22], ['on serif measure', 'ink a on x of'])], [('regression: overwide word test', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: overwide word test', [['x', 'baseline', 'is', 'ink', 'type', 'measure', 'on', 'a', 'the'], 8], ['x', 'baseline', 'is ink', 'type', 'measure', 'on a the']), ('partial-repair probe', [['on', 'a', 'grid', 'ink', 'baseline'], 7], ['overflow']), ('partial-repair probe', [['measure', 'x'], 6], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'a', 'on', 'x', 'of', 'the', 'a', 'grid'], 16], ['kerning a on x', 'of the a grid']), ('control layout', [['at', 'baseline', 'baseline', 'grid', 'a', 'on', 'baseline', 'of'], 21], ['at baseline baseline', 'grid a on baseline of'])], [('regression: overwide word test', [['is', 'is', 'the', 'serif'], 5], ['is is', 'the', 'serif']), ('regression: overwide word test', [['on', 'baseline'], 8], ['on', 'baseline']), ('partial-repair probe', [['is', 'ink', 'serif', 'kerning', 'x', 'baseline', 'kerning', 'of', 'is'], 7], ['overflow']), ('partial-repair probe', [['at', 'type', 'is', 'baseline', 'ink'], 7], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'ink', 'a'], 19], ['the ink a']), ('control layout', [['a', 'baseline', 'type'], 6], ['overflow'])], [('regression: overwide word test', [['kerning', 'type', 'is', 'is', 'is', 'measure', 'measure', 'type', 'at'], 7], ['kerning', 'type is', 'is is', 'measure', 'measure', 'type at']), ('regression: overwide word test', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('partial-repair probe', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('partial-repair probe', [['kerning', 'serif', 'of', 'grid', 'serif', 'on', 'grid', 'x'], 6], ['overflow']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'on', 'at', 'of', 'is', 'measure', 'type'], 10], ['the on', 'at of is', 'measure', 'type']), ('control layout', [['type', 'at', 'ink', 'of', 'measure'], 14], ['type at ink', 'of measure'])]]
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 exactly as wide as a candidate width are rejected.

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: interword space in counting', [['at', 'kerning', 'baseline', 'kerning', 'x', 'a', 'at', 'x'], 29], ['at kerning baseline', 'kerning x a at x']), ('regression: interword space in counting', [['is', 'on', 'the', 'grid', 'a', 'is', 'measure'], 14], ['is on the grid', 'a is measure']), ('regression: interword space in counting', [['at', 'serif', 'kerning', 'on', 'the', 'serif', 'at', 'the', 'type'], 12], ['at serif', 'kerning on', 'the serif', 'at the type']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('control layout', [['grid', 'is', 'kerning', 'grid'], 10], ['grid is', 'kerning', 'grid']), ('control layout', [['grid', 'a', 'at', 'serif', 'serif'], 5], ['grid', 'a at', 'serif', 'serif'])], [('regression: interword space in counting', [['x', 'a'], 25], ['x a']), ('regression: interword space in counting', [['of', 'kerning', 'is'], 18], ['of kerning is']), ('regression: interword space in counting', [['on', 'serif', 'measure', 'the', 'on', 'the'], 16], ['on serif measure', 'the on the']), ('regression: interword space in counting', [['of', 'x', 'at', 'grid', 'measure', 'at', 'the', 'at', 'the'], 10], ['of x at', 'grid', 'measure at', 'the at the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['measure', 'kerning', 'a'], 7], ['measure', 'kerning', 'a']), ('control layout', [['is', 'is', 'the', 'serif'], 5], ['is is', 'the', 'serif'])], [('regression: interword space in counting', [['ink', 'a', 'a'], 6], ['ink', 'a a']), ('regression: interword space in counting', [['kerning', 'baseline', 'a', 'serif'], 27], ['kerning baseline a serif']), ('regression: interword space in counting', [['baseline', 'is'], 11], ['baseline is']), ('regression: interword space in counting', [['of', 'a', 'kerning', 'of', 'kerning', 'of', 'type', 'the', 'x'], 21], ['of a kerning of', 'kerning of type the x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['a', 'serif', 'serif', 'baseline', 'grid', 'measure'], 11], ['a serif', 'serif', 'baseline', 'grid', 'measure']), ('control layout', [['x', 'baseline', 'a', 'measure', 'is'], 5], ['overflow'])], [('regression: interword space in counting', [['at', 'kerning', 'at', 'measure'], 29], ['at kerning at measure']), ('regression: interword space in counting', [['of', 'is', 'the', 'on', 'kerning'], 17], ['of is the', 'on kerning']), ('regression: interword space in counting', [['the', 'x', 'type', 'serif', 'grid', 'is', 'a'], 17], ['the x type', 'serif grid is a']), ('regression: interword space in counting', [['baseline', 'measure', 'measure'], 15], ['baseline', 'measure measure']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'on', 'the'], 9], ['kerning', 'on the']), ('control layout', [['x', 'baseline', 'is', 'ink', 'type', 'measure', 'on', 'a', 'the'], 8], ['x', 'baseline', 'is ink', 'type', 'measure', 'on a the'])], [('regression: interword space in counting', [['grid', 'on', 'baseline', 'x', 'x', 'grid', 'x', 'a', 'is'], 30], ['grid on baseline', 'x x grid x a is']), ('regression: interword space in counting', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: interword space in counting', [['the', 'at', 'of', 'on', 'on', 'serif'], 17], ['the at of', 'on on serif']), ('regression: interword space in counting', [['kerning', 'kerning', 'ink', 'ink', 'type', 'baseline'], 15], ['kerning kerning', 'ink ink type', 'baseline']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['grid', 'serif', 'type'], 6], ['grid', 'serif', 'type']), ('control layout', [['kerning', 'measure', 'type', 'on', 'measure', 'x', 'baseline'], 6], ['overflow'])]]
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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) < w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: interword space in counting', [['at', 'kerning', 'baseline', 'kerning', 'x', 'a', 'at', 'x'], 29], ['at kerning baseline', 'kerning x a at x']), ('regression: interword space in counting', [['is', 'on', 'the', 'grid', 'a', 'is', 'measure'], 14], ['is on the grid', 'a is measure']), ('regression: interword space in counting', [['at', 'serif', 'kerning', 'on', 'the', 'serif', 'at', 'the', 'type'], 12], ['at serif', 'kerning on', 'the serif', 'at the type']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['measure', 'x', 'serif', 'a', 'a', 'x', 'is', 'of', 'x'], 6], ['overflow']), ('control layout', [['grid', 'is', 'kerning', 'grid'], 10], ['grid is', 'kerning', 'grid']), ('control layout', [['grid', 'a', 'at', 'serif', 'serif'], 5], ['grid', 'a at', 'serif', 'serif'])], [('regression: interword space in counting', [['x', 'a'], 25], ['x a']), ('regression: interword space in counting', [['of', 'kerning', 'is'], 18], ['of kerning is']), ('regression: interword space in counting', [['on', 'serif', 'measure', 'the', 'on', 'the'], 16], ['on serif measure', 'the on the']), ('regression: interword space in counting', [['of', 'x', 'at', 'grid', 'measure', 'at', 'the', 'at', 'the'], 10], ['of x at', 'grid', 'measure at', 'the at the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['measure', 'kerning', 'a'], 7], ['measure', 'kerning', 'a']), ('control layout', [['is', 'is', 'the', 'serif'], 5], ['is is', 'the', 'serif'])], [('regression: interword space in counting', [['ink', 'a', 'a'], 6], ['ink', 'a a']), ('regression: interword space in counting', [['kerning', 'baseline', 'a', 'serif'], 27], ['kerning baseline a serif']), ('regression: interword space in counting', [['baseline', 'is'], 11], ['baseline is']), ('regression: interword space in counting', [['of', 'a', 'kerning', 'of', 'kerning', 'of', 'type', 'the', 'x'], 21], ['of a kerning of', 'kerning of type the x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['a', 'serif', 'serif', 'baseline', 'grid', 'measure'], 11], ['a serif', 'serif', 'baseline', 'grid', 'measure']), ('control layout', [['x', 'baseline', 'a', 'measure', 'is'], 5], ['overflow'])], [('regression: interword space in counting', [['at', 'kerning', 'at', 'measure'], 29], ['at kerning at measure']), ('regression: interword space in counting', [['of', 'is', 'the', 'on', 'kerning'], 17], ['of is the', 'on kerning']), ('regression: interword space in counting', [['the', 'x', 'type', 'serif', 'grid', 'is', 'a'], 17], ['the x type', 'serif grid is a']), ('regression: interword space in counting', [['baseline', 'measure', 'measure'], 15], ['baseline', 'measure measure']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['kerning', 'on', 'the'], 9], ['kerning', 'on the']), ('control layout', [['x', 'baseline', 'is', 'ink', 'type', 'measure', 'on', 'a', 'the'], 8], ['x', 'baseline', 'is ink', 'type', 'measure', 'on a the'])], [('regression: interword space in counting', [['grid', 'on', 'baseline', 'x', 'x', 'grid', 'x', 'a', 'is'], 30], ['grid on baseline', 'x x grid x a is']), ('regression: interword space in counting', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: interword space in counting', [['the', 'at', 'of', 'on', 'on', 'serif'], 17], ['the at of', 'on on serif']), ('regression: interword space in counting', [['kerning', 'kerning', 'ink', 'ink', 'type', 'baseline'], 15], ['kerning kerning', 'ink ink type', 'baseline']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['grid', 'serif', 'type'], 6], ['grid', 'serif', 'type']), ('control layout', [['kerning', 'measure', 'type', 'on', 'measure', 'x', 'baseline'], 6], ['overflow'])]]
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:
Balanced widths are too narrow and lines overflow when laid out.

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= width:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: final layout width', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of grid']), ('regression: final layout width', [['grid', 'measure', 'baseline', 'grid', 'kerning', 'type', 'type', 'type', 'of'], 24], ['grid measure baseline', 'grid kerning type', 'type type of']), ('regression: final layout width', [['at', 'is', 'baseline', 'on', 'a', 'of', 'measure'], 25], ['at is baseline', 'on a of measure']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['type', 'a', 'the', 'measure', 'x'], 5], ['overflow']), ('control layout', [['is', 'x', 'at', 'baseline', 'type', 'ink'], 30], ['is x at baseline type ink']), ('control layout', [['kerning', 'is', 'x', 'the', 'is', 'type', 'a', 'the'], 16], ['kerning is x the', 'is type a the'])], [('regression: final layout width', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: final layout width', [['grid', 'at', 'a', 'measure', 'a', 'at', 'on', 'the'], 27], ['grid at a measure', 'a at on the']), ('regression: final layout width', [['kerning', 'a', 'is', 'the'], 10], ['kerning', 'a is the']), ('regression: final layout width', [['of', 'ink', 'is', 'at', 'a', 'the', 'on', 'ink'], 20], ['of ink is at', 'a the on ink']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'at', 'a', 'baseline', 'is'], 20], ['x at a baseline is']), ('control layout', [['kerning', 'of', 'baseline', 'serif', 'baseline', 'at', 'measure', 'of'], 16], ['kerning of', 'baseline serif', 'baseline at', 'measure of'])], [('regression: final layout width', [['on', 'measure', 'grid', 'a', 'x', 'is', 'kerning', 'kerning'], 27], ['on measure grid a x', 'is kerning kerning']), ('regression: final layout width', [['serif', 'of', 'is', 'on', 'of', 'ink', 'the'], 12], ['serif of', 'is on of', 'ink the']), ('regression: final layout width', [['measure', 'measure', 'the', 'baseline', 'kerning', 'at', 'serif'], 22], ['measure measure', 'the baseline', 'kerning at serif']), ('regression: final layout width', [['on', 'is', 'of', 'serif', 'grid', 'grid', 'on', 'grid'], 30], ['on is of serif', 'grid grid on grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'the', 'type', 'serif', 'x', 'type', 'is', 'a'], 18], ['baseline the type', 'serif x type is a']), ('control layout', [['a', 'serif', 'ink', 'measure', 'a', 'at'], 11], ['a serif ink', 'measure a', 'at'])], [('regression: final layout width', [['on', 'kerning', 'a', 'serif', 'at', 'at'], 18], ['on kerning a', 'serif at at']), ('regression: final layout width', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: final layout width', [['type', 'type', 'the', 'ink', 'of', 'baseline', 'measure'], 29], ['type type the ink', 'of baseline measure']), ('regression: final layout width', [['on', 'of', 'baseline', 'x', 'kerning', 'x', 'type', 'the', 'the'], 29], ['on of baseline x', 'kerning x type the the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'serif', 'ink', 'x', 'on', 'the', 'on'], 27], ['x serif ink x on the on']), ('control layout', [['at', 'is', 'serif', 'a', 'kerning', 'serif', 'baseline'], 26], ['at is serif a kerning', 'serif baseline'])], [('regression: final layout width', [['grid', 'kerning', 'ink', 'type', 'grid', 'serif', 'baseline'], 28], ['grid kerning ink type', 'grid serif baseline']), ('regression: final layout width', [['is', 'kerning', 'type', 'x', 'of', 'at', 'baseline', 'grid'], 21], ['is kerning type x', 'of at baseline grid']), ('regression: final layout width', [['baseline', 'a', 'of', 'is', 'kerning', 'ink'], 27], ['baseline a of', 'is kerning ink']), ('regression: final layout width', [['a', 'of', 'at', 'x', 'is', 'of'], 13], ['a of at', 'x is of']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'grid', 'baseline', 'of'], 12], ['measure grid', 'baseline of']), ('control layout', [['grid', 'on', 'is', 'kerning', 'a'], 7], ['grid on', 'is', 'kerning', 'a'])]]
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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= (lo + width) // 2:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: final layout width', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of grid']), ('regression: final layout width', [['grid', 'measure', 'baseline', 'grid', 'kerning', 'type', 'type', 'type', 'of'], 24], ['grid measure baseline', 'grid kerning type', 'type type of']), ('regression: final layout width', [['at', 'is', 'baseline', 'on', 'a', 'of', 'measure'], 25], ['at is baseline', 'on a of measure']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['type', 'a', 'the', 'measure', 'x'], 5], ['overflow']), ('control layout', [['is', 'x', 'at', 'baseline', 'type', 'ink'], 30], ['is x at baseline type ink']), ('control layout', [['kerning', 'is', 'x', 'the', 'is', 'type', 'a', 'the'], 16], ['kerning is x the', 'is type a the'])], [('regression: final layout width', [['at', 'grid', 'measure', 'a', 'x', 'is', 'baseline', 'is'], 10], ['at grid', 'measure', 'a x is', 'baseline', 'is']), ('regression: final layout width', [['grid', 'at', 'a', 'measure', 'a', 'at', 'on', 'the'], 27], ['grid at a measure', 'a at on the']), ('regression: final layout width', [['kerning', 'a', 'is', 'the'], 10], ['kerning', 'a is the']), ('regression: final layout width', [['of', 'ink', 'is', 'at', 'a', 'the', 'on', 'ink'], 20], ['of ink is at', 'a the on ink']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'at', 'a', 'baseline', 'is'], 20], ['x at a baseline is']), ('control layout', [['kerning', 'of', 'baseline', 'serif', 'baseline', 'at', 'measure', 'of'], 16], ['kerning of', 'baseline serif', 'baseline at', 'measure of'])], [('regression: final layout width', [['on', 'measure', 'grid', 'a', 'x', 'is', 'kerning', 'kerning'], 27], ['on measure grid a x', 'is kerning kerning']), ('regression: final layout width', [['serif', 'of', 'is', 'on', 'of', 'ink', 'the'], 12], ['serif of', 'is on of', 'ink the']), ('regression: final layout width', [['measure', 'measure', 'the', 'baseline', 'kerning', 'at', 'serif'], 22], ['measure measure', 'the baseline', 'kerning at serif']), ('regression: final layout width', [['on', 'is', 'of', 'serif', 'grid', 'grid', 'on', 'grid'], 30], ['on is of serif', 'grid grid on grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['baseline', 'the', 'type', 'serif', 'x', 'type', 'is', 'a'], 18], ['baseline the type', 'serif x type is a']), ('control layout', [['a', 'serif', 'ink', 'measure', 'a', 'at'], 11], ['a serif ink', 'measure a', 'at'])], [('regression: final layout width', [['on', 'kerning', 'a', 'serif', 'at', 'at'], 18], ['on kerning a', 'serif at at']), ('regression: final layout width', [['grid', 'serif', 'serif', 'grid', 'x', 'of'], 7], ['grid', 'serif', 'serif', 'grid', 'x of']), ('regression: final layout width', [['type', 'type', 'the', 'ink', 'of', 'baseline', 'measure'], 29], ['type type the ink', 'of baseline measure']), ('regression: final layout width', [['on', 'of', 'baseline', 'x', 'kerning', 'x', 'type', 'the', 'the'], 29], ['on of baseline x', 'kerning x type the the']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'serif', 'ink', 'x', 'on', 'the', 'on'], 27], ['x serif ink x on the on']), ('control layout', [['at', 'is', 'serif', 'a', 'kerning', 'serif', 'baseline'], 26], ['at is serif a kerning', 'serif baseline'])], [('regression: final layout width', [['grid', 'kerning', 'ink', 'type', 'grid', 'serif', 'baseline'], 28], ['grid kerning ink type', 'grid serif baseline']), ('regression: final layout width', [['is', 'kerning', 'type', 'x', 'of', 'at', 'baseline', 'grid'], 21], ['is kerning type x', 'of at baseline grid']), ('regression: final layout width', [['baseline', 'a', 'of', 'is', 'kerning', 'ink'], 27], ['baseline a of', 'is kerning ink']), ('regression: final layout width', [['a', 'of', 'at', 'x', 'is', 'of'], 13], ['a of at', 'x is of']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['measure', 'grid', 'baseline', 'of'], 12], ['measure grid', 'baseline of']), ('control layout', [['grid', 'on', 'is', 'kerning', 'a'], 7], ['grid on', 'is', 'kerning', 'a'])]]
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 balanced width is computed but lines are laid out at the full width.

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

split: open-access


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

N = 1
observations = []
def solve(x):
    words, width = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo + 1 < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: search termination', [['type', 'on', 'the', 'is', 'ink', 'of', 'grid'], 21], ['type on the is', 'ink of grid']), ('regression: search termination', [['the', 'at', 'grid', 'baseline', 'serif'], 29], ['the at grid baseline serif']), ('regression: search termination', [['at', 'x', 'ink', 'at', 'the', 'is'], 5], ['at x', 'ink', 'at', 'the', 'is']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['at', 'serif', 'at', 'measure', 'measure', 'grid'], 21], ['at serif at measure', 'measure grid']), ('control layout', [['at', 'is', 'the', 'ink', 'x', 'measure', 'grid', 'at', 'grid'], 18], ['at is the ink', 'x measure', 'grid at grid']), ('control layout', [['kerning', 'x', 'ink', 'the', 'on', 'grid', 'x', 'grid'], 8], ['kerning', 'x ink', 'the on', 'grid x', 'grid'])], [('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: search termination', [['is', 'on', 'of', 'of', 'serif', 'baseline', 'kerning'], 23], ['is on of of serif', 'baseline kerning']), ('partial-repair probe', [['x', 'x'], 11], ['x x']), ('partial-repair probe', [['serif', 'ink', 'x', 'ink', 'kerning', 'a'], 9], ['serif ink', 'x ink', 'kerning a']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'a', 'on'], 11], ['the a on']), ('control layout', [['x', 'x', 'the', 'on', 'of'], 15], ['x x the on of']), ('control layout', [['grid', 'the', 'at'], 16], ['grid the at'])], [('regression: search termination', [['baseline', 'is', 'the', 'grid', 'on'], 29], ['baseline is the grid on']), ('regression: search termination', [['of', 'x', 'at', 'grid', 'measure', 'at', 'the', 'at', 'the'], 10], ['of x at', 'grid', 'measure at', 'the at the']), ('partial-repair probe', [['on', 'a', 'measure'], 18], ['on a measure']), ('partial-repair probe', [['on', 'grid', 'a', 'at', 'ink'], 16], ['on grid a at ink']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'a'], 26], ['x a']), ('control layout', [['of', 'x', 'grid', 'baseline', 'kerning'], 22], ['of x grid', 'baseline kerning'])], [('regression: search termination', [['ink', 'grid', 'ink', 'the', 'grid', 'ink', 'is'], 8], ['ink grid', 'ink the', 'grid ink', 'is']), ('regression: search termination', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: search termination', [['type', 'kerning', 'grid', 'of', 'a'], 20], ['type kerning', 'grid of a']), ('partial-repair probe', [['at', 'at', 'at', 'grid'], 29], ['at at at grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'x', 'the', 'on', 'of'], 15], ['x x the on of']), ('control layout', [['type', 'ink', 'measure', 'the', 'x', 'the'], 23], ['type ink measure', 'the x the'])], [('regression: search termination', [['ink', 'baseline', 'the', 'kerning', 'of', 'kerning'], 18], ['ink baseline the', 'kerning of kerning']), ('regression: search termination', [['of', 'measure', 'x', 'at', 'a', 'on', 'serif', 'on', 'serif'], 20], ['of measure x at a', 'on serif on serif']), ('partial-repair probe', [['kerning', 'measure', 'on', 'a', 'on', 'of', 'of', 'serif', 'type'], 17], ['kerning measure', 'on a on of of', 'serif type']), ('regression: search termination', [['of', 'the', 'ink', 'serif'], 15], ['of the', 'ink serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'kerning', 'serif', 'is', 'baseline', 'baseline', 'on', 'grid'], 17], ['x kerning serif', 'is baseline', 'baseline on grid']), ('control layout', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of 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 = x
    def lines_for(w):
        count, cur = 0, -1
        for t in words:
            if len(t) > w:
                return None
            if cur == -1:
                cur = len(t)
                count = 1
            elif cur + 1 + len(t) <= w:
                cur += 1 + len(t)
            else:
                count += 1
                cur = len(t)
        return count
    target = lines_for(width)
    if target is None:
        return ['overflow']
    lo = max(len(t) for t in words)
    hi = width
    while lo + 2 < hi:
        mid = (lo + hi) // 2
        c = lines_for(mid)
        if c is not None and c <= target:
            hi = mid
        else:
            lo = mid + 1
    out, cur = [], ''
    for t in words:
        cand = t if not cur else cur + ' ' + t
        if len(cand) <= lo:
            cur = cand
        else:
            out.append(cur)
            cur = t
    out.append(cur)
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: search termination', [['type', 'on', 'the', 'is', 'ink', 'of', 'grid'], 21], ['type on the is', 'ink of grid']), ('regression: search termination', [['the', 'at', 'grid', 'baseline', 'serif'], 29], ['the at grid baseline serif']), ('regression: search termination', [['at', 'x', 'ink', 'at', 'the', 'is'], 5], ['at x', 'ink', 'at', 'the', 'is']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['at', 'serif', 'at', 'measure', 'measure', 'grid'], 21], ['at serif at measure', 'measure grid']), ('control layout', [['at', 'is', 'the', 'ink', 'x', 'measure', 'grid', 'at', 'grid'], 18], ['at is the ink', 'x measure', 'grid at grid']), ('control layout', [['kerning', 'x', 'ink', 'the', 'on', 'grid', 'x', 'grid'], 8], ['kerning', 'x ink', 'the on', 'grid x', 'grid'])], [('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('regression: search termination', [['is', 'on', 'of', 'of', 'serif', 'baseline', 'kerning'], 23], ['is on of of serif', 'baseline kerning']), ('partial-repair probe', [['x', 'x'], 11], ['x x']), ('partial-repair probe', [['serif', 'ink', 'x', 'ink', 'kerning', 'a'], 9], ['serif ink', 'x ink', 'kerning a']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['the', 'a', 'on'], 11], ['the a on']), ('control layout', [['x', 'x', 'the', 'on', 'of'], 15], ['x x the on of']), ('control layout', [['grid', 'the', 'at'], 16], ['grid the at'])], [('regression: search termination', [['baseline', 'is', 'the', 'grid', 'on'], 29], ['baseline is the grid on']), ('regression: search termination', [['of', 'x', 'at', 'grid', 'measure', 'at', 'the', 'at', 'the'], 10], ['of x at', 'grid', 'measure at', 'the at the']), ('partial-repair probe', [['on', 'a', 'measure'], 18], ['on a measure']), ('partial-repair probe', [['on', 'grid', 'a', 'at', 'ink'], 16], ['on grid a at ink']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('control layout', [['x', 'a'], 26], ['x a']), ('control layout', [['of', 'x', 'grid', 'baseline', 'kerning'], 22], ['of x grid', 'baseline kerning'])], [('regression: search termination', [['ink', 'grid', 'ink', 'the', 'grid', 'ink', 'is'], 8], ['ink grid', 'ink the', 'grid ink', 'is']), ('regression: search termination', [['is', 'grid', 'type', 'ink', 'x', 'grid', 'grid'], 16], ['is grid type', 'ink x grid grid']), ('regression: search termination', [['type', 'kerning', 'grid', 'of', 'a'], 20], ['type kerning', 'grid of a']), ('partial-repair probe', [['at', 'at', 'at', 'grid'], 29], ['at at at grid']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'x', 'the', 'on', 'of'], 15], ['x x the on of']), ('control layout', [['type', 'ink', 'measure', 'the', 'x', 'the'], 23], ['type ink measure', 'the x the'])], [('regression: search termination', [['ink', 'baseline', 'the', 'kerning', 'of', 'kerning'], 18], ['ink baseline the', 'kerning of kerning']), ('regression: search termination', [['of', 'measure', 'x', 'at', 'a', 'on', 'serif', 'on', 'serif'], 20], ['of measure x at a', 'on serif on serif']), ('partial-repair probe', [['kerning', 'measure', 'on', 'a', 'on', 'of', 'of', 'serif', 'type'], 17], ['kerning measure', 'on a on of of', 'serif type']), ('regression: search termination', [['of', 'the', 'ink', 'serif'], 15], ['of the', 'ink serif']), ('word exactly the width', [['baseline', 'x'], 8], ['baseline', 'x']), ('two balanced lines', [['the', 'type', 'grid', 'is', 'on', 'serif'], 22], ['the type grid', 'is on serif']), ('control layout', [['x', 'kerning', 'serif', 'is', 'baseline', 'baseline', 'on', 'grid'], 17], ['x kerning serif', 'is baseline', 'baseline on grid']), ('control layout', [['x', 'kerning', 'of', 'grid'], 14], ['x kerning', 'of 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:
Balanced widths come out one column too wide.

Input [words, width]. Let L be the greedy line count at width (["overflow"] if a word is wider). Find the smallest width w (>= longest word) whose greedy wrap needs no more than L lines, then return the greedy wrap at w.

split: open-access


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

N = 1
observations = []
def solve(x):
    url, width = x
    n = len(url)
    ok = set()
    for p in range(1, n):
        a, b = url[p - 1], url[p]
        if a == ':':
            ok.add(p)
        elif a == '/' and url[p - 2:p] == '//':
            ok.add(p)
        elif b == '/':
            ok.add(p)
        elif b in '.-_?#%~':
            ok.add(p)
        elif a in '=&' or b in '=&':
            ok.add(p)
    lines = []
    start = 0
    while n - start > width:
        cands = [p for p in ok if start < p <= start + width]
        cut = max(cands) if cands else start + width
        lines.append(url[start:cut])
        start = cut
    lines.append(url[start:])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: double slash integrity', ['https://x.io/search?term=line%20break&lang=en', 7], ['https:', '//x.io', '/search', '?term=', 'line', '%20brea', 'k&lang=', 'en']), ('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://w.org/a=b/c&d', 5], ['https', '://w', '.org', '/a=b', '/c&d']), ('control layout', ['file:///usr/local/share/doc', 14], ['file:///usr', '/local/share', '/doc'])], [('regression: double slash integrity', ['https://www.docs.io/a/b/c#sec-3', 7], ['https:', '//www', '.docs', '.io/a/b', '/c#sec', '-3']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 6], ['http:', '//a.b', '.org', '/x//y', '?q=1&r', '=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://www.docs.io/a/b/c#sec-3', 4], ['http', 's://', 'www', '.doc', 's.io', '/a/b', '/c', '#sec', '-3']), ('control layout', ['https://t.co/AbCdEf', 5], ['https', '://t', '.co', '/AbCd', 'Ef'])], [('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 6], ['http:', '//a.b', '.org', '/x//y', '?q=1&r', '=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['file:///usr/local/share/doc', 7], ['file://', '/usr', '/local', '/share', '/doc']), ('control layout', ['https://www.docs.io/a/b/c#sec-3', 14], ['https://www', '.docs.io/a/b/c', '#sec-3'])], [('regression: double slash integrity', ['ftp://files.site.net/pub/file-name_v2.tar.gz', 5], ['ftp:', '//', 'files', '.site', '.net', '/pub', '/file', '-name', '_v2', '.tar', '.gz']), ('regression: double slash integrity', ['https://t.co/AbCdEf', 7], ['https:', '//t.co', '/AbCdEf']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://w.org/a=b/c&d', 16], ['https://w.org/a=', 'b/c&d']), ('control layout', ['https://example.com/path/to/page.html', 11], ['https://', 'example.com', '/path/to', '/page.html'])], [('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['file:///usr/local/share/doc', 6], ['file:', '///usr', '/local', '/share', '/doc']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 16], ['http://a.b.org', '/x//y?q=1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['mailto:someone@example.com', 15], ['mailto:', 'someone@example', '.com']), ('control layout', ['file:///usr/local/share/doc', 12], ['file:///usr', '/local/share', '/doc'])]]
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):
    url, width = x
    n = len(url)
    ok = set()
    for p in range(1, n):
        a, b = url[p - 1], url[p]
        if a == ':':
            ok.add(p)
        elif a == '/' and url[p - 2:p] == '//':
            ok.add(p)
        elif b == '/' and a != '/':
            ok.add(p)
        elif b in '.-_?#%~':
            ok.add(p)
        elif a in '=&' or b in '=&':
            ok.add(p)
    lines = []
    start = 0
    while n - start > width:
        cands = [p for p in ok if start < p <= start + width]
        cut = max(cands) if cands else start + width
        lines.append(url[start:cut])
        start = cut
    lines.append(url[start:])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression: double slash integrity', ['https://x.io/search?term=line%20break&lang=en', 7], ['https:', '//x.io', '/search', '?term=', 'line', '%20brea', 'k&lang=', 'en']), ('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://w.org/a=b/c&d', 5], ['https', '://w', '.org', '/a=b', '/c&d']), ('control layout', ['file:///usr/local/share/doc', 14], ['file:///usr', '/local/share', '/doc'])], [('regression: double slash integrity', ['https://www.docs.io/a/b/c#sec-3', 7], ['https:', '//www', '.docs', '.io/a/b', '/c#sec', '-3']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 6], ['http:', '//a.b', '.org', '/x//y', '?q=1&r', '=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://www.docs.io/a/b/c#sec-3', 4], ['http', 's://', 'www', '.doc', 's.io', '/a/b', '/c', '#sec', '-3']), ('control layout', ['https://t.co/AbCdEf', 5], ['https', '://t', '.co', '/AbCd', 'Ef'])], [('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 6], ['http:', '//a.b', '.org', '/x//y', '?q=1&r', '=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['file:///usr/local/share/doc', 7], ['file://', '/usr', '/local', '/share', '/doc']), ('control layout', ['https://www.docs.io/a/b/c#sec-3', 14], ['https://www', '.docs.io/a/b/c', '#sec-3'])], [('regression: double slash integrity', ['ftp://files.site.net/pub/file-name_v2.tar.gz', 5], ['ftp:', '//', 'files', '.site', '.net', '/pub', '/file', '-name', '_v2', '.tar', '.gz']), ('regression: double slash integrity', ['https://t.co/AbCdEf', 7], ['https:', '//t.co', '/AbCdEf']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 9], ['http://a', '.b.org', '/x//y?q=1', '&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['https://w.org/a=b/c&d', 16], ['https://w.org/a=', 'b/c&d']), ('control layout', ['https://example.com/path/to/page.html', 11], ['https://', 'example.com', '/path/to', '/page.html'])], [('regression: double slash integrity', ['https://example.com/path/to/page.html', 7], ['https:', '//', 'example', '.com', '/path', '/to', '/page', '.html']), ('regression: double slash integrity', ['file:///usr/local/share/doc', 6], ['file:', '///usr', '/local', '/share', '/doc']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 8], ['http://a', '.b.org', '/x//y?q=', '1&r=2']), ('regression: double slash integrity', ['http://a.b.org/x//y?q=1&r=2', 16], ['http://a.b.org', '/x//y?q=1&r=2']), ('double slash inside path', ['http://a.b.org/x//y?q=1&r=2', 10], ['http://a.b', '.org/x//y', '?q=1&r=2']), ('break after scheme', ['https://example.com/path/to/page.html', 8], ['https://', 'example', '.com', '/path/to', '/page', '.html']), ('control layout', ['mailto:someone@example.com', 15], ['mailto:', 'someone@example', '.com']), ('control layout', ['file:///usr/local/share/doc', 12], ['file:///usr', '/local/share', '/doc'])]]
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:
URLs are broken between the two slashes of "//".

Input [url, width]. Break opportunities (no hyphen added): after ":"; after "//" (never between its slashes); before a single "/"; before . - _ ? # % ~; before or after = and &. While the rest exceeds the width, break at the latest opportunity within width characters, else hard-break at width. Return lines.

split: open-access