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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr // len(pieces) for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[1] // max(1, sum(pp[1] for pp in pieces)) for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Every drawn piece carries the same freight regardless of how many units it holds.

Layers are [qty, total value]. A transfer of qty draws FIFO from src; a partial draw takes floor(t*value/qty) and leaves the residue in src; a layer is removed when its quantity reaches zero. Freight is split across the drawn pieces by quantity (floor), residual cents to the first piece, and each piece is appended to dst in draw order as the newest layers. qty<=0 or qty above src stock returns {"error"}. Return {src, dst}.

split: open-access


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):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[-1] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A few cents of transfer freight disappear from inventory on every transfer.

Layers are [qty, total value]. A transfer of qty draws FIFO from src; a partial draw takes floor(t*value/qty) and leaves the residue in src; a layer is removed when its quantity reaches zero. Freight is split across the drawn pieces by quantity (floor), residual cents to the first piece, and each piece is appended to dst in draw order as the newest layers. qty<=0 or qty above src stock returns {"error"}. Return {src, dst}.

split: open-access


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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.insert(0, [p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in list(zip(pieces, shares))[::-1]:
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Transferred stock becomes the oldest stock at the destination and ships before local receipts.

Layers are [qty, total value]. A transfer of qty draws FIFO from src; a partial draw takes floor(t*value/qty) and leaves the residue in src; a layer is removed when its quantity reaches zero. Freight is split across the drawn pieces by quantity (floor), residual cents to the first piece, and each piece is appended to dst in draw order as the newest layers. qty<=0 or qty above src stock returns {"error"}. Return {src, dst}.

split: open-access


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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][1] == 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    src = [list(l) for l in x['src']]
    dst = [list(l) for l in x['dst']]
    q = x['qty']
    if q <= 0 or q > sum(l[0] for l in src):
        return {'error': 'invalid quantity'}
    pieces = []
    while q > 0:
        lq, lv = src[0]
        t = min(q, lq)
        val = lv if t == lq else t * lv // lq
        pieces.append([t, val])
        src[0] = [lq - t, lv - val]
        if src[0][0] <= 0 or src[0][1] <= 0:
            src.pop(0)
        q -= t
    fr = x['freight']
    tq = x['qty']
    shares = [fr * p[0] // tq for p in pieces]
    shares[0] += fr - sum(shares)
    for p, s in zip(pieces, shares):
        dst.append([p[0], p[1] + s])
    return {'src': src, 'dst': dst}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 11}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 78]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 1}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 75]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 8}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 8]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 14}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 62], [3, 188]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 4, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 12]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 12]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 12}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 79]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 2}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 76]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 9}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 9]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 15}, {'src': [[2, 120]], 'dst': [[1, 53], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 5, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 13]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 13]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 13}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 80]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 3}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 77]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 10}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 10]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 16}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 189]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 6, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 14]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 14]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 5, 'freight': 101}, {'src': [[3, 156]], 'dst': [[2, 90], [3, 161], [2, 144]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 14}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 81]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 4}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 78]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 11}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 11]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 17}, {'src': [[2, 120]], 'dst': [[1, 54], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 7, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 15]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 15]]}]], [['span layers', {'src': [[3, 100], [5, 260]], 'dst': [[2, 90]], 'qty': 6, 'freight': 101}, {'src': [[2, 104]], 'dst': [[2, 90], [3, 151], [3, 206]]}], ['partial residue', {'src': [[3, 101], [4, 77]], 'dst': [], 'qty': 2, 'freight': 15}, {'src': [[1, 34], [4, 77]], 'dst': [[2, 82]]}], ['odd layer value', {'src': [[4, 99], [1, 5]], 'dst': [], 'qty': 3, 'freight': 5}, {'src': [[1, 25], [1, 5]], 'dst': [[3, 79]]}], ['free goods layer', {'src': [[5, 0], [2, 40]], 'dst': [[1, 10]], 'qty': 2, 'freight': 12}, {'src': [[3, 0], [2, 40]], 'dst': [[1, 10], [2, 12]]}], ['uneven freight', {'src': [[1, 50], [1, 60], [5, 300]], 'dst': [], 'qty': 5, 'freight': 18}, {'src': [[2, 120]], 'dst': [[1, 55], [1, 63], [3, 190]]}], ['insufficient', {'src': [[2, 20]], 'dst': [], 'qty': 8, 'freight': 5}, {'error': 'invalid quantity'}], ['nothing requested', {'src': [[2, 20]], 'dst': [], 'qty': 0, 'freight': 5}, {'error': 'invalid quantity'}], ['exact all', {'src': [[2, 21], [1, 16]], 'dst': [[4, 44]], 'qty': 3, 'freight': 0}, {'src': [], 'dst': [[4, 44], [2, 21], [1, 16]]}]]]
for label, args, expected in fixtures[N-1]:
    check(label, solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
A zero-cost free-goods layer is dropped from the source after a partial draw, losing units.

Layers are [qty, total value]. A transfer of qty draws FIFO from src; a partial draw takes floor(t*value/qty) and leaves the residue in src; a layer is removed when its quantity reaches zero. Freight is split across the drawn pieces by quantity (floor), residual cents to the first piece, and each piece is appended to dst in draw order as the newest layers. qty<=0 or qty above src stock returns {"error"}. Return {src, dst}.

split: open-access


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

N = 1
observations = []
def solve(x):
    prior = x['begin'][0] - x['begin'][1]
    rows = []
    for (fe, le), cogs in zip(x['years'], x['lifo_cogs']):
        reserve = le - fe
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - change,
                     'flag': 'decline' if change < 0 else 'build',
                     'deferral': (reserve * x['tax_bp'] + 5000) // 10000})
        prior = reserve
    return rows
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4701], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1299, 'change': 499, 'fifo_cogs': 19501, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 301, 'fifo_cogs': 20699, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4499]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1501, 'change': -699, 'fifo_cogs': 31699, 'flag': 'decline', 'deferral': 375}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3601, 2601]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[901, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 601, 'change': 601, 'fifo_cogs': 4399, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1011, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 10, 'change': 7, 'fifo_cogs': 793, 'flag': 'build', 'deferral': 3}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1001]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -101, 'change': -1, 'fifo_cogs': 51, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1651], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 849, 'change': -51, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 351, 'fifo_cogs': -348, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4702], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1298, 'change': 498, 'fifo_cogs': 19502, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 302, 'fifo_cogs': 20698, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4498]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1502, 'change': -698, 'fifo_cogs': 31698, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3602, 2602]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[902, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 602, 'change': 602, 'fifo_cogs': 4398, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1012, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 11, 'change': 8, 'fifo_cogs': 792, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1002]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -102, 'change': -2, 'fifo_cogs': 52, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1652], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 848, 'change': -52, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 352, 'fifo_cogs': -349, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4703], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1297, 'change': 497, 'fifo_cogs': 19503, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 303, 'fifo_cogs': 20697, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4497]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1503, 'change': -697, 'fifo_cogs': 31697, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3603, 2603]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[903, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 603, 'change': 603, 'fifo_cogs': 4397, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1013, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 12, 'change': 9, 'fifo_cogs': 791, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1003]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -103, 'change': -3, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1653], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 847, 'change': -53, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 353, 'fifo_cogs': -350, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4704], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1296, 'change': 496, 'fifo_cogs': 19504, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 304, 'fifo_cogs': 20696, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4496]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1504, 'change': -696, 'fifo_cogs': 31696, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3604, 2604]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[904, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 604, 'change': 604, 'fifo_cogs': 4396, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1014, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 13, 'change': 10, 'fifo_cogs': 790, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1004]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -104, 'change': -4, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1654], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 846, 'change': -54, 'fifo_cogs': 56, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 354, 'fifo_cogs': -351, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4705], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1295, 'change': 495, 'fifo_cogs': 19505, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 305, 'fifo_cogs': 20695, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4495]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1505, 'change': -695, 'fifo_cogs': 31695, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3605, 2605]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[905, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 605, 'change': 605, 'fifo_cogs': 4395, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1015, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 14, 'change': 11, 'fifo_cogs': 789, 'flag': 'build', 'deferral': 5}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1005]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -105, 'change': -5, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1655], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 845, 'change': -55, 'fifo_cogs': 57, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 355, 'fifo_cogs': -352, 'flag': 'build', 'deferral': 240}]]]]
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):
    prior = x['begin'][0] - x['begin'][1]
    rows = []
    for (fe, le), cogs in zip(x['years'], x['lifo_cogs']):
        reserve = abs(le - fe) if fe >= le else le - fe
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - change,
                     'flag': 'decline' if change < 0 else 'build',
                     'deferral': (reserve * x['tax_bp'] + 5000) // 10000})
        prior = reserve
    return rows
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4701], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1299, 'change': 499, 'fifo_cogs': 19501, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 301, 'fifo_cogs': 20699, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4499]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1501, 'change': -699, 'fifo_cogs': 31699, 'flag': 'decline', 'deferral': 375}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3601, 2601]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[901, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 601, 'change': 601, 'fifo_cogs': 4399, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1011, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 10, 'change': 7, 'fifo_cogs': 793, 'flag': 'build', 'deferral': 3}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1001]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -101, 'change': -1, 'fifo_cogs': 51, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1651], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 849, 'change': -51, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 351, 'fifo_cogs': -348, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4702], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1298, 'change': 498, 'fifo_cogs': 19502, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 302, 'fifo_cogs': 20698, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4498]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1502, 'change': -698, 'fifo_cogs': 31698, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3602, 2602]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[902, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 602, 'change': 602, 'fifo_cogs': 4398, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1012, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 11, 'change': 8, 'fifo_cogs': 792, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1002]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -102, 'change': -2, 'fifo_cogs': 52, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1652], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 848, 'change': -52, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 352, 'fifo_cogs': -349, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4703], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1297, 'change': 497, 'fifo_cogs': 19503, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 303, 'fifo_cogs': 20697, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4497]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1503, 'change': -697, 'fifo_cogs': 31697, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3603, 2603]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[903, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 603, 'change': 603, 'fifo_cogs': 4397, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1013, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 12, 'change': 9, 'fifo_cogs': 791, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1003]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -103, 'change': -3, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1653], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 847, 'change': -53, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 353, 'fifo_cogs': -350, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4704], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1296, 'change': 496, 'fifo_cogs': 19504, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 304, 'fifo_cogs': 20696, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4496]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1504, 'change': -696, 'fifo_cogs': 31696, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3604, 2604]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[904, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 604, 'change': 604, 'fifo_cogs': 4396, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1014, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 13, 'change': 10, 'fifo_cogs': 790, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1004]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -104, 'change': -4, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1654], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 846, 'change': -54, 'fifo_cogs': 56, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 354, 'fifo_cogs': -351, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4705], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1295, 'change': 495, 'fifo_cogs': 19505, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 305, 'fifo_cogs': 20695, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4495]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1505, 'change': -695, 'fifo_cogs': 31695, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3605, 2605]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[905, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 605, 'change': 605, 'fifo_cogs': 4395, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1015, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 14, 'change': 11, 'fifo_cogs': 789, 'flag': 'build', 'deferral': 5}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1005]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -105, 'change': -5, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1655], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 845, 'change': -55, 'fifo_cogs': 57, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 355, 'fifo_cogs': -352, 'flag': 'build', 'deferral': 240}]]]]
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 LIFO reserve comes out negative in an inflationary period.

Input {begin:[fifo,lifo], years:[[fifo_end,lifo_end]], lifo_cogs:[per year], tax_bp}. Reserve = FIFO - LIFO. Each year: change = reserve_end - prior year reserve; FIFO-basis COGS = LIFO COGS - change; a year is flagged "decline" when change < 0; cumulative tax deferral = round-half-up(reserve_end*tax_bp/10000). Return list of {reserve, change, fifo_cogs, flag, deferral}.

split: open-access


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

N = 1
observations = []
def solve(x):
    prior = x['begin'][0] - x['begin'][1]
    rows = []
    for (fe, le), cogs in zip(x['years'], x['lifo_cogs']):
        reserve = fe - le
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - change,
                     'flag': 'decline' if change < 0 else 'build',
                     'deferral': (reserve * x['tax_bp'] + 5000) // 10000})
    return rows
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4701], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1299, 'change': 499, 'fifo_cogs': 19501, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 301, 'fifo_cogs': 20699, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4499]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1501, 'change': -699, 'fifo_cogs': 31699, 'flag': 'decline', 'deferral': 375}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3601, 2601]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[901, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 601, 'change': 601, 'fifo_cogs': 4399, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1011, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 10, 'change': 7, 'fifo_cogs': 793, 'flag': 'build', 'deferral': 3}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1001]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -101, 'change': -1, 'fifo_cogs': 51, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1651], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 849, 'change': -51, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 351, 'fifo_cogs': -348, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4702], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1298, 'change': 498, 'fifo_cogs': 19502, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 302, 'fifo_cogs': 20698, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4498]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1502, 'change': -698, 'fifo_cogs': 31698, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3602, 2602]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[902, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 602, 'change': 602, 'fifo_cogs': 4398, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1012, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 11, 'change': 8, 'fifo_cogs': 792, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1002]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -102, 'change': -2, 'fifo_cogs': 52, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1652], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 848, 'change': -52, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 352, 'fifo_cogs': -349, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4703], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1297, 'change': 497, 'fifo_cogs': 19503, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 303, 'fifo_cogs': 20697, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4497]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1503, 'change': -697, 'fifo_cogs': 31697, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3603, 2603]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[903, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 603, 'change': 603, 'fifo_cogs': 4397, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1013, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 12, 'change': 9, 'fifo_cogs': 791, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1003]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -103, 'change': -3, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1653], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 847, 'change': -53, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 353, 'fifo_cogs': -350, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4704], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1296, 'change': 496, 'fifo_cogs': 19504, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 304, 'fifo_cogs': 20696, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4496]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1504, 'change': -696, 'fifo_cogs': 31696, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3604, 2604]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[904, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 604, 'change': 604, 'fifo_cogs': 4396, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1014, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 13, 'change': 10, 'fifo_cogs': 790, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1004]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -104, 'change': -4, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1654], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 846, 'change': -54, 'fifo_cogs': 56, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 354, 'fifo_cogs': -351, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4705], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1295, 'change': 495, 'fifo_cogs': 19505, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 305, 'fifo_cogs': 20695, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4495]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1505, 'change': -695, 'fifo_cogs': 31695, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3605, 2605]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[905, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 605, 'change': 605, 'fifo_cogs': 4395, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1015, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 14, 'change': 11, 'fifo_cogs': 789, 'flag': 'build', 'deferral': 5}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1005]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -105, 'change': -5, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1655], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 845, 'change': -55, 'fifo_cogs': 57, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 355, 'fifo_cogs': -352, 'flag': 'build', 'deferral': 240}]]]]
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):
    prior = x['begin'][0] - x['begin'][1]
    rows = []
    for (fe, le), cogs in zip(x['years'], x['lifo_cogs']):
        reserve = fe - le
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - change,
                     'flag': 'decline' if change < 0 else 'build',
                     'deferral': (reserve * x['tax_bp'] + 5000) // 10000})
        if change > 0:
            prior = reserve
    return rows
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4701], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1299, 'change': 499, 'fifo_cogs': 19501, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 301, 'fifo_cogs': 20699, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4499]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1501, 'change': -699, 'fifo_cogs': 31699, 'flag': 'decline', 'deferral': 375}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3601, 2601]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[901, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 601, 'change': 601, 'fifo_cogs': 4399, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1011, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 10, 'change': 7, 'fifo_cogs': 793, 'flag': 'build', 'deferral': 3}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1001]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -101, 'change': -1, 'fifo_cogs': 51, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1651], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 849, 'change': -51, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 351, 'fifo_cogs': -348, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4702], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1298, 'change': 498, 'fifo_cogs': 19502, 'flag': 'build', 'deferral': 273}, {'reserve': 1600, 'change': 302, 'fifo_cogs': 20698, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4498]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1502, 'change': -698, 'fifo_cogs': 31698, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3602, 2602]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[902, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 602, 'change': 602, 'fifo_cogs': 4398, 'flag': 'build', 'deferral': 129}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1012, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 11, 'change': 8, 'fifo_cogs': 792, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1002]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -102, 'change': -2, 'fifo_cogs': 52, 'flag': 'decline', 'deferral': -20}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1652], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 848, 'change': -52, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': 170}, {'reserve': 1200, 'change': 352, 'fifo_cogs': -349, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4703], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1297, 'change': 497, 'fifo_cogs': 19503, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 303, 'fifo_cogs': 20697, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4497]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1503, 'change': -697, 'fifo_cogs': 31697, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3603, 2603]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[903, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 603, 'change': 603, 'fifo_cogs': 4397, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1013, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 12, 'change': 9, 'fifo_cogs': 791, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1003]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -103, 'change': -3, 'fifo_cogs': 53, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1653], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 847, 'change': -53, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 353, 'fifo_cogs': -350, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4704], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1296, 'change': 496, 'fifo_cogs': 19504, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 304, 'fifo_cogs': 20696, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4496]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1504, 'change': -696, 'fifo_cogs': 31696, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3604, 2604]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[904, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 604, 'change': 604, 'fifo_cogs': 4396, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1014, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 13, 'change': 10, 'fifo_cogs': 790, 'flag': 'build', 'deferral': 4}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1004]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -104, 'change': -4, 'fifo_cogs': 54, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1654], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 846, 'change': -54, 'fifo_cogs': 56, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 354, 'fifo_cogs': -351, 'flag': 'build', 'deferral': 240}]]], [['rising reserve', {'begin': [5000, 4200], 'years': [[6000, 4705], [6500, 4900]], 'lifo_cogs': [20000, 21000], 'tax_bp': 2100}, [{'reserve': 1295, 'change': 495, 'fifo_cogs': 19505, 'flag': 'build', 'deferral': 272}, {'reserve': 1600, 'change': 305, 'fifo_cogs': 20695, 'flag': 'build', 'deferral': 336}]], ['declining reserve', {'begin': [8000, 5000], 'years': [[7000, 4800], [6000, 4495]], 'lifo_cogs': [30000, 31000], 'tax_bp': 2500}, [{'reserve': 2200, 'change': -800, 'fifo_cogs': 30800, 'flag': 'decline', 'deferral': 550}, {'reserve': 1505, 'change': -695, 'fifo_cogs': 31695, 'flag': 'decline', 'deferral': 376}]], ['flat year', {'begin': [3000, 2000], 'years': [[3500, 2500], [3605, 2605]], 'lifo_cogs': [9000, 9100], 'tax_bp': 3000}, [{'reserve': 1000, 'change': 0, 'fifo_cogs': 9000, 'flag': 'build', 'deferral': 300}, {'reserve': 1000, 'change': 0, 'fifo_cogs': 9100, 'flag': 'build', 'deferral': 300}]], ['single year', {'begin': [100, 100], 'years': [[905, 300]], 'lifo_cogs': [5000], 'tax_bp': 2150}, [{'reserve': 605, 'change': 605, 'fifo_cogs': 4395, 'flag': 'build', 'deferral': 130}]], ['odd tax', {'begin': [1000, 999], 'years': [[1003, 1000], [1015, 1001]], 'lifo_cogs': [700, 800], 'tax_bp': 3333}, [{'reserve': 3, 'change': 2, 'fifo_cogs': 698, 'flag': 'build', 'deferral': 1}, {'reserve': 14, 'change': 11, 'fifo_cogs': 789, 'flag': 'build', 'deferral': 5}]], ['deflation', {'begin': [1000, 1100], 'years': [[900, 1005]], 'lifo_cogs': [50], 'tax_bp': 2000}, [{'reserve': -105, 'change': -5, 'fifo_cogs': 55, 'flag': 'decline', 'deferral': -21}]], ['three years', {'begin': [2000, 1500], 'years': [[2600, 1700], [2500, 1655], [2900, 1700]], 'lifo_cogs': [1, 2, 3], 'tax_bp': 2000}, [{'reserve': 900, 'change': 400, 'fifo_cogs': -399, 'flag': 'build', 'deferral': 180}, {'reserve': 845, 'change': -55, 'fifo_cogs': 57, 'flag': 'decline', 'deferral': 169}, {'reserve': 1200, 'change': 355, 'fifo_cogs': -352, 'flag': 'build', 'deferral': 240}]]]]
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:
Every year after the first reports the cumulative change since the opening balance.

Input {begin:[fifo,lifo], years:[[fifo_end,lifo_end]], lifo_cogs:[per year], tax_bp}. Reserve = FIFO - LIFO. Each year: change = reserve_end - prior year reserve; FIFO-basis COGS = LIFO COGS - change; a year is flagged "decline" when change < 0; cumulative tax deferral = round-half-up(reserve_end*tax_bp/10000). Return list of {reserve, change, fifo_cogs, flag, deferral}.

split: open-access