broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 0
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2) or 0) or 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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:
Single-line hunks are rejected as truncated or misread.

Parse "@@ -a[,b] +c[,d] @@" hunks (omitted counts are 1) and consume body lines until both remaining counts reach zero: " " (or a completely empty line) counts for both sides, "-" for old, "+" for new, "\\" marker lines are skipped and not counted, anything else ends the body. Remaining counts other than zero yield {"error": "hunk k truncated"}. Return [a, b, c, d, body_lines] per hunk.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                body += 1
                continue
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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:
Patches touching a last line without newline are reported as truncated.

Parse "@@ -a[,b] +c[,d] @@" hunks (omitted counts are 1) and consume body lines until both remaining counts reach zero: " " (or a completely empty line) counts for both sides, "-" for old, "+" for new, "\\" marker lines are skipped and not counted, anything else ends the body. Remaining counts other than zero yield {"error": "hunk k truncated"}. Return [a, b, c, d, body_lines] per hunk.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1]
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while (need_o > 0 or need_n > 0) and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1] if ln else '+'
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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:
Patches whose blank context lines lost their leading space are rejected as truncated.

Parse "@@ -a[,b] +c[,d] @@" hunks (omitted counts are 1) and consume body lines until both remaining counts reach zero: " " (or a completely empty line) counts for both sides, "-" for old, "+" for new, "\\" marker lines are skipped and not counted, anything else ends the body. Remaining counts other than zero yield {"error": "hunk k truncated"}. Return [a, b, c, d, body_lines] per hunk.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while need_o > 0 and need_n > 0 and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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
import re
N = 1
observations = []
def solve(lines):
    hdr = re.compile(r'^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@')
    hunks = []
    i = 0
    while i < len(lines):
        m = hdr.match(lines[i])
        if not m:
            i += 1
            continue
        os_ = int(m.group(1))
        ol = int(m.group(2)) if m.group(2) is not None else 1
        ns = int(m.group(3))
        nl = int(m.group(4)) if m.group(4) is not None else 1
        need_o, need_n, body = ol, nl, 0
        i += 1
        while need_o > 0 and i < len(lines):
            ln = lines[i]
            if ln.startswith('\\'):
                i += 1
                continue
            t = ln[:1] if ln else ' '
            if t == ' ':
                need_o -= 1
                need_n -= 1
            elif t == '-':
                need_o -= 1
            elif t == '+':
                need_n -= 1
            else:
                break
            body += 1
            i += 1
        if need_o != 0 or need_n != 0:
            return {'error': 'hunk %d truncated' % len(hunks)}
        hunks.append([os_, ol, ns, nl, body])
    return {'hunks': hunks}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,1 @@', '+n']], {'hunks': [[0, 0, 1, 1, 1]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,3 @@', ' a', '-b', '+x', '+x']], {'hunks': [[2, 2, 2, 3, 4]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    2: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,2 @@', '+n', '+n']], {'hunks': [[0, 0, 1, 2, 2]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,4 @@', ' a', '-b', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 4, 5]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    3: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,3 @@', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 3, 3]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,5 @@', ' a', '-b', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 5, 6]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    4: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,4 @@', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 4, 4]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,6 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 6, 7]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
    5: [('well-formed hunk', [['--- a/f', '+++ b/f', '@@ -1,3 +1,3 @@', ' a', '-b', '+B', ' c']], {'hunks': [[1, 3, 1, 3, 4]]}), ('omitted counts mean one', [['@@ -4 +4 @@', '-x', '+y']], {'hunks': [[4, 1, 4, 1, 2]]}), ('explicit zero count', [['@@ -0,0 +1,5 @@', '+n', '+n', '+n', '+n', '+n']], {'hunks': [[0, 0, 1, 5, 5]]}), ('no-newline marker is not a body line', [['@@ -1 +1 @@', '-old', '\\ No newline at end of file', '+new', '\\ No newline at end of file']], {'hunks': [[1, 1, 1, 1, 2]]}), ('empty line tolerated as context', [['@@ -1,3 +1,3 @@', ' a', '', ' c']], {'hunks': [[1, 3, 1, 3, 3]]}), ('truncated hunk', [['@@ -1,3 +1,4 @@', ' a', '+b', ' c']], {'error': 'hunk 0 truncated'}), ('trailing additions after old side is exhausted', [['@@ -2,2 +2,7 @@', ' a', '-b', '+x', '+x', '+x', '+x', '+x', '+x']], {'hunks': [[2, 2, 2, 7, 8]]}), ('two hunks', [['@@ -1,2 +1,1 @@', ' a', '-b', '@@ -9 +8,2 @@', ' z', '+w']], {'hunks': [[1, 2, 1, 1, 2], [9, 1, 8, 2, 2]]})],
}[N]
for label, args, expected in cases:
    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:
Hunks ending in additions or removals are reported as truncated.

Parse "@@ -a[,b] +c[,d] @@" hunks (omitted counts are 1) and consume body lines until both remaining counts reach zero: " " (or a completely empty line) counts for both sides, "-" for old, "+" for new, "\\" marker lines are skipped and not counted, anything else ends the body. Remaining counts other than zero yield {"error": "hunk k truncated"}. Return [a, b, c, d, body_lines] per hunk.

split: open-access


prompt:
A file replaced by a same-named directory is reported as a type change, and entries around directories are misordered.

Compare two tree listings of [name, mode, hash]. Entries are ordered by name, with directories (mode 040000) compared as name + "/". Walk both in that order: entries only in the old tree are "D", only in the new tree "A". Entries matched by sort key report "T" when the mode type (first two digits) differs, else "M" when the hash or mode differs. Return [status, name] rows.

split: open-access


prompt:
Toggling the executable bit shows up as "T" instead of a modification.

Compare two tree listings of [name, mode, hash]. Entries are ordered by name, with directories (mode 040000) compared as name + "/". Walk both in that order: entries only in the old tree are "D", only in the new tree "A". Entries matched by sort key report "T" when the mode type (first two digits) differs, else "M" when the hash or mode differs. Return [status, name] rows.

split: open-access


prompt:
Making a script executable produces no change entry.

Compare two tree listings of [name, mode, hash]. Entries are ordered by name, with directories (mode 040000) compared as name + "/". Walk both in that order: entries only in the old tree are "D", only in the new tree "A". Entries matched by sort key report "T" when the mode type (first two digits) differs, else "M" when the hash or mode differs. Return [status, name] rows.

split: open-access


prompt:
Files added or deleted at the end of the directory are missing from the diff.

Compare two tree listings of [name, mode, hash]. Entries are ordered by name, with directories (mode 040000) compared as name + "/". Walk both in that order: entries only in the old tree are "D", only in the new tree "A". Entries matched by sort key report "T" when the mode type (first two digits) differs, else "M" when the hash or mode differs. Return [status, name] rows.

split: open-access


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

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 1, -1, -1):
            ln = old[k]
            if ln[:1].isalpha() or ln[:1] in ('_', '$'):
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 3, -1, -1):
            ln = old[k]
            if ln[:1].isalpha() or ln[:1] in ('_', '$'):
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    check(label, solve(*args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A hunk that starts on a definition shows that definition instead of the enclosing one.

For each hunk whose old range starts at 1-based line s, search the old lines strictly before s, nearest first, for a line whose first character is a letter, "_" or "$". The heading is that line with trailing whitespace removed, truncated to limit characters; "" if none.

split: open-access


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

N = 1
observations = []
def solve(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln.strip()[:1].isalpha() or ln.strip()[:1] in ('_', '$'):
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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(old, starts, limit):
    res = []
    for s in starts:
        found = ''
        for k in range(s - 2, -1, -1):
            ln = old[k]
            if ln[:1].isalnum() or ln[:1] in ('_', '$'):
                found = ln.rstrip()[:limit]
                break
        res.append(found)
    return res
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
cases = {
    1: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 11], ['def xxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    2: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 12], ['def xxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    3: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 13], ['def xxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    4: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 14], ['def xxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
    5: [('nearest preceding definition', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [5], 40], ['int main(void)']), ('the hunk start line itself is not searched', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [2, 3], 40], ['', 'int main(void)']), ('indented lines are not headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [6], 40], ['int main(void)']), ('dollar and underscore headings', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [10, 12], 40], ['$macro', '_private:']), ('nothing before the first line', [['#include <x>', 'int main(void)', '{', '  int a;', '  helper();', '  return 0;', '}', '', '$macro', '  body', '_private:', '  x = 1'], [1], 40], ['']), ('long heading is truncated', [['def xxxxxxxxxxxxxxxxxxxxxxxxx(): ', '    pass', '    pass'], [3], 15], ['def xxxxxxxxxxx']), ('digits do not start a heading', [['label:', '42 items', 'y'], [3], 40], ['label:']), ('trailing whitespace trimmed', [['Section   ', 'text', 'more'], [3], 40], ['text'])],
}[N]
for label, args, expected in cases:
    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 header shows a statement inside the function instead of the function signature.

For each hunk whose old range starts at 1-based line s, search the old lines strictly before s, nearest first, for a line whose first character is a letter, "_" or "$". The heading is that line with trailing whitespace removed, truncated to limit characters; "" if none.

split: open-access