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 = labor * 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 = 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 = labor * 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 = abs(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:
A lot that beats its normal yield books a negative abnormal loss that lowers good-unit cost below 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


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 = labor * 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['good_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)

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):
    mat = sum(q * u for q, u in x['material'])
    labor = x['hours'] * x['rate']
    oh = labor * 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 // max(1, scrap))
    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:
Abnormal loss is overvalued because it is priced at good-unit 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


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 = labor * 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 = abnormal * 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 = labor * 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 = min(scrap, normal) * 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:
Scrap sale proceeds are credited only for abnormal scrap, overstating good-unit 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


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 = labor * 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['input_units']) // (2 * x['input_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 = labor * 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['input_units'] - normal) // (2 * (x['input_units'] - normal))
    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:
Good-unit cost is understated because the lot cost is spread over units that were scrapped.

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


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

N = 1
observations = []
def solve(x):
    units = x['begin'][0] + sum(q for q, v in x['purchases'])
    pool = x['begin'][1] + sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= (x["begin"][1] + sum(v for q, v in x["purchases"])) * rq // (x["begin"][0] + sum(q for q, v in x["purchases"]))
        units -= rq
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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):
    units = x['begin'][0] + sum(q for q, v in x['purchases'])
    pool = x['begin'][1] + sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= x["purchases"][-1][1] * rq // x["purchases"][-1][0]
        units -= rq
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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:
Returned goods are removed at the blended average rather than their own invoice cost.

Input {begin:[qty,value], purchases:[[qty,value]], vendor_returns:[[purchase_index,qty]], ending_qty}. Each vendor return removes qty units and floor(value*qty/purchase_qty) value of that purchase. The pool is beginning plus purchases less returns; ending value = round-half-up(ending_qty*pool value/pool units); COGS = pool value - ending value. ending_qty outside [0, pool units] returns {"error"}. Return {units, pool, ending_value, cogs}.

split: open-access


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

N = 1
observations = []
def solve(x):
    units = x['begin'][0] + sum(q for q, v in x['purchases'])
    pool = sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= pv * rq // pq
        units -= rq
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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):
    units = sum(q for q, v in x['purchases'])
    pool = sum(v for q, v in x['purchases'])
    for i, rq in x['vendor_returns']:
        pq, pv = x['purchases'][i]
        pool -= pv * rq // pq
        units -= rq
    e = x['ending_qty']
    if e < 0 or e > units:
        return {'error': 'invalid count'}
    ending = (2 * e * pool + units) // (2 * units) if units else 0
    return {'units': units, 'pool': pool, 'ending_value': ending, 'cogs': pool - ending}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1501]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4451, 'ending_value': 1526, 'cogs': 2925}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 781]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1261, 'ending_value': 378, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1410]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2501], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3001, 'ending_value': 1200, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1502]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4452, 'ending_value': 1526, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 782]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1262, 'ending_value': 379, 'cogs': 883}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1411]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1824, 'ending_value': 1026, 'cogs': 798}], ['pricey begin', {'begin': [5, 2502], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3002, 'ending_value': 1201, 'cogs': 1801}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1503]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4453, 'ending_value': 1527, 'cogs': 2926}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 783]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1263, 'ending_value': 379, 'cogs': 884}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1412]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1825, 'ending_value': 1027, 'cogs': 798}], ['pricey begin', {'begin': [5, 2503], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3003, 'ending_value': 1201, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1504]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4454, 'ending_value': 1527, 'cogs': 2927}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 784]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1264, 'ending_value': 379, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 2]], 'ending_qty': 5}, {'units': 8, 'pool': 655, 'ending_value': 409, 'cogs': 246}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 6}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1413]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2504], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3004, 'ending_value': 1202, 'cogs': 1802}]], [['basic', {'begin': [10, 1000], 'purchases': [[20, 2600], [10, 1505]], 'vendor_returns': [[0, 5]], 'ending_qty': 12}, {'units': 35, 'pool': 4455, 'ending_value': 1527, 'cogs': 2928}], ['no returns', {'begin': [4, 480], 'purchases': [[6, 785]], 'vendor_returns': [], 'ending_qty': 3}, {'units': 10, 'pool': 1265, 'ending_value': 380, 'cogs': 885}], ['return second', {'begin': [0, 0], 'purchases': [[3, 100], [7, 777]], 'vendor_returns': [[1, 3]], 'ending_qty': 5}, {'units': 7, 'pool': 544, 'ending_value': 389, 'cogs': 155}], ['too many counted', {'begin': [2, 200], 'purchases': [[5, 600]], 'vendor_returns': [[0, 2]], 'ending_qty': 7}, {'error': 'invalid count'}], ['empty count', {'begin': [3, 330], 'purchases': [[3, 390]], 'vendor_returns': [[0, 1]], 'ending_qty': 0}, {'units': 5, 'pool': 590, 'ending_value': 0, 'cogs': 590}], ['odd costs', {'begin': [7, 701], 'purchases': [[3, 337], [11, 1414]], 'vendor_returns': [[1, 4], [0, 1]], 'ending_qty': 9}, {'units': 16, 'pool': 1826, 'ending_value': 1027, 'cogs': 799}], ['pricey begin', {'begin': [5, 2505], 'purchases': [[5, 500]], 'vendor_returns': [], 'ending_qty': 4}, {'units': 10, 'pool': 3005, 'ending_value': 1202, 'cogs': 1803}]]]
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 average ignores opening stock, so a costly opening balance does not raise the average.

Input {begin:[qty,value], purchases:[[qty,value]], vendor_returns:[[purchase_index,qty]], ending_qty}. Each vendor return removes qty units and floor(value*qty/purchase_qty) value of that purchase. The pool is beginning plus purchases less returns; ending value = round-half-up(ending_qty*pool value/pool units); COGS = pool value - ending value. ending_qty outside [0, pool units] returns {"error"}. Return {units, pool, ending_value, cogs}.

split: open-access