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

N = 1
observations = []
def solve(x):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        base = list(vals)
        tb = sum(base)
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = ((vals[i] + shares[i]) * x['duty_bp'] + 5000) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        base = list(vals)
        tb = sum(base)
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = ((vals[i] + shares[i]) * x['duty_bp']) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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:
Duty is overstated because it is levied on the allocated freight as well as the goods.

Input {lines:[{sku,qty,unit_cost,weight(per unit)}], freight, duty_bp, basis:"weight"|"value"}. Freight is allocated in proportion to qty*weight (or line value); floor shares are topped up one cent at a time by largest remainder, ties to the earlier line. If the weight basis sums to zero, fall back to value; if that is zero too return {"error"}. Duty is round-half-up(line value*duty_bp/10000) on merchandise value only. Landed total = value+freight share+duty; unit = round-half-up(landed/qty).

split: open-access


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

N = 1
observations = []
def solve(x):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        base = list(vals)
        tb = sum(base)
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = (vals[i] * x['duty_bp']) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        base = list(vals)
        tb = sum(base)
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = (vals[i] * x['duty_bp'] + 4999) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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:
Duty amounts ending in exactly half a cent are rounded down.

Input {lines:[{sku,qty,unit_cost,weight(per unit)}], freight, duty_bp, basis:"weight"|"value"}. Freight is allocated in proportion to qty*weight (or line value); floor shares are topped up one cent at a time by largest remainder, ties to the earlier line. If the weight basis sums to zero, fall back to value; if that is zero too return {"error"}. Duty is round-half-up(line value*duty_bp/10000) on merchandise value only. Landed total = value+freight share+duty; unit = round-half-up(landed/qty).

split: open-access


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

N = 1
observations = []
def solve(x):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        return {'error': 'no weights'}
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = (vals[i] * x['duty_bp'] + 5000) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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):
    lines = x['lines']
    freight = x['freight']
    vals = [l['qty'] * l['unit_cost'] for l in lines]
    if x['basis'] == 'weight':
        base = [l['qty'] * l['weight'] for l in lines]
    else:
        base = list(vals)
    tb = sum(base)
    if tb == 0:
        base = [1] * len(lines)
        tb = sum(base)
    if tb == 0:
        return {'error': 'no allocation basis'}
    shares = [freight * b // tb for b in base]
    rems = [freight * b % tb for b in base]
    left = freight - sum(shares)
    order = sorted(range(len(lines)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        shares[i] += 1
    out = []
    for i, l in enumerate(lines):
        duty = (vals[i] * x['duty_bp'] + 5000) // 10000
        total = vals[i] + shares[i] + duty
        unit = (2 * total + l['qty']) // (2 * l['qty'])
        out.append({'sku': l['sku'], 'freight': shares[i], 'duty': duty, 'landed': total, 'unit': unit})
    return out
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 4}], 'freight': 1001, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 834, 'duty': 75, 'landed': 3909, 'unit': 391}, {'sku': 'B', 'freight': 167, 'duty': 125, 'landed': 5292, 'unit': 5292}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 116, 'unit': 116}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 104, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'B', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}, {'sku': 'C', 'freight': 34, 'duty': 1, 'landed': 135, 'unit': 135}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 11, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 6, 'duty': 0, 'landed': 16, 'unit': 16}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 301, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 241, 'duty': 40, 'landed': 1081, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 50, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 300, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 6, 'landed': 606, 'unit': 303}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1201, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 5}], 'freight': 1002, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 802, 'duty': 75, 'landed': 3877, 'unit': 388}, {'sku': 'B', 'freight': 200, 'duty': 125, 'landed': 5325, 'unit': 5325}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 101, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 117, 'unit': 117}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 107, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'B', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}, {'sku': 'C', 'freight': 35, 'duty': 1, 'landed': 136, 'unit': 136}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 12, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 3, 'duty': 0, 'landed': 13, 'unit': 13}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 302, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 60, 'duty': 10, 'landed': 270, 'unit': 270}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 100, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 350, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 7, 'landed': 707, 'unit': 354}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1202, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 601, 'landed': 7340, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 360, 'landed': 3618, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 6}], 'freight': 1003, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 772, 'duty': 75, 'landed': 3847, 'unit': 385}, {'sku': 'B', 'freight': 231, 'duty': 125, 'landed': 5356, 'unit': 5356}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 102, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 339, 'duty': 0, 'landed': 2439, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 16, 'duty': 0, 'landed': 118, 'unit': 118}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 110, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'B', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}, {'sku': 'C', 'freight': 36, 'duty': 1, 'landed': 137, 'unit': 137}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 13, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 7, 'duty': 0, 'landed': 17, 'unit': 17}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 303, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 242, 'duty': 40, 'landed': 1082, 'unit': 541}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 150, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 400, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 8, 'landed': 808, 'unit': 404}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1203, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 7}], 'freight': 1004, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 744, 'duty': 75, 'landed': 3819, 'unit': 382}, {'sku': 'B', 'freight': 260, 'duty': 125, 'landed': 5385, 'unit': 5385}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 103, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 120, 'unit': 120}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 113, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'B', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}, {'sku': 'C', 'freight': 37, 'duty': 1, 'landed': 138, 'unit': 138}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 14, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 8, 'duty': 0, 'landed': 18, 'unit': 18}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 304, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 243, 'duty': 40, 'landed': 1083, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 200, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 9, 'landed': 909, 'unit': 455}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1204, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 602, 'landed': 7341, 'unit': 1835}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]], [['weight basis', {'lines': [{'sku': 'A', 'qty': 10, 'unit_cost': 300, 'weight': 2}, {'sku': 'B', 'qty': 1, 'unit_cost': 5000, 'weight': 8}], 'freight': 1005, 'duty_bp': 250, 'basis': 'weight'}, [{'sku': 'A', 'freight': 718, 'duty': 75, 'landed': 3793, 'unit': 379}, {'sku': 'B', 'freight': 287, 'duty': 125, 'landed': 5412, 'unit': 5412}]], ['value basis', {'lines': [{'sku': 'A', 'qty': 3, 'unit_cost': 700, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 450, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 104, 'weight': 9}], 'freight': 500, 'duty_bp': 0, 'basis': 'value'}, [{'sku': 'A', 'freight': 338, 'duty': 0, 'landed': 2438, 'unit': 813}, {'sku': 'B', 'freight': 145, 'duty': 0, 'landed': 1045, 'unit': 523}, {'sku': 'C', 'freight': 17, 'duty': 0, 'landed': 121, 'unit': 121}]], ['remainder spread', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 100, 'weight': 1}, {'sku': 'C', 'qty': 1, 'unit_cost': 100, 'weight': 1}], 'freight': 116, 'duty_bp': 100, 'basis': 'weight'}, [{'sku': 'A', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'B', 'freight': 39, 'duty': 1, 'landed': 140, 'unit': 140}, {'sku': 'C', 'freight': 38, 'duty': 1, 'landed': 139, 'unit': 139}]], ['uneven remainders', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 10, 'weight': 1}, {'sku': 'B', 'qty': 1, 'unit_cost': 10, 'weight': 2}, {'sku': 'C', 'qty': 1, 'unit_cost': 10, 'weight': 4}], 'freight': 15, 'duty_bp': 0, 'basis': 'weight'}, [{'sku': 'A', 'freight': 2, 'duty': 0, 'landed': 12, 'unit': 12}, {'sku': 'B', 'freight': 4, 'duty': 0, 'landed': 14, 'unit': 14}, {'sku': 'C', 'freight': 9, 'duty': 0, 'landed': 19, 'unit': 19}]], ['zero weights', {'lines': [{'sku': 'A', 'qty': 2, 'unit_cost': 400, 'weight': 0}, {'sku': 'B', 'qty': 1, 'unit_cost': 200, 'weight': 0}], 'freight': 305, 'duty_bp': 500, 'basis': 'weight'}, [{'sku': 'A', 'freight': 244, 'duty': 40, 'landed': 1084, 'unit': 542}, {'sku': 'B', 'freight': 61, 'duty': 10, 'landed': 271, 'unit': 271}]], ['nothing to allocate', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 0, 'weight': 0}], 'freight': 250, 'duty_bp': 0, 'basis': 'weight'}, {'error': 'no allocation basis'}], ['half cent duty', {'lines': [{'sku': 'A', 'qty': 1, 'unit_cost': 150, 'weight': 1}, {'sku': 'B', 'qty': 2, 'unit_cost': 500, 'weight': 1}], 'freight': 0, 'duty_bp': 100, 'basis': 'value'}, [{'sku': 'A', 'freight': 0, 'duty': 2, 'landed': 152, 'unit': 152}, {'sku': 'B', 'freight': 0, 'duty': 10, 'landed': 1010, 'unit': 505}]], ['heavy duty', {'lines': [{'sku': 'A', 'qty': 4, 'unit_cost': 1250, 'weight': 5}, {'sku': 'B', 'qty': 3, 'unit_cost': 999, 'weight': 1}], 'freight': 2000, 'duty_bp': 1205, 'basis': 'weight'}, [{'sku': 'A', 'freight': 1739, 'duty': 603, 'landed': 7342, 'unit': 1836}, {'sku': 'B', 'freight': 261, 'duty': 361, 'landed': 3619, 'unit': 1206}]]]]
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 receipt of items with no recorded weight fails with no allocation basis even though values exist.

Input {lines:[{sku,qty,unit_cost,weight(per unit)}], freight, duty_bp, basis:"weight"|"value"}. Freight is allocated in proportion to qty*weight (or line value); floor shares are topped up one cent at a time by largest remainder, ties to the earlier line. If the weight basis sums to zero, fall back to value; if that is zero too return {"error"}. Duty is round-half-up(line value*duty_bp/10000) on merchandise value only. Landed total = value+freight share+duty; unit = round-half-up(landed/qty).

split: open-access