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

N = 1
observations = []
def solve(x):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = nrv
        prior = x['prior'].get(it['sku'], it['cost'])
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: max(0, v[0] - v[1]) for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = min(x['prior'].get(it['sku'], it['cost']), nrv)
        prior = x['prior'].get(it['sku'], it['cost'])
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: max(0, v[0] - v[1]) for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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:
Items whose NRV has risen above original cost are written up above cost.

Input {items:[{sku,qty,cost,price,complete,sell_bp,group}], prior:{sku:carrying unit}}. NRV = price - complete - round-half-up(price*sell_bp/10000), floored at 0. Carrying unit = min(original cost, NRV). Adjustment = qty*(carrying - prior carrying), prior defaulting to original cost (reversals are thereby capped at cost). Group check: write-down per group = max(0, sum qty*cost - sum qty*NRV). Return {items:[[sku,carry,adj]], group_writedown}.

split: open-access


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

N = 1
observations = []
def solve(x):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = min(it['cost'], nrv)
        prior = x['prior'].get(it['sku'], 0)
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: max(0, v[0] - v[1]) for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = min(it['cost'], nrv)
        prior = x['prior'].get(it['sku'], carry)
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: max(0, v[0] - v[1]) for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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:
Newly counted items show a huge positive adjustment as if they were previously carried at zero.

Input {items:[{sku,qty,cost,price,complete,sell_bp,group}], prior:{sku:carrying unit}}. NRV = price - complete - round-half-up(price*sell_bp/10000), floored at 0. Carrying unit = min(original cost, NRV). Adjustment = qty*(carrying - prior carrying), prior defaulting to original cost (reversals are thereby capped at cost). Group check: write-down per group = max(0, sum qty*cost - sum qty*NRV). Return {items:[[sku,carry,adj]], group_writedown}.

split: open-access


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

N = 1
observations = []
def solve(x):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = min(it['cost'], nrv)
        prior = x['prior'].get(it['sku'], it['cost'])
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: v[0] - v[1] for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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):
    res = []
    groups = {}
    for it in x['items']:
        sell = (it['price'] * it['sell_bp'] + 5000) // 10000
        nrv = it['price'] - it['complete'] - sell
        if nrv < 0:
            nrv = 0
        carry = min(it['cost'], nrv)
        prior = x['prior'].get(it['sku'], it['cost'])
        res.append([it['sku'], carry, it['qty'] * (carry - prior)])
        g = groups.setdefault(it['group'], [0, 0])
        g[0] += it['qty'] * it['cost']
        g[1] += it['qty'] * nrv
    gw = {k: abs(v[0] - v[1]) for k, v in sorted(groups.items())}
    return {'items': res, 'group_writedown': gw}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1001, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 251, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 601}}, {'items': [['D', 800, 796]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 601, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1002, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 252, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 602}}, {'items': [['D', 800, 792]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 602, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1003, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 253, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 603}}, {'items': [['D', 800, 788]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 603, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1004, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 890, -550]], 'group_writedown': {'g1': 550}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 254, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 604}}, {'items': [['D', 800, 784]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 604, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]], [['write down', {'items': [{'sku': 'A', 'qty': 5, 'cost': 1000, 'price': 1100, 'complete': 100, 'sell_bp': 1005, 'group': 'g1'}], 'prior': {}}, {'items': [['A', 889, -555]], 'group_writedown': {'g1': 555}}], ['no write down', {'items': [{'sku': 'B', 'qty': 2, 'cost': 500, 'price': 900, 'complete': 50, 'sell_bp': 500, 'group': 'g1'}], 'prior': {}}, {'items': [['B', 500, 0]], 'group_writedown': {'g1': 0}}], ['negative nrv', {'items': [{'sku': 'C', 'qty': 3, 'cost': 400, 'price': 300, 'complete': 255, 'sell_bp': 2000, 'group': 'g2'}], 'prior': {}}, {'items': [['C', 0, -1200]], 'group_writedown': {'g2': 1200}}], ['reversal capped', {'items': [{'sku': 'D', 'qty': 4, 'cost': 800, 'price': 1500, 'complete': 0, 'sell_bp': 0, 'group': 'g2'}], 'prior': {'D': 605}}, {'items': [['D', 800, 780]], 'group_writedown': {'g2': 0}}], ['partial reversal', {'items': [{'sku': 'E', 'qty': 1, 'cost': 800, 'price': 850, 'complete': 0, 'sell_bp': 1000, 'group': 'g1'}], 'prior': {'E': 700}}, {'items': [['E', 765, 65]], 'group_writedown': {'g1': 35}}], ['group offset', {'items': [{'sku': 'F', 'qty': 1, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}, {'sku': 'G', 'qty': 1, 'cost': 200, 'price': 605, 'complete': 0, 'sell_bp': 0, 'group': 'g3'}], 'prior': {}}, {'items': [['F', 700, -300], ['G', 200, 0]], 'group_writedown': {'g3': 0}}], ['group loss', {'items': [{'sku': 'H', 'qty': 2, 'cost': 1000, 'price': 700, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}, {'sku': 'J', 'qty': 1, 'cost': 200, 'price': 250, 'complete': 0, 'sell_bp': 0, 'group': 'g4'}], 'prior': {'H': 900}}, {'items': [['H', 700, -400], ['J', 200, 0]], 'group_writedown': {'g4': 550}}], ['costly completion', {'items': [{'sku': 'K', 'qty': 1, 'cost': 300, 'price': 1000, 'complete': 900, 'sell_bp': 500, 'group': 'g5'}], 'prior': {}}, {'items': [['K', 50, -250]], 'group_writedown': {'g5': 250}}]]]
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:
Groups whose NRV exceeds cost report a negative write-down, i.e. an unrealized gain.

Input {items:[{sku,qty,cost,price,complete,sell_bp,group}], prior:{sku:carrying unit}}. NRV = price - complete - round-half-up(price*sell_bp/10000), floored at 0. Carrying unit = min(original cost, NRV). Adjustment = qty*(carrying - prior carrying), prior defaulting to original cost (reversals are thereby capped at cost). Group check: write-down per group = max(0, sum qty*cost - sum qty*NRV). Return {items:[[sku,carry,adj]], group_writedown}.

split: open-access


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

N = 1
observations = []
def solve(x):
    cost_pool = x['begin_cost'] + x['purch_cost'] - x['returns_cost']
    retail_pool = x['begin_retail'] + x['purch_retail'] - x['returns_retail'] + x['markups'] - x['markup_cancel']
    net_md = x['markdowns'] - x['markdown_cancel']
    denom = retail_pool if x['method'] == 'conventional' else retail_pool - net_md
    ending_retail = retail_pool - net_md - x['sales'] - x['employee_discounts']
    ratio_bp = (2 * cost_pool * 10000 + denom) // (2 * denom)
    ending_cost = (2 * ending_retail * cost_pool + denom) // (2 * denom)
    return {'ratio_bp': ratio_bp, 'ending_retail': ending_retail, 'ending_cost': ending_cost}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5864, 'ending_retail': 16490, 'ending_cost': 9670}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6122, 'ending_retail': 16490, 'ending_cost': 10096}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40001, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5864, 'ending_retail': 21099, 'ending_cost': 12373}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8001, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6107, 'ending_retail': 10089, 'ending_cost': 6161}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5670, 'ending_retail': 17290, 'ending_cost': 9804}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1999, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6216, 'ending_retail': 12991, 'ending_cost': 8075}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3001, 'method': 'cost'}, {'ratio_bp': 6122, 'ending_retail': 14289, 'ending_cost': 8748}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5880, 'ending_retail': 16480, 'ending_cost': 9691}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6140, 'ending_retail': 16480, 'ending_cost': 10119}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40002, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5880, 'ending_retail': 21098, 'ending_cost': 12407}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8002, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6127, 'ending_retail': 10078, 'ending_cost': 6175}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5687, 'ending_retail': 17280, 'ending_cost': 9826}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1998, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6233, 'ending_retail': 12982, 'ending_cost': 8091}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3002, 'method': 'cost'}, {'ratio_bp': 6140, 'ending_retail': 14278, 'ending_cost': 8767}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5897, 'ending_retail': 16470, 'ending_cost': 9712}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6158, 'ending_retail': 16470, 'ending_cost': 10143}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40003, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5897, 'ending_retail': 21097, 'ending_cost': 12440}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8003, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6148, 'ending_retail': 10067, 'ending_cost': 6189}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5703, 'ending_retail': 17270, 'ending_cost': 9849}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1997, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6250, 'ending_retail': 12973, 'ending_cost': 8108}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3003, 'method': 'cost'}, {'ratio_bp': 6158, 'ending_retail': 14267, 'ending_cost': 8786}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5913, 'ending_retail': 16460, 'ending_cost': 9732}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6176, 'ending_retail': 16460, 'ending_cost': 10166}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40004, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5913, 'ending_retail': 21096, 'ending_cost': 12474}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8004, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6168, 'ending_retail': 10056, 'ending_cost': 6202}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5719, 'ending_retail': 17260, 'ending_cost': 9871}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1996, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6267, 'ending_retail': 12964, 'ending_cost': 8124}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3004, 'method': 'cost'}, {'ratio_bp': 6176, 'ending_retail': 14256, 'ending_cost': 8805}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5929, 'ending_retail': 16450, 'ending_cost': 9753}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6194, 'ending_retail': 16450, 'ending_cost': 10189}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40005, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5929, 'ending_retail': 21095, 'ending_cost': 12507}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8005, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6188, 'ending_retail': 10045, 'ending_cost': 6216}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5735, 'ending_retail': 17250, 'ending_cost': 9893}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1995, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6284, 'ending_retail': 12955, 'ending_cost': 8141}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3005, 'method': 'cost'}, {'ratio_bp': 6194, 'ending_retail': 14245, 'ending_cost': 8823}]]]
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):
    cost_pool = x['begin_cost'] + x['purch_cost'] + x['freight_in'] - x['returns_cost']
    retail_pool = x['begin_retail'] + x['purch_retail'] - x['returns_retail'] + x['markups'] - x['markup_cancel'] + x['freight_in']
    net_md = x['markdowns'] - x['markdown_cancel']
    denom = retail_pool if x['method'] == 'conventional' else retail_pool - net_md
    ending_retail = retail_pool - net_md - x['sales'] - x['employee_discounts']
    ratio_bp = (2 * cost_pool * 10000 + denom) // (2 * denom)
    ending_cost = (2 * ending_retail * cost_pool + denom) // (2 * denom)
    return {'ratio_bp': ratio_bp, 'ending_retail': ending_retail, 'ending_cost': ending_cost}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5864, 'ending_retail': 16490, 'ending_cost': 9670}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6122, 'ending_retail': 16490, 'ending_cost': 10096}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40001, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5864, 'ending_retail': 21099, 'ending_cost': 12373}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8001, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6107, 'ending_retail': 10089, 'ending_cost': 6161}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5670, 'ending_retail': 17290, 'ending_cost': 9804}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1999, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6216, 'ending_retail': 12991, 'ending_cost': 8075}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30100, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3010, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3001, 'method': 'cost'}, {'ratio_bp': 6122, 'ending_retail': 14289, 'ending_cost': 8748}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5880, 'ending_retail': 16480, 'ending_cost': 9691}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6140, 'ending_retail': 16480, 'ending_cost': 10119}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40002, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5880, 'ending_retail': 21098, 'ending_cost': 12407}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8002, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6127, 'ending_retail': 10078, 'ending_cost': 6175}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5687, 'ending_retail': 17280, 'ending_cost': 9826}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1998, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6233, 'ending_retail': 12982, 'ending_cost': 8091}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30200, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3020, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3002, 'method': 'cost'}, {'ratio_bp': 6140, 'ending_retail': 14278, 'ending_cost': 8767}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5897, 'ending_retail': 16470, 'ending_cost': 9712}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6158, 'ending_retail': 16470, 'ending_cost': 10143}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40003, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5897, 'ending_retail': 21097, 'ending_cost': 12440}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8003, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6148, 'ending_retail': 10067, 'ending_cost': 6189}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5703, 'ending_retail': 17270, 'ending_cost': 9849}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1997, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6250, 'ending_retail': 12973, 'ending_cost': 8108}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30300, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3030, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3003, 'method': 'cost'}, {'ratio_bp': 6158, 'ending_retail': 14267, 'ending_cost': 8786}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5913, 'ending_retail': 16460, 'ending_cost': 9732}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6176, 'ending_retail': 16460, 'ending_cost': 10166}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40004, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5913, 'ending_retail': 21096, 'ending_cost': 12474}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8004, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6168, 'ending_retail': 10056, 'ending_cost': 6202}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5719, 'ending_retail': 17260, 'ending_cost': 9871}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1996, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6267, 'ending_retail': 12964, 'ending_cost': 8124}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30400, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3040, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3004, 'method': 'cost'}, {'ratio_bp': 6176, 'ending_retail': 14256, 'ending_cost': 8805}]], [['conventional', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5929, 'ending_retail': 16450, 'ending_cost': 9753}], ['cost method', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6194, 'ending_retail': 16450, 'ending_cost': 10189}], ['no markdowns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 0, 'markdown_cancel': 0, 'sales': 40005, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 5929, 'ending_retail': 21095, 'ending_cost': 12507}], ['heavy returns', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 5000, 'returns_retail': 8005, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'cost'}, {'ratio_bp': 6188, 'ending_retail': 10045, 'ending_cost': 6216}], ['no freight', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 0, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 0, 'method': 'conventional'}, {'ratio_bp': 5735, 'ending_retail': 17250, 'ending_cost': 9893}], ['markup cancel', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 2000, 'markup_cancel': 1995, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 800, 'method': 'conventional'}, {'ratio_bp': 6284, 'ending_retail': 12955, 'ending_cost': 8141}], ['discounts heavy', {'begin_cost': 6000, 'begin_retail': 10000, 'purch_cost': 30500, 'purch_retail': 50000, 'freight_in': 1200, 'returns_cost': 1000, 'returns_retail': 1600, 'markups': 4000, 'markup_cancel': 500, 'markdowns': 3050, 'markdown_cancel': 400, 'sales': 42000, 'employee_discounts': 3005, 'method': 'cost'}, {'ratio_bp': 6194, 'ending_retail': 14245, 'ending_cost': 8823}]]]
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:
Cost-to-retail ratio is too low because inbound freight is left out of the cost pool.

Input totals for a period. Cost pool = begin_cost + purch_cost + freight_in - returns_cost. Retail pool = begin_retail + purch_retail - returns_retail + markups - markup_cancel. Net markdowns = markdowns - markdown_cancel. The conventional (LCM) ratio uses the retail pool before markdowns; the cost method ratio deducts net markdowns. Ending retail = retail pool - net markdowns - sales - employee_discounts. Ending cost = round-half-up(ending retail * cost pool / ratio denominator). Return {ratio_bp (rounded half up), ending_retail, ending_cost}.

split: open-access