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})
        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 = fe - le
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - reserve,
                     '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:
Converting to FIFO basis makes COGS higher when the reserve grew.

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})
        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)

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):
    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 reserve < prior or reserve == 0 or 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:
A year with an unchanged reserve is flagged as a LIFO decline.

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': (change * 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 = fe - le
        change = reserve - prior
        rows.append({'reserve': reserve, 'change': change, 'fifo_cogs': cogs - change,
                     'flag': 'decline' if change < 0 else 'build',
                     'deferral': ((reserve + prior) * 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:
Cumulative tax deferral swings with each year change and can go negative.

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


prompt:
A layer exactly 30 days old is reported in the 31-60 bucket and reserved at the higher rate.

Input {layers:[[receipt_day,qty,unit]], today, buckets:[upper age bounds, inclusive], reserve_bp:[rate per bucket incl. overflow]}. Age = today - receipt_day; layers with negative age are in transit and excluded. A layer falls in the first bucket whose bound is >= age, else the overflow bucket. Reserve = sum over buckets of round-half-up(value*bp/10000). Return {values, reserve}.

split: open-access


prompt:
Stock older than the last bound is merged into the last bucket and reserved at its lower rate.

Input {layers:[[receipt_day,qty,unit]], today, buckets:[upper age bounds, inclusive], reserve_bp:[rate per bucket incl. overflow]}. Age = today - receipt_day; layers with negative age are in transit and excluded. A layer falls in the first bucket whose bound is >= age, else the overflow bucket. Reserve = sum over buckets of round-half-up(value*bp/10000). Return {values, reserve}.

split: open-access


prompt:
Future-dated receipts in transit are valued as fresh stock in the current bucket.

Input {layers:[[receipt_day,qty,unit]], today, buckets:[upper age bounds, inclusive], reserve_bp:[rate per bucket incl. overflow]}. Age = today - receipt_day; layers with negative age are in transit and excluded. A layer falls in the first bucket whose bound is >= age, else the overflow bucket. Reserve = sum over buckets of round-half-up(value*bp/10000). Return {values, reserve}.

split: open-access


prompt:
Every layer is treated as in transit and the aging report is empty.

Input {layers:[[receipt_day,qty,unit]], today, buckets:[upper age bounds, inclusive], reserve_bp:[rate per bucket incl. overflow]}. Age = today - receipt_day; layers with negative age are in transit and excluded. A layer falls in the first bucket whose bound is >= age, else the overflow bucket. Reserve = sum over buckets of round-half-up(value*bp/10000). Return {values, reserve}.

split: open-access


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

N = 1
observations = []
def solve(x):
    mat = sum(q * u for q, u in x['material'])
    labor = x['hours'] * x['rate']
    oh = x['hours'] * x['oh_bp'] // 10000
    total = mat + labor + oh
    if x['good_units'] == 0:
        return {'error': 'no good output'}
    scrap = x['input_units'] - x['good_units']
    normal = x['input_units'] * x['normal_bp'] // 10000
    abnormal = max(0, scrap - normal)
    ab_value = abnormal * (total // x['input_units'])
    credit = scrap * x['recovery']
    net = total - credit - ab_value
    unit = (2 * net + x['good_units']) // (2 * x['good_units'])
    return {'total': total, 'abnormal_units': abnormal, 'abnormal_value': ab_value, 'unit_cost': unit}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 501}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 39, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 9, 'abnormal_value': 1980, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2010, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6020, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 151}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 4, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 26037, 'abnormal_units': 3, 'abnormal_value': 2601, 'unit_cost': 866}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 151, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 502}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 38, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 10, 'abnormal_value': 2200, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2020, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6040, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 151}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 5, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 31797, 'abnormal_units': 3, 'abnormal_value': 3177, 'unit_cost': 1058}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 152, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 503}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 37, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 11, 'abnormal_value': 2420, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2030, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6060, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 152}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 6, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 37557, 'abnormal_units': 3, 'abnormal_value': 3753, 'unit_cost': 1250}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 153, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 504}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 36, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 12, 'abnormal_value': 2640, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2040, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6080, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 152}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 7, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 43317, 'abnormal_units': 3, 'abnormal_value': 4329, 'unit_cost': 1442}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 154, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 505}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 35, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 13, 'abnormal_value': 2860, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2050, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6100, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 153}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 8, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 49077, 'abnormal_units': 3, 'abnormal_value': 4905, 'unit_cost': 1634}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 155, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]]]
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):
    mat = sum(q * u for q, u in x['material'])
    labor = x['hours'] * x['rate']
    oh = mat * x['oh_bp'] // 10000
    total = mat + labor + oh
    if x['good_units'] == 0:
        return {'error': 'no good output'}
    scrap = x['input_units'] - x['good_units']
    normal = x['input_units'] * x['normal_bp'] // 10000
    abnormal = max(0, scrap - normal)
    ab_value = abnormal * (total // x['input_units'])
    credit = scrap * x['recovery']
    net = total - credit - ab_value
    unit = (2 * net + x['good_units']) // (2 * x['good_units'])
    return {'total': total, 'abnormal_units': abnormal, 'abnormal_value': ab_value, 'unit_cost': unit}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 501}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 39, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 9, 'abnormal_value': 1980, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2010, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6020, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 151}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 4, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 26037, 'abnormal_units': 3, 'abnormal_value': 2601, 'unit_cost': 866}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 151, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 502}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 38, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 10, 'abnormal_value': 2200, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2020, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6040, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 151}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 5, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 31797, 'abnormal_units': 3, 'abnormal_value': 3177, 'unit_cost': 1058}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 152, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 503}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 37, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 11, 'abnormal_value': 2420, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2030, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6060, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 152}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 6, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 37557, 'abnormal_units': 3, 'abnormal_value': 3753, 'unit_cost': 1250}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 153, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 504}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 36, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 12, 'abnormal_value': 2640, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2040, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6080, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 152}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 7, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 43317, 'abnormal_units': 3, 'abnormal_value': 4329, 'unit_cost': 1442}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 154, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]], [['normal run', {'material': [[10, 300], [5, 120]], 'hours': 4, 'rate': 2500, 'oh_bp': 15000, 'input_units': 100, 'good_units': 96, 'recovery': 5, 'normal_bp': 505}, {'total': 28600, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 298}], ['abnormal loss', {'material': [[20, 100]], 'hours': 2, 'rate': 3000, 'oh_bp': 5000, 'input_units': 50, 'good_units': 35, 'recovery': 10, 'normal_bp': 400}, {'total': 11000, 'abnormal_units': 13, 'abnormal_value': 2860, 'unit_cost': 228}], ['better than normal', {'material': [[8, 250]], 'hours': 1, 'rate': 2050, 'oh_bp': 10000, 'input_units': 40, 'good_units': 40, 'recovery': 0, 'normal_bp': 1000}, {'total': 6100, 'abnormal_units': 0, 'abnormal_value': 0, 'unit_cost': 153}], ['no good units', {'material': [[1, 100]], 'hours': 1, 'rate': 100, 'oh_bp': 0, 'input_units': 5, 'good_units': 0, 'recovery': 1, 'normal_bp': 0}, {'error': 'no good output'}], ['high overhead', {'material': [[3, 999]], 'hours': 8, 'rate': 1800, 'oh_bp': 22000, 'input_units': 30, 'good_units': 27, 'recovery': 20, 'normal_bp': 300}, {'total': 49077, 'abnormal_units': 3, 'abnormal_value': 4905, 'unit_cost': 1634}], ['recovery heavy', {'material': [[12, 450]], 'hours': 5, 'rate': 1500, 'oh_bp': 8000, 'input_units': 60, 'good_units': 51, 'recovery': 155, 'normal_bp': 1000}, {'total': 18900, 'abnormal_units': 3, 'abnormal_value': 945, 'unit_cost': 325}]]]
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:
Overhead absorbed by a lot is tiny because the rate is applied to hours instead of labor cost.

Input {material:[[qty,unit]], hours, rate, oh_bp (of labor cost), input_units, good_units, recovery (per scrapped unit), normal_bp}. Total = material + labor + overhead. Scrap = input - good; normal loss = floor(input*normal_bp/10000); abnormal = max(0, scrap - normal) valued at floor(total/input) per unit and expensed. Scrap recovery credits every scrapped unit. Good-unit cost = round-half-up((total - recovery credit - abnormal value)/good); good_units == 0 returns {"error"}. Return {total, abnormal_units, abnormal_value, unit_cost}.

split: open-access