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

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

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

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

prompt:
The average cost falls after a vendor return because units stay in the pool while value leaves.

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

split: open-access


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

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

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

prompt:
A physical count that includes goods already returned to the vendor is accepted.

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

split: open-access


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

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

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

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

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

split: open-access


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

N = 1
observations = []
def solve(x):
    end = x['period_end']
    add = 0
    sub = 0
    for it in x['items']:
        k = it['type']
        v = it['value']
        arrived = it['arrived_day'] is not None and it['arrived_day'] <= end
        if k == 'in_transit_in':
            if it['terms'] == 'FOB_destination' and it['shipped_day'] <= end and not arrived:
                add += v
        elif k == 'in_transit_out':
            if it['terms'] == 'FOB_destination' and it['shipped_day'] <= end and not arrived:
                add += v
        elif k == 'consigned_out':
            add += v
        elif k in ('consigned_in', 'bill_and_hold'):
            sub += v
    return {'adjusted': x['count_value'] + add - sub, 'adds': add, 'subtracts': sub}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 501, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10501, 'adds': 501, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 201, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5501, 'adds': 501, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 26}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 801, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 451, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9749, 'adds': 1200, 'subtracts': 451}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 351, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5549, 'adds': 0, 'subtracts': 451}], ['undelivered none', {'count_value': 100, 'period_end': 32, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 502, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10502, 'adds': 502, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 202, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5502, 'adds': 502, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 27}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 802, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 452, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9748, 'adds': 1200, 'subtracts': 452}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 352, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5548, 'adds': 0, 'subtracts': 452}], ['undelivered none', {'count_value': 100, 'period_end': 33, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 503, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10503, 'adds': 503, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 203, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5503, 'adds': 503, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 28}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 803, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 453, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9747, 'adds': 1200, 'subtracts': 453}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 353, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5547, 'adds': 0, 'subtracts': 453}], ['undelivered none', {'count_value': 100, 'period_end': 34, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 504, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10504, 'adds': 504, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 204, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5504, 'adds': 504, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 29}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 804, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 454, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9746, 'adds': 1200, 'subtracts': 454}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 354, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5546, 'adds': 0, 'subtracts': 454}], ['undelivered none', {'count_value': 100, 'period_end': 35, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 505, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10505, 'adds': 505, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 205, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5505, 'adds': 505, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 30}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 805, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 455, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9745, 'adds': 1200, 'subtracts': 455}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 355, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5545, 'adds': 0, 'subtracts': 455}], ['undelivered none', {'count_value': 100, 'period_end': 36, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]]]
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):
    end = x['period_end']
    add = 0
    sub = 0
    for it in x['items']:
        k = it['type']
        v = it['value']
        arrived = it['arrived_day'] is not None and it['arrived_day'] <= end
        if k == 'in_transit_in':
            if it['shipped_day'] <= end and not arrived:
                add += v
        elif k == 'in_transit_out':
            if it['terms'] == 'FOB_destination' and it['shipped_day'] <= end and not arrived:
                add += v
        elif k == 'consigned_out':
            add += v
        elif k in ('consigned_in', 'bill_and_hold'):
            sub += v
    return {'adjusted': x['count_value'] + add - sub, 'adds': add, 'subtracts': sub}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 501, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10501, 'adds': 501, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 201, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5501, 'adds': 501, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 26}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 801, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 451, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9749, 'adds': 1200, 'subtracts': 451}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 351, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5549, 'adds': 0, 'subtracts': 451}], ['undelivered none', {'count_value': 100, 'period_end': 32, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 502, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10502, 'adds': 502, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 202, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5502, 'adds': 502, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 27}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 802, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 452, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9748, 'adds': 1200, 'subtracts': 452}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 352, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5548, 'adds': 0, 'subtracts': 452}], ['undelivered none', {'count_value': 100, 'period_end': 33, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 503, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10503, 'adds': 503, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 203, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5503, 'adds': 503, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 28}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 803, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 453, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9747, 'adds': 1200, 'subtracts': 453}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 353, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5547, 'adds': 0, 'subtracts': 453}], ['undelivered none', {'count_value': 100, 'period_end': 34, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 504, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10504, 'adds': 504, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 204, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5504, 'adds': 504, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 29}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 804, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 454, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9746, 'adds': 1200, 'subtracts': 454}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 354, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5546, 'adds': 0, 'subtracts': 454}], ['undelivered none', {'count_value': 100, 'period_end': 35, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]], [['inbound shipping terms', {'count_value': 10000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 505, 'shipped_day': 30, 'arrived_day': None}, {'type': 'in_transit_in', 'terms': 'FOB_destination', 'value': 700, 'shipped_day': 29, 'arrived_day': 33}]}, {'adjusted': 10505, 'adds': 505, 'subtracts': 0}], ['shipped on period end', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 31, 'arrived_day': 34}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 205, 'shipped_day': 31, 'arrived_day': None}]}, {'adjusted': 5505, 'adds': 505, 'subtracts': 0}], ['shipped after period', {'count_value': 5000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 300, 'shipped_day': 32, 'arrived_day': 35}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 250, 'shipped_day': 32, 'arrived_day': None}]}, {'adjusted': 5000, 'adds': 0, 'subtracts': 0}], ['arrived before end', {'count_value': 8000, 'period_end': 31, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 400, 'shipped_day': 20, 'arrived_day': 30}, {'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 90, 'shipped_day': 28, 'arrived_day': 31}]}, {'adjusted': 8000, 'adds': 0, 'subtracts': 0}], ['outbound terms', {'count_value': 7000, 'period_end': 31, 'items': [{'type': 'in_transit_out', 'terms': 'FOB_destination', 'value': 600, 'shipped_day': 30, 'arrived_day': 32}, {'type': 'in_transit_out', 'terms': 'FOB_shipping', 'value': 805, 'shipped_day': 30, 'arrived_day': 33}]}, {'adjusted': 7600, 'adds': 600, 'subtracts': 0}], ['consignment', {'count_value': 9000, 'period_end': 31, 'items': [{'type': 'consigned_out', 'terms': 'none', 'value': 1200, 'shipped_day': 10, 'arrived_day': None}, {'type': 'consigned_in', 'terms': 'none', 'value': 455, 'shipped_day': 5, 'arrived_day': 6}]}, {'adjusted': 9745, 'adds': 1200, 'subtracts': 455}], ['bill and hold', {'count_value': 6000, 'period_end': 31, 'items': [{'type': 'bill_and_hold', 'terms': 'FOB_destination', 'value': 355, 'shipped_day': 0, 'arrived_day': None}, {'type': 'bill_and_hold', 'terms': 'FOB_shipping', 'value': 100, 'shipped_day': 0, 'arrived_day': None}]}, {'adjusted': 5545, 'adds': 0, 'subtracts': 455}], ['undelivered none', {'count_value': 100, 'period_end': 36, 'items': [{'type': 'in_transit_in', 'terms': 'FOB_shipping', 'value': 55, 'shipped_day': 12, 'arrived_day': None}]}, {'adjusted': 155, 'adds': 55, 'subtracts': 0}]]]
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:
Purchases shipped FOB shipping point are missing from year-end inventory while FOB destination goods still at sea are added.

Input {count_value, period_end, items:[{type, terms, value, shipped_day, arrived_day|None}]}. Goods "arrived" when arrived_day is set and <= period_end. Inbound in transit is added when FOB_shipping, shipped by period end and not arrived. Outbound in transit is added back when FOB_destination, shipped by period end and not arrived. Goods consigned out are added; consigned-in and bill-and-hold goods (counted but not owned) are subtracted regardless of terms. Return {adjusted, adds, subtracts}.

split: open-access