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

N = 1
observations = []
def solve(x):
    wo = set(x['writeoffs'])
    allowance = x['allowance']
    buckets = [0] * len(x['rates'])
    for iid, due, amt in x['invoices']:
        if iid in wo:
            allowance -= amt
            continue
        past = max(0, x['as_of'] - due)
        for k, (limit, bp) in enumerate(x['rates']):
            if limit is None or past <= limit:
                buckets[k] += amt
                break
    required = 0
    for total, (limit, bp) in zip(buckets, x['rates']):
        q, r = divmod(total * bp, 10000)
        required += q + (1 if 2 * r >= 10000 else 0)
    ar = sum(buckets)
    return {'buckets': buckets, 'required': required, 'adjustment': required - abs(allowance), 'net_ar': ar - required}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', -100, 5], ['I1', 39, 333], ['I2', 9, 2500], ['I3', -100, 4000], ['I4', 69, 150], ['I5', 70, 2500], ['I6', 55, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [0, 0, 150, 333, 6505], 'required': 5035, 'adjustment': 11535, 'net_ar': 1953}], ['control 1', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 110, 150], ['I2', 100, 4000], ['I3', 40, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [4150, 0, 8000], 'required': 4083, 'adjustment': 4083, 'net_ar': 8067}], ['control 2', {'as_of': 100, 'invoices': [['I0', 100, 333], ['I1', 70, 2500], ['I2', 55, 5], ['I3', 39, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [333, 2505, 2500], 'required': 1570, 'adjustment': 1370, 'net_ar': 3768}], ['control 3', {'as_of': 100, 'invoices': [['I0', 40, 333], ['I1', 70, 150], ['I2', 99, 150], ['I3', 9, 333], ['I4', 100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [333, 300, 333, 0, 333], 'required': 318, 'adjustment': -182, 'net_ar': 981}], ['control 4', {'as_of': 100, 'invoices': [['I0', 40, 2500], ['I1', 99, 4000], ['I2', 9, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4000, 3500], 'required': 2250, 'adjustment': 2250, 'net_ar': 5250}], ['control 5', {'as_of': 100, 'invoices': [['I0', 70, 333], ['I1', 70, 150], ['I2', 54, 5], ['I3', 100, 2500], ['I4', 100, 1000], ['I5', 70, 5], ['I6', 54, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [3500, 488, 1005], 'required': 634, 'adjustment': 634, 'net_ar': 4359}], ['control 6', {'as_of': 100, 'invoices': [['I0', 69, 150], ['I1', 54, 150], ['I2', 54, 4000], ['I3', 70, 4000], ['I4', 40, 333], ['I5', 69, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 4000, 7133, 0, 0], 'required': 1270, 'adjustment': 1070, 'net_ar': 9863}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 99, 2500], ['I1', 69, 1000], ['I2', 55, 333], ['I3', 39, 1000], ['I4', 39, 333], ['I5', 40, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1', 'I4', 'I5']}, {'buckets': [0, 2500, 333, 1000, 0], 'required': 575, 'adjustment': 5708, 'net_ar': 3258}], ['control 1', {'as_of': 100, 'invoices': [['I0', 70, 5], ['I1', 69, 5], ['I2', 71, 333], ['I3', -100, 5], ['I4', 54, 333], ['I5', 100, 150], ['I6', 71, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0', 'I5']}, {'buckets': [0, 483, 338, 0, 5], 'required': 79, 'adjustment': 34, 'net_ar': 747}], ['control 2', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 69, 333], ['I2', 55, 150], ['I3', 70, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 633, 1000], 'required': 579, 'adjustment': 379, 'net_ar': 1054}], ['control 3', {'as_of': 100, 'invoices': [['I0', 39, 333], ['I1', 100, 1000], ['I2', 54, 2500], ['I3', -100, 2500], ['I4', 71, 150], ['I5', 70, 4000], ['I6', -100, 1000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [1000, 4150, 2500, 333, 3500], 'required': 3351, 'adjustment': 2851, 'net_ar': 8132}], ['control 4', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', -100, 2500], ['I2', 55, 5], ['I3', 39, 5], ['I4', 99, 4000], ['I5', 69, 2500], ['I6', 40, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 8000, 6505, 0, 2500], 'required': 3251, 'adjustment': 2756, 'net_ar': 13754}], ['control 5', {'as_of': 100, 'invoices': [['I0', 100, 5], ['I1', 69, 2500], ['I2', 9, 4000], ['I3', 39, 4000], ['I4', 99, 4000], ['I5', 39, 1000], ['I6', 100, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [10, 6500, 9000], 'required': 5313, 'adjustment': 5313, 'net_ar': 10197}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 150], ['I1', 55, 150], ['I2', -100, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I2']}, {'buckets': [0, 150, 150, 0, 0], 'required': 31, 'adjustment': -164, 'net_ar': 269}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 69, 4000], ['I1', 110, 4000], ['I2', 55, 1000], ['I3', 100, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 0, 5000, 0, 0], 'required': 790, 'adjustment': 4790, 'net_ar': 8210}], ['control 1', {'as_of': 100, 'invoices': [['I0', 54, 150], ['I1', 71, 4000], ['I2', 70, 2500], ['I3', 110, 150], ['I4', 99, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 6505, 150, 0, 0], 'required': 348, 'adjustment': -2, 'net_ar': 6307}], ['control 2', {'as_of': 100, 'invoices': [['I0', 100, 4000], ['I1', 69, 1000], ['I2', 55, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [4000, 5000, 0], 'required': 705, 'adjustment': 505, 'net_ar': 8295}], ['control 3', {'as_of': 100, 'invoices': [['I0', 100, 1000], ['I1', 54, 333], ['I2', 69, 1000], ['I3', 40, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [1000, 1000, 483], 'required': 387, 'adjustment': 187, 'net_ar': 2096}], ['control 4', {'as_of': 100, 'invoices': [['I0', 39, 2500], ['I1', 69, 150], ['I2', 55, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 333, 2500, 0], 'required': 1050, 'adjustment': 1000, 'net_ar': 1783}], ['control 5', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', 54, 1000], ['I2', 70, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 4005, 1000, 0, 0], 'required': 350, 'adjustment': 150, 'net_ar': 4655}], ['control 6', {'as_of': 100, 'invoices': [['I0', 9, 2500], ['I1', 10, 2500], ['I2', 55, 333], ['I3', 69, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 0, 338, 2500, 2500], 'required': 2926, 'adjustment': 2926, 'net_ar': 2412}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 110, 150], ['I1', 10, 1000], ['I2', 70, 150], ['I3', 99, 1000], ['I4', 100, 2500], ['I5', 55, 333], ['I6', 54, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [2650, 1150, 0, 1000, 0], 'required': 485, 'adjustment': 968, 'net_ar': 4315}], ['control 1', {'as_of': 100, 'invoices': [['I0', 69, 150], ['I1', 71, 150], ['I2', 70, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [0, 150, 150, 0, 0], 'required': 31, 'adjustment': -319, 'net_ar': 269}], ['control 2', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 100, 5], ['I2', 70, 150], ['I3', 110, 150], ['I4', 100, 2500], ['I5', -100, 333], ['I6', 100, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [2805, 150, 1000, 0, 333], 'required': 436, 'adjustment': 436, 'net_ar': 3852}], ['control 3', {'as_of': 100, 'invoices': [['I0', 71, 333], ['I1', 39, 150], ['I2', 71, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4333, 0, 150, 0], 'required': 277, 'adjustment': 277, 'net_ar': 4206}], ['control 4', {'as_of': 100, 'invoices': [['I0', 110, 2500], ['I1', 69, 150], ['I2', 54, 2500], ['I3', 10, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [2500, 0, 2500, 333, 0], 'required': 533, 'adjustment': 483, 'net_ar': 4800}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 69, 150], ['I2', 55, 5], ['I3', 71, 1000], ['I4', 55, 4000], ['I5', -100, 2500], ['I6', 69, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [0, 1000, 10655, 0, 2500], 'required': 3523, 'adjustment': 3023, 'net_ar': 10632}], ['control 6', {'as_of': 100, 'invoices': [['I0', 71, 150], ['I1', 10, 5], ['I2', 54, 4000], ['I3', 54, 2500], ['I4', 55, 1000], ['I5', 99, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4150, 7500, 5, 0], 'required': 1335, 'adjustment': 1335, 'net_ar': 10320}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', 70, 2500], ['I2', 100, 4000], ['I3', 71, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 4005, 0], 'required': 581, 'adjustment': 3081, 'net_ar': 7424}], ['control 1', {'as_of': 100, 'invoices': [['I0', 70, 2500], ['I1', 110, 4000], ['I2', -100, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [4000, 2500, 0], 'required': 393, 'adjustment': 43, 'net_ar': 6107}], ['control 2', {'as_of': 100, 'invoices': [['I0', 40, 4000], ['I1', 39, 1000], ['I2', -100, 5], ['I3', 99, 5], ['I4', -100, 333], ['I5', 10, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 0, 4000, 5000, 338], 'required': 2854, 'adjustment': 2359, 'net_ar': 6484}], ['control 3', {'as_of': 100, 'invoices': [['I0', 54, 5], ['I1', 40, 5], ['I2', 99, 333], ['I3', 71, 150], ['I4', 54, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 483, 4010], 'required': 2065, 'adjustment': 2065, 'net_ar': 2428}], ['control 4', {'as_of': 100, 'invoices': [['I0', 39, 2500], ['I1', 39, 150], ['I2', 10, 4000], ['I3', 9, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 0, 0, 6650, 2500], 'required': 4535, 'adjustment': 4335, 'net_ar': 4615}], ['control 5', {'as_of': 100, 'invoices': [['I0', 71, 2500], ['I1', 54, 4000], ['I2', 40, 333], ['I3', 54, 4000], ['I4', 100, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [5, 2500, 8333, 0, 0], 'required': 1375, 'adjustment': 1175, 'net_ar': 9463}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 150], ['I1', 9, 4000], ['I2', -100, 2500], ['I3', 110, 333], ['I4', 9, 1000], ['I5', 69, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [333, 150, 5, 0, 7500], 'required': 5637, 'adjustment': 5137, 'net_ar': 2351}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    wo = set(x['writeoffs'])
    allowance = x['allowance']
    buckets = [0] * len(x['rates'])
    for iid, due, amt in x['invoices']:
        if iid in wo:
            allowance -= amt
            continue
        past = max(0, x['as_of'] - due)
        for k, (limit, bp) in enumerate(x['rates']):
            if limit is None or past <= limit:
                buckets[k] += amt
                break
    required = 0
    for total, (limit, bp) in zip(buckets, x['rates']):
        q, r = divmod(total * bp, 10000)
        required += q + (1 if 2 * r >= 10000 else 0)
    ar = sum(buckets)
    return {'buckets': buckets, 'required': required, 'adjustment': required + allowance, 'net_ar': ar - required}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', -100, 5], ['I1', 39, 333], ['I2', 9, 2500], ['I3', -100, 4000], ['I4', 69, 150], ['I5', 70, 2500], ['I6', 55, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [0, 0, 150, 333, 6505], 'required': 5035, 'adjustment': 11535, 'net_ar': 1953}], ['control 1', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 110, 150], ['I2', 100, 4000], ['I3', 40, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [4150, 0, 8000], 'required': 4083, 'adjustment': 4083, 'net_ar': 8067}], ['control 2', {'as_of': 100, 'invoices': [['I0', 100, 333], ['I1', 70, 2500], ['I2', 55, 5], ['I3', 39, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [333, 2505, 2500], 'required': 1570, 'adjustment': 1370, 'net_ar': 3768}], ['control 3', {'as_of': 100, 'invoices': [['I0', 40, 333], ['I1', 70, 150], ['I2', 99, 150], ['I3', 9, 333], ['I4', 100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [333, 300, 333, 0, 333], 'required': 318, 'adjustment': -182, 'net_ar': 981}], ['control 4', {'as_of': 100, 'invoices': [['I0', 40, 2500], ['I1', 99, 4000], ['I2', 9, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4000, 3500], 'required': 2250, 'adjustment': 2250, 'net_ar': 5250}], ['control 5', {'as_of': 100, 'invoices': [['I0', 70, 333], ['I1', 70, 150], ['I2', 54, 5], ['I3', 100, 2500], ['I4', 100, 1000], ['I5', 70, 5], ['I6', 54, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [3500, 488, 1005], 'required': 634, 'adjustment': 634, 'net_ar': 4359}], ['control 6', {'as_of': 100, 'invoices': [['I0', 69, 150], ['I1', 54, 150], ['I2', 54, 4000], ['I3', 70, 4000], ['I4', 40, 333], ['I5', 69, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 4000, 7133, 0, 0], 'required': 1270, 'adjustment': 1070, 'net_ar': 9863}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 99, 2500], ['I1', 69, 1000], ['I2', 55, 333], ['I3', 39, 1000], ['I4', 39, 333], ['I5', 40, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1', 'I4', 'I5']}, {'buckets': [0, 2500, 333, 1000, 0], 'required': 575, 'adjustment': 5708, 'net_ar': 3258}], ['control 1', {'as_of': 100, 'invoices': [['I0', 70, 5], ['I1', 69, 5], ['I2', 71, 333], ['I3', -100, 5], ['I4', 54, 333], ['I5', 100, 150], ['I6', 71, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0', 'I5']}, {'buckets': [0, 483, 338, 0, 5], 'required': 79, 'adjustment': 34, 'net_ar': 747}], ['control 2', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 69, 333], ['I2', 55, 150], ['I3', 70, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 633, 1000], 'required': 579, 'adjustment': 379, 'net_ar': 1054}], ['control 3', {'as_of': 100, 'invoices': [['I0', 39, 333], ['I1', 100, 1000], ['I2', 54, 2500], ['I3', -100, 2500], ['I4', 71, 150], ['I5', 70, 4000], ['I6', -100, 1000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [1000, 4150, 2500, 333, 3500], 'required': 3351, 'adjustment': 2851, 'net_ar': 8132}], ['control 4', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', -100, 2500], ['I2', 55, 5], ['I3', 39, 5], ['I4', 99, 4000], ['I5', 69, 2500], ['I6', 40, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 8000, 6505, 0, 2500], 'required': 3251, 'adjustment': 2756, 'net_ar': 13754}], ['control 5', {'as_of': 100, 'invoices': [['I0', 100, 5], ['I1', 69, 2500], ['I2', 9, 4000], ['I3', 39, 4000], ['I4', 99, 4000], ['I5', 39, 1000], ['I6', 100, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [10, 6500, 9000], 'required': 5313, 'adjustment': 5313, 'net_ar': 10197}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 150], ['I1', 55, 150], ['I2', -100, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I2']}, {'buckets': [0, 150, 150, 0, 0], 'required': 31, 'adjustment': -164, 'net_ar': 269}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 69, 4000], ['I1', 110, 4000], ['I2', 55, 1000], ['I3', 100, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 0, 5000, 0, 0], 'required': 790, 'adjustment': 4790, 'net_ar': 8210}], ['control 1', {'as_of': 100, 'invoices': [['I0', 54, 150], ['I1', 71, 4000], ['I2', 70, 2500], ['I3', 110, 150], ['I4', 99, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 6505, 150, 0, 0], 'required': 348, 'adjustment': -2, 'net_ar': 6307}], ['control 2', {'as_of': 100, 'invoices': [['I0', 100, 4000], ['I1', 69, 1000], ['I2', 55, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [4000, 5000, 0], 'required': 705, 'adjustment': 505, 'net_ar': 8295}], ['control 3', {'as_of': 100, 'invoices': [['I0', 100, 1000], ['I1', 54, 333], ['I2', 69, 1000], ['I3', 40, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [1000, 1000, 483], 'required': 387, 'adjustment': 187, 'net_ar': 2096}], ['control 4', {'as_of': 100, 'invoices': [['I0', 39, 2500], ['I1', 69, 150], ['I2', 55, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 333, 2500, 0], 'required': 1050, 'adjustment': 1000, 'net_ar': 1783}], ['control 5', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', 54, 1000], ['I2', 70, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 4005, 1000, 0, 0], 'required': 350, 'adjustment': 150, 'net_ar': 4655}], ['control 6', {'as_of': 100, 'invoices': [['I0', 9, 2500], ['I1', 10, 2500], ['I2', 55, 333], ['I3', 69, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 0, 338, 2500, 2500], 'required': 2926, 'adjustment': 2926, 'net_ar': 2412}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 110, 150], ['I1', 10, 1000], ['I2', 70, 150], ['I3', 99, 1000], ['I4', 100, 2500], ['I5', 55, 333], ['I6', 54, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [2650, 1150, 0, 1000, 0], 'required': 485, 'adjustment': 968, 'net_ar': 4315}], ['control 1', {'as_of': 100, 'invoices': [['I0', 69, 150], ['I1', 71, 150], ['I2', 70, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [0, 150, 150, 0, 0], 'required': 31, 'adjustment': -319, 'net_ar': 269}], ['control 2', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 100, 5], ['I2', 70, 150], ['I3', 110, 150], ['I4', 100, 2500], ['I5', -100, 333], ['I6', 100, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [2805, 150, 1000, 0, 333], 'required': 436, 'adjustment': 436, 'net_ar': 3852}], ['control 3', {'as_of': 100, 'invoices': [['I0', 71, 333], ['I1', 39, 150], ['I2', 71, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4333, 0, 150, 0], 'required': 277, 'adjustment': 277, 'net_ar': 4206}], ['control 4', {'as_of': 100, 'invoices': [['I0', 110, 2500], ['I1', 69, 150], ['I2', 54, 2500], ['I3', 10, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [2500, 0, 2500, 333, 0], 'required': 533, 'adjustment': 483, 'net_ar': 4800}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 69, 150], ['I2', 55, 5], ['I3', 71, 1000], ['I4', 55, 4000], ['I5', -100, 2500], ['I6', 69, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [0, 1000, 10655, 0, 2500], 'required': 3523, 'adjustment': 3023, 'net_ar': 10632}], ['control 6', {'as_of': 100, 'invoices': [['I0', 71, 150], ['I1', 10, 5], ['I2', 54, 4000], ['I3', 54, 2500], ['I4', 55, 1000], ['I5', 99, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4150, 7500, 5, 0], 'required': 1335, 'adjustment': 1335, 'net_ar': 10320}]], [['regression: adjustment against debit balance', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', 70, 2500], ['I2', 100, 4000], ['I3', 71, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 4005, 0], 'required': 581, 'adjustment': 3081, 'net_ar': 7424}], ['control 1', {'as_of': 100, 'invoices': [['I0', 70, 2500], ['I1', 110, 4000], ['I2', -100, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [4000, 2500, 0], 'required': 393, 'adjustment': 43, 'net_ar': 6107}], ['control 2', {'as_of': 100, 'invoices': [['I0', 40, 4000], ['I1', 39, 1000], ['I2', -100, 5], ['I3', 99, 5], ['I4', -100, 333], ['I5', 10, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 0, 4000, 5000, 338], 'required': 2854, 'adjustment': 2359, 'net_ar': 6484}], ['control 3', {'as_of': 100, 'invoices': [['I0', 54, 5], ['I1', 40, 5], ['I2', 99, 333], ['I3', 71, 150], ['I4', 54, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 483, 4010], 'required': 2065, 'adjustment': 2065, 'net_ar': 2428}], ['control 4', {'as_of': 100, 'invoices': [['I0', 39, 2500], ['I1', 39, 150], ['I2', 10, 4000], ['I3', 9, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 0, 0, 6650, 2500], 'required': 4535, 'adjustment': 4335, 'net_ar': 4615}], ['control 5', {'as_of': 100, 'invoices': [['I0', 71, 2500], ['I1', 54, 4000], ['I2', 40, 333], ['I3', 54, 4000], ['I4', 100, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': []}, {'buckets': [5, 2500, 8333, 0, 0], 'required': 1375, 'adjustment': 1175, 'net_ar': 9463}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 150], ['I1', 9, 4000], ['I2', -100, 2500], ['I3', 110, 333], ['I4', 9, 1000], ['I5', 69, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [333, 150, 5, 0, 7500], 'required': 5637, 'adjustment': 5137, 'net_ar': 2351}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
An allowance carrying a debit balance is under-adjusted.

x = {'as_of': day, 'invoices': [[id, due_day, open cents]], 'rates': [[max_days_past_due or None, basis points]] ascending, 'allowance': existing credit balance (negative = debit balance), 'writeoffs': ids}. Written-off invoices leave receivables and reduce the allowance by their amount. Days past due = max(0, as_of - due); an invoice goes in the first bucket whose limit is None or >= days. Required allowance = sum over buckets of bucket total * bp / 10000 rounded half-up per bucket. Return {'buckets', 'required', 'adjustment': required - allowance after write-offs, 'net_ar'}.

split: open-access


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):
    wo = set(x['writeoffs'])
    allowance = x['allowance']
    buckets = [0] * len(x['rates'])
    for iid, due, amt in x['invoices']:
        if iid in wo:
            allowance -= amt
            continue
        past = max(0, x['as_of'] - due)
        for k, (limit, bp) in enumerate(x['rates']):
            if limit is None or past <= limit:
                buckets[k] += amt
                break
    required = 0
    for total, (limit, bp) in zip(buckets, x['rates']):
        q, r = divmod(total * bp, 10000)
        required += q + (1 if 2 * r >= 10000 else 0)
    ar = sum(buckets)
    return {'buckets': buckets, 'required': required, 'adjustment': required - allowance, 'net_ar': sum(amt for _, _, amt in x['invoices']) - required}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: net receivable base', {'as_of': 100, 'invoices': [['I0', -100, 5], ['I1', 39, 333], ['I2', 9, 2500], ['I3', -100, 4000], ['I4', 69, 150], ['I5', 70, 2500], ['I6', 55, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [0, 0, 150, 333, 6505], 'required': 5035, 'adjustment': 11535, 'net_ar': 1953}], ['sample 1', {'as_of': 100, 'invoices': [['I0', 99, 4000], ['I1', 55, 150], ['I2', -100, 333], ['I3', 100, 150], ['I4', 100, 5], ['I5', 9, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I5']}, {'buckets': [155, 4000, 150, 0, 333], 'required': 475, 'adjustment': 4275, 'net_ar': 4163}], ['sample 2', {'as_of': 100, 'invoices': [['I0', 39, 4000], ['I1', 10, 150], ['I2', 110, 5], ['I3', 40, 5], ['I4', 99, 150], ['I5', 54, 4000], ['I6', 54, 1000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I6']}, {'buckets': [5, 150, 4005, 4150, 0], 'required': 2269, 'adjustment': 2769, 'net_ar': 6041}], ['sample 3', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 110, 150], ['I2', 100, 4000], ['I3', 40, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': []}, {'buckets': [4150, 0, 8000], 'required': 4083, 'adjustment': 4083, 'net_ar': 8067}], ['sample 4', {'as_of': 100, 'invoices': [['I0', 70, 1000], ['I1', 99, 5], ['I2', 99, 5], ['I3', 110, 5], ['I4', -100, 333], ['I5', -100, 1000], ['I6', 54, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I0', 'I1', 'I4']}, {'buckets': [5, 5, 4000, 0, 1000], 'required': 1350, 'adjustment': 2838, 'net_ar': 3660}], ['sample 5', {'as_of': 100, 'invoices': [['I0', 55, 2500], ['I1', 99, 4000], ['I2', 99, 150], ['I3', 55, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0']}, {'buckets': [0, 4150, 5, 0, 0], 'required': 209, 'adjustment': 2509, 'net_ar': 3946}], ['sample 6', {'as_of': 100, 'invoices': [['I0', 71, 4000], ['I1', 100, 333], ['I2', 100, 150], ['I3', 54, 5], ['I4', 39, 333]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [483, 4000, 338], 'required': 679, 'adjustment': 829, 'net_ar': 4142}]], [['regression: net receivable base', {'as_of': 100, 'invoices': [['I0', 99, 2500], ['I1', 69, 1000], ['I2', 55, 333], ['I3', 39, 1000], ['I4', 39, 333], ['I5', 40, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1', 'I4', 'I5']}, {'buckets': [0, 2500, 333, 1000, 0], 'required': 575, 'adjustment': 5708, 'net_ar': 3258}], ['sample 1', {'as_of': 100, 'invoices': [['I0', 99, 5], ['I1', 39, 5], ['I2', 10, 150], ['I3', -100, 333], ['I4', 39, 1000], ['I5', 39, 4000], ['I6', 100, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': ['I2', 'I3']}, {'buckets': [2500, 5, 5005], 'required': 2554, 'adjustment': 3187, 'net_ar': 4956}], ['sample 2', {'as_of': 100, 'invoices': [['I0', 54, 2500], ['I1', 55, 5], ['I2', 10, 1000], ['I3', 71, 333], ['I4', 71, 1000], ['I5', 100, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': ['I2', 'I5']}, {'buckets': [0, 1338, 2500], 'required': 1417, 'adjustment': 2572, 'net_ar': 2421}], ['sample 3', {'as_of': 100, 'invoices': [['I0', 55, 4000], ['I1', 40, 4000], ['I2', 40, 5], ['I3', 39, 4000], ['I4', 55, 1000], ['I5', 110, 150], ['I6', 100, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I1']}, {'buckets': [4150, 0, 5005, 4000, 0], 'required': 2393, 'adjustment': 5893, 'net_ar': 10762}], ['sample 4', {'as_of': 100, 'invoices': [['I0', 70, 5], ['I1', 69, 5], ['I2', 71, 333], ['I3', -100, 5], ['I4', 54, 333], ['I5', 100, 150], ['I6', 71, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0', 'I5']}, {'buckets': [0, 483, 338, 0, 5], 'required': 79, 'adjustment': 34, 'net_ar': 747}], ['sample 5', {'as_of': 100, 'invoices': [['I0', -100, 150], ['I1', 40, 1000], ['I2', 40, 5], ['I3', 39, 1000], ['I4', 9, 150], ['I5', 10, 1000], ['I6', 10, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': ['I2', 'I5']}, {'buckets': [0, 0, 2450], 'required': 1225, 'adjustment': 2030, 'net_ar': 1225}], ['sample 6', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 69, 333], ['I2', 55, 150], ['I3', 70, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [0, 633, 1000], 'required': 579, 'adjustment': 379, 'net_ar': 1054}]], [['regression: net receivable base', {'as_of': 100, 'invoices': [['I0', 54, 150], ['I1', 71, 4000], ['I2', 70, 2500], ['I3', 110, 150], ['I4', 99, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 6505, 150, 0, 0], 'required': 348, 'adjustment': -2, 'net_ar': 6307}], ['sample 1', {'as_of': 100, 'invoices': [['I0', 69, 4000], ['I1', 110, 4000], ['I2', 55, 1000], ['I3', 100, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 0, 5000, 0, 0], 'required': 790, 'adjustment': 4790, 'net_ar': 8210}], ['sample 2', {'as_of': 100, 'invoices': [['I0', 100, 4000], ['I1', 69, 1000], ['I2', 55, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [4000, 5000, 0], 'required': 705, 'adjustment': 505, 'net_ar': 8295}], ['sample 3', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 69, 5], ['I2', 110, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [150, 0, 5, 4000, 0], 'required': 1603, 'adjustment': 1753, 'net_ar': 2552}], ['sample 4', {'as_of': 100, 'invoices': [['I0', 100, 1000], ['I1', 54, 333], ['I2', 69, 1000], ['I3', 40, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': []}, {'buckets': [1000, 1000, 483], 'required': 387, 'adjustment': 187, 'net_ar': 2096}], ['sample 5', {'as_of': 100, 'invoices': [['I0', 39, 2500], ['I1', 69, 150], ['I2', 55, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 333, 2500, 0], 'required': 1050, 'adjustment': 1000, 'net_ar': 1783}], ['sample 6', {'as_of': 100, 'invoices': [['I0', 100, 150], ['I1', 55, 150], ['I2', 40, 1000], ['I3', 10, 1000], ['I4', 10, 4000], ['I5', 10, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I1']}, {'buckets': [150, 0, 1000, 5150, 0], 'required': 2212, 'adjustment': 2512, 'net_ar': 4088}]], [['regression: net receivable base', {'as_of': 100, 'invoices': [['I0', 110, 150], ['I1', 10, 1000], ['I2', 70, 150], ['I3', 99, 1000], ['I4', 100, 2500], ['I5', 55, 333], ['I6', 54, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5', 'I6']}, {'buckets': [2650, 1150, 0, 1000, 0], 'required': 485, 'adjustment': 968, 'net_ar': 4315}], ['sample 1', {'as_of': 100, 'invoices': [['I0', 70, 1000], ['I1', 10, 5], ['I2', 39, 150], ['I3', 54, 5], ['I4', 70, 2500], ['I5', 40, 150], ['I6', 39, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 200, 'writeoffs': ['I2', 'I4']}, {'buckets': [0, 1000, 165], 'required': 208, 'adjustment': 2658, 'net_ar': 957}], ['sample 2', {'as_of': 100, 'invoices': [['I0', 69, 150], ['I1', 71, 150], ['I2', 70, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [0, 150, 150, 0, 0], 'required': 31, 'adjustment': -319, 'net_ar': 269}], ['sample 3', {'as_of': 100, 'invoices': [['I0', 40, 1000], ['I1', 40, 333], ['I2', 70, 1000], ['I3', 70, 4000], ['I4', 69, 333], ['I5', 10, 4000], ['I6', 9, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 5000, 1666, 4000, 2500], 'required': 3975, 'adjustment': 4125, 'net_ar': 9191}], ['sample 4', {'as_of': 100, 'invoices': [['I0', 54, 1000], ['I1', 100, 5], ['I2', 70, 150], ['I3', 110, 150], ['I4', 100, 2500], ['I5', -100, 333], ['I6', 100, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [2805, 150, 1000, 0, 333], 'required': 436, 'adjustment': 436, 'net_ar': 3852}], ['sample 5', {'as_of': 100, 'invoices': [['I0', 54, 5], ['I1', 100, 333], ['I2', -100, 4000], ['I3', 9, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [333, 0, 5, 0, 8000], 'required': 6004, 'adjustment': 6154, 'net_ar': 2334}], ['sample 6', {'as_of': 100, 'invoices': [['I0', 71, 333], ['I1', 39, 150], ['I2', 71, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': []}, {'buckets': [0, 4333, 0, 150, 0], 'required': 277, 'adjustment': 277, 'net_ar': 4206}]], [['regression: net receivable base', {'as_of': 100, 'invoices': [['I0', 70, 4000], ['I1', 70, 2500], ['I2', 100, 4000], ['I3', 71, 5]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [4000, 4005, 0], 'required': 581, 'adjustment': 3081, 'net_ar': 7424}], ['control 1', {'as_of': 100, 'invoices': [['I0', 99, 1000], ['I1', 9, 333], ['I2', 70, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': []}, {'buckets': [0, 5000, 0, 0, 333], 'required': 500, 'adjustment': 0, 'net_ar': 4833}], ['sample 2', {'as_of': 100, 'invoices': [['I0', 70, 2500], ['I1', 110, 4000], ['I2', -100, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 500, 'writeoffs': ['I2']}, {'buckets': [4000, 2500, 0], 'required': 393, 'adjustment': 43, 'net_ar': 6107}], ['sample 3', {'as_of': 100, 'invoices': [['I0', -100, 4000], ['I1', 9, 4000], ['I2', 110, 1000], ['I3', 100, 333], ['I4', 9, 5], ['I5', 99, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I5']}, {'buckets': [1333, 0, 0, 0, 8005], 'required': 6017, 'adjustment': 6317, 'net_ar': 3321}], ['sample 4', {'as_of': 100, 'invoices': [['I0', 9, 150], ['I1', 9, 1000], ['I2', 54, 1000], ['I3', 99, 2500], ['I4', 69, 333], ['I5', 71, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 2505, 1333, 0, 1150], 'required': 1188, 'adjustment': 1338, 'net_ar': 3800}], ['sample 5', {'as_of': 100, 'invoices': [['I0', -100, 150], ['I1', 110, 150], ['I2', 100, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I0']}, {'buckets': [4150, 0, 0, 0, 0], 'required': 42, 'adjustment': 342, 'net_ar': 4108}], ['sample 6', {'as_of': 100, 'invoices': [['I0', 40, 4000], ['I1', 39, 1000], ['I2', -100, 5], ['I3', 99, 5], ['I4', -100, 333], ['I5', 10, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 500, 'writeoffs': ['I3']}, {'buckets': [0, 0, 4000, 5000, 338], 'required': 2854, 'adjustment': 2359, 'net_ar': 6484}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Net receivables use the pre-adjustment allowance.

x = {'as_of': day, 'invoices': [[id, due_day, open cents]], 'rates': [[max_days_past_due or None, basis points]] ascending, 'allowance': existing credit balance (negative = debit balance), 'writeoffs': ids}. Written-off invoices leave receivables and reduce the allowance by their amount. Days past due = max(0, as_of - due); an invoice goes in the first bucket whose limit is None or >= days. Required allowance = sum over buckets of bucket total * bp / 10000 rounded half-up per bucket. Return {'buckets', 'required', 'adjustment': required - allowance after write-offs, 'net_ar'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], -i))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: remainder tie-break', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 10, 'weights': [['mkt', 1], ['ops', 7], ['lab', 1], ['it', 5], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['ops', 'D', 5], ['lab', 'D', 1], ['it', 'D', 3]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 2', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 3', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 4', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 5', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: remainder tie-break', {'amount': 3, 'weights': [['it', 0], ['hr', 0], ['fin', 2], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 3], ['fin', 'D', 2], ['ops', 'D', 1]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 101, 'weights': [['it', 3], ['mkt', 0], ['lab', 7], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 101], ['it', 'D', 18], ['lab', 'D', 42], ['hr', 'D', 41]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]]], [['regression: remainder tie-break', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]], ['control 6', {'amount': 0, 'weights': [['it', 7], ['ops', 7], ['lab', 5], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: remainder tie-break', {'amount': -101, 'weights': [['fin', 7], ['mkt', 0], ['hr', 3], ['it', 7], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['fin', 'C', 30], ['hr', 'C', 13], ['it', 'C', 29], ['ops', 'C', 29]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 1', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 2', {'amount': -101, 'weights': [['it', 5], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['it', 'C', 42], ['ops', 'C', 59]]], ['control 3', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 4', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 5', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]]], [['regression: remainder tie-break', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]], ['regression: remainder tie-break, partial-repair probe', {'amount': -101, 'weights': [['ops', 2], ['hr', 2], ['lab', 7], ['it', 1], ['fin', 1]], 'source': 'pool'}, [['pool', 'D', 101], ['ops', 'C', 16], ['hr', 'C', 15], ['lab', 'C', 54], ['it', 'C', 8], ['fin', 'C', 8]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 4', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], ws[i][0]))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: remainder tie-break', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 10, 'weights': [['mkt', 1], ['ops', 7], ['lab', 1], ['it', 5], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['ops', 'D', 5], ['lab', 'D', 1], ['it', 'D', 3]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 2', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 3', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 4', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 5', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: remainder tie-break', {'amount': 3, 'weights': [['it', 0], ['hr', 0], ['fin', 2], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 3], ['fin', 'D', 2], ['ops', 'D', 1]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 101, 'weights': [['it', 3], ['mkt', 0], ['lab', 7], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 101], ['it', 'D', 18], ['lab', 'D', 42], ['hr', 'D', 41]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]]], [['regression: remainder tie-break', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]], ['control 6', {'amount': 0, 'weights': [['it', 7], ['ops', 7], ['lab', 5], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: remainder tie-break', {'amount': -101, 'weights': [['fin', 7], ['mkt', 0], ['hr', 3], ['it', 7], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['fin', 'C', 30], ['hr', 'C', 13], ['it', 'C', 29], ['ops', 'C', 29]]], ['regression: remainder tie-break, partial-repair probe', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 1', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 2', {'amount': -101, 'weights': [['it', 5], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['it', 'C', 42], ['ops', 'C', 59]]], ['control 3', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 4', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 5', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]]], [['regression: remainder tie-break', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]], ['regression: remainder tie-break, partial-repair probe', {'amount': -101, 'weights': [['ops', 2], ['hr', 2], ['lab', 7], ['it', 1], ['fin', 1]], 'source': 'pool'}, [['pool', 'D', 101], ['ops', 'C', 16], ['hr', 'C', 15], ['lab', 'C', 54], ['it', 'C', 8], ['fin', 'C', 8]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 4', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
The spare cent goes to the last of several equally entitled centers.

x = {'amount': signed cents, 'weights': [[center, non-negative weight]] (some positive), 'source': account}. The pool magnitude is split across positive-weight centers by the largest remainder method: floor shares, then one extra cent to the largest remainders, ties to the earlier-listed center. A non-negative amount credits the source and debits centers; a negative amount (reversal) debits the source and credits centers, all amounts positive. Return lines [[source, side, magnitude]] + [[center, side, share]] for nonzero shares in listing order.

split: open-access


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

N = 1
observations = []
def solve(x):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    base[max(range(len(ws)), key=lambda i: ws[i][1])] += left
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: leftover distribution', {'amount': 99999, 'weights': [['it', 7], ['lab', 2], ['ops', 5]], 'source': 'pool'}, [['pool', 'C', 99999], ['it', 'D', 49999], ['lab', 'D', 14286], ['ops', 'D', 35714]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: leftover distribution', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 1', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 2', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 3', {'amount': 100, 'weights': [['fin', 7], ['lab', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 4', {'amount': 0, 'weights': [['mkt', 1], ['ops', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': -101, 'weights': [['mkt', 7], ['fin', 1], ['hr', 1]], 'source': 'pool'}, [['pool', 'D', 101], ['mkt', 'C', 79], ['fin', 'C', 11], ['hr', 'C', 11]]], ['control 6', {'amount': 3, 'weights': [['it', 0], ['hr', 0], ['fin', 2], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 3], ['fin', 'D', 2], ['ops', 'D', 1]]]], [['regression: leftover distribution', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 4', {'amount': 0, 'weights': [['it', 7], ['ops', 7], ['lab', 5], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 7, 'weights': [['mkt', 0], ['ops', 1], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['ops', 'D', 1], ['fin', 'D', 6]]], ['control 6', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]]], [['regression: leftover distribution', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 1', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 2', {'amount': -101, 'weights': [['it', 5], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['it', 'C', 42], ['ops', 'C', 59]]], ['control 3', {'amount': 10, 'weights': [['ops', 1], ['mkt', 7], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 1], ['mkt', 'D', 8], ['fin', 'D', 1]]], ['control 4', {'amount': 101, 'weights': [['lab', 0], ['fin', 1], ['mkt', 1], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 25], ['mkt', 'D', 25], ['it', 'D', 51]]], ['control 5', {'amount': -7, 'weights': [['hr', 0], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 7]]], ['control 6', {'amount': 3, 'weights': [['ops', 7], ['hr', 1], ['fin', 1], ['mkt', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['ops', 'D', 2], ['mkt', 'D', 1]]]], [['regression: leftover distribution', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 3', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 4', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]], ['control 5', {'amount': 10, 'weights': [['ops', 5], ['fin', 7], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['fin', 'D', 6]]], ['control 6', {'amount': 7, 'weights': [['it', 0], ['lab', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 7]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    base[0] += left
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: leftover distribution', {'amount': 99999, 'weights': [['it', 7], ['lab', 2], ['ops', 5]], 'source': 'pool'}, [['pool', 'C', 99999], ['it', 'D', 49999], ['lab', 'D', 14286], ['ops', 'D', 35714]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: leftover distribution', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 1', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 2', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 3', {'amount': 100, 'weights': [['fin', 7], ['lab', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 4', {'amount': 0, 'weights': [['mkt', 1], ['ops', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': -101, 'weights': [['mkt', 7], ['fin', 1], ['hr', 1]], 'source': 'pool'}, [['pool', 'D', 101], ['mkt', 'C', 79], ['fin', 'C', 11], ['hr', 'C', 11]]], ['control 6', {'amount': 3, 'weights': [['it', 0], ['hr', 0], ['fin', 2], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 3], ['fin', 'D', 2], ['ops', 'D', 1]]]], [['regression: leftover distribution', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 4', {'amount': 0, 'weights': [['it', 7], ['ops', 7], ['lab', 5], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 7, 'weights': [['mkt', 0], ['ops', 1], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['ops', 'D', 1], ['fin', 'D', 6]]], ['control 6', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]]], [['regression: leftover distribution', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 1', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 2', {'amount': -101, 'weights': [['it', 5], ['ops', 7]], 'source': 'pool'}, [['pool', 'D', 101], ['it', 'C', 42], ['ops', 'C', 59]]], ['control 3', {'amount': 10, 'weights': [['ops', 1], ['mkt', 7], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 1], ['mkt', 'D', 8], ['fin', 'D', 1]]], ['control 4', {'amount': 101, 'weights': [['lab', 0], ['fin', 1], ['mkt', 1], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 25], ['mkt', 'D', 25], ['it', 'D', 51]]], ['control 5', {'amount': -7, 'weights': [['hr', 0], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 7]]], ['control 6', {'amount': 3, 'weights': [['ops', 7], ['hr', 1], ['fin', 1], ['mkt', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['ops', 'D', 2], ['mkt', 'D', 1]]]], [['regression: leftover distribution', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 3', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 4', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]], ['control 5', {'amount': 10, 'weights': [['ops', 5], ['fin', 7], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['fin', 'D', 6]]], ['control 6', {'amount': 7, 'weights': [['it', 0], ['lab', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 7]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
All rounding cents pile onto the heaviest cost center.

x = {'amount': signed cents, 'weights': [[center, non-negative weight]] (some positive), 'source': account}. The pool magnitude is split across positive-weight centers by the largest remainder method: floor shares, then one extra cent to the largest remainders, ties to the earlier-listed center. A non-negative amount credits the source and debits centers; a negative amount (reversal) debits the source and credits centers, all amounts positive. Return lines [[source, side, magnitude]] + [[center, side, share]] for nonzero shares in listing order.

split: open-access


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

N = 1
observations = []
def solve(x):
    amt = x['amount']
    side_src, side_dst = ('C', 'D')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: reversal sides', {'amount': -7, 'weights': [['ops', 0], ['mkt', 5], ['hr', 1], ['it', 2], ['lab', 0]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['hr', 'C', 1], ['it', 'C', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: reversal sides', {'amount': -101, 'weights': [['lab', 1], ['mkt', 5], ['ops', 2]], 'source': 'pool'}, [['pool', 'D', 101], ['lab', 'C', 13], ['mkt', 'C', 63], ['ops', 'C', 25]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]], ['control 6', {'amount': 1000, 'weights': [['mkt', 2], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 1000], ['mkt', 'D', 286], ['fin', 'D', 714]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 6', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 1', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 2', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 4', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]], ['control 5', {'amount': 99999, 'weights': [['hr', 0], ['lab', 0], ['ops', 3], ['mkt', 5], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 99999], ['ops', 'D', 30000], ['mkt', 'D', 49999], ['it', 'D', 20000]]], ['control 6', {'amount': 101, 'weights': [['mkt', 3], ['hr', 0], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['mkt', 'D', 38], ['fin', 'D', 63]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 4', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 5', {'amount': 101, 'weights': [['fin', 5], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 72], ['hr', 'D', 29]]], ['control 6', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    amt = x['amount']
    side_src, side_dst = ('D', 'C') if amt >= 0 else ('C', 'D')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [mag * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: reversal sides', {'amount': -7, 'weights': [['ops', 0], ['mkt', 5], ['hr', 1], ['it', 2], ['lab', 0]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['hr', 'C', 1], ['it', 'C', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: reversal sides', {'amount': -101, 'weights': [['lab', 1], ['mkt', 5], ['ops', 2]], 'source': 'pool'}, [['pool', 'D', 101], ['lab', 'C', 13], ['mkt', 'C', 63], ['ops', 'C', 25]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]], ['control 6', {'amount': 1000, 'weights': [['mkt', 2], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 1000], ['mkt', 'D', 286], ['fin', 'D', 714]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 6', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 1', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 2', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 4', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]], ['control 5', {'amount': 99999, 'weights': [['hr', 0], ['lab', 0], ['ops', 3], ['mkt', 5], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 99999], ['ops', 'D', 30000], ['mkt', 'D', 49999], ['it', 'D', 20000]]], ['control 6', {'amount': 101, 'weights': [['mkt', 3], ['hr', 0], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['mkt', 'D', 38], ['fin', 'D', 63]]]], [['regression: reversal sides', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 4', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 5', {'amount': 101, 'weights': [['fin', 5], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 72], ['hr', 'D', 29]]], ['control 6', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Reversing an allocation posts it a second time.

x = {'amount': signed cents, 'weights': [[center, non-negative weight]] (some positive), 'source': account}. The pool magnitude is split across positive-weight centers by the largest remainder method: floor shares, then one extra cent to the largest remainders, ties to the earlier-listed center. A non-negative amount credits the source and debits centers; a negative amount (reversal) debits the source and credits centers, all amounts positive. Return lines [[source, side, magnitude]] + [[center, side, share]] for nonzero shares in listing order.

split: open-access


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

N = 1
observations = []
def solve(x):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [amt * w // total for _, w in ws]
    rems = [amt * w % total for _, w in ws]
    left = amt - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: magnitude for shares', {'amount': -7, 'weights': [['ops', 0], ['mkt', 5], ['hr', 1], ['it', 2], ['lab', 0]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['hr', 'C', 1], ['it', 'C', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: magnitude for shares', {'amount': -101, 'weights': [['lab', 1], ['mkt', 5], ['ops', 2]], 'source': 'pool'}, [['pool', 'D', 101], ['lab', 'C', 13], ['mkt', 'C', 63], ['ops', 'C', 25]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]], ['control 6', {'amount': 1000, 'weights': [['mkt', 2], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 1000], ['mkt', 'D', 286], ['fin', 'D', 714]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 6', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 1', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 2', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 4', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]], ['control 5', {'amount': 99999, 'weights': [['hr', 0], ['lab', 0], ['ops', 3], ['mkt', 5], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 99999], ['ops', 'D', 30000], ['mkt', 'D', 49999], ['it', 'D', 20000]]], ['control 6', {'amount': 101, 'weights': [['mkt', 3], ['hr', 0], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['mkt', 'D', 38], ['fin', 'D', 63]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 4', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 5', {'amount': 101, 'weights': [['fin', 5], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 72], ['hr', 'D', 29]]], ['control 6', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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):
    amt = x['amount']
    side_src, side_dst = ('C', 'D') if amt >= 0 else ('D', 'C')
    mag = abs(amt)
    ws = [(c, w) for c, w in x['weights'] if w > 0]
    total = sum(w for _, w in ws)
    base = [amt * w // total for _, w in ws]
    rems = [mag * w % total for _, w in ws]
    left = mag - sum(base)
    order = sorted(range(len(ws)), key=lambda i: (-rems[i], i))
    for i in order[:left]:
        base[i] += 1
    lines = [[x['source'], side_src, mag]]
    for (c, w), share in zip(ws, base):
        if share:
            lines.append([c, side_dst, share])
    return lines
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: magnitude for shares', {'amount': -7, 'weights': [['ops', 0], ['mkt', 5], ['hr', 1], ['it', 2], ['lab', 0]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['hr', 'C', 1], ['it', 'C', 2]]], ['control 1', {'amount': 7, 'weights': [['fin', 0], ['it', 7], ['hr', 2], ['lab', 3], ['ops', 2]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['hr', 'D', 1], ['lab', 'D', 1], ['ops', 'D', 1]]], ['control 2', {'amount': 7, 'weights': [['mkt', 0], ['fin', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 7]]], ['control 3', {'amount': 99999, 'weights': [['hr', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 99999], ['hr', 'D', 99999]]], ['control 4', {'amount': 0, 'weights': [['ops', 3], ['mkt', 7], ['fin', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 5', {'amount': 100, 'weights': [['ops', 5], ['it', 5]], 'source': 'pool'}, [['pool', 'C', 100], ['ops', 'D', 50], ['it', 'D', 50]]], ['control 6', {'amount': 0, 'weights': [['ops', 5], ['hr', 5], ['mkt', 7], ['fin', 3]], 'source': 'pool'}, [['pool', 'C', 0]]]], [['regression: magnitude for shares', {'amount': -101, 'weights': [['lab', 1], ['mkt', 5], ['ops', 2]], 'source': 'pool'}, [['pool', 'D', 101], ['lab', 'C', 13], ['mkt', 'C', 63], ['ops', 'C', 25]]], ['control 1', {'amount': 7, 'weights': [['mkt', 0], ['it', 7], ['ops', 2], ['fin', 2], ['lab', 1]], 'source': 'pool'}, [['pool', 'C', 7], ['it', 'D', 4], ['ops', 'D', 1], ['fin', 'D', 1], ['lab', 'D', 1]]], ['control 2', {'amount': 0, 'weights': [['it', 3], ['lab', 1], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 0]]], ['control 3', {'amount': 101, 'weights': [['lab', 7], ['hr', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['lab', 'D', 59], ['hr', 'D', 42]]], ['control 4', {'amount': 10, 'weights': [['lab', 1], ['it', 3], ['hr', 1], ['ops', 7], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 2], ['hr', 'D', 1], ['ops', 'D', 6]]], ['control 5', {'amount': 10, 'weights': [['mkt', 1], ['it', 2], ['fin', 1], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 2], ['fin', 'D', 1], ['hr', 'D', 6]]], ['control 6', {'amount': 1000, 'weights': [['mkt', 2], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 1000], ['mkt', 'D', 286], ['fin', 'D', 714]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['mkt', 7], ['fin', 7]], 'source': 'pool'}, [['pool', 'D', 7], ['mkt', 'C', 4], ['fin', 'C', 3]]], ['control 1', {'amount': 7, 'weights': [['fin', 1], ['mkt', 5]], 'source': 'pool'}, [['pool', 'C', 7], ['fin', 'D', 1], ['mkt', 'D', 6]]], ['control 2', {'amount': 7, 'weights': [['it', 0], ['lab', 7], ['mkt', 1], ['hr', 3]], 'source': 'pool'}, [['pool', 'C', 7], ['lab', 'D', 4], ['mkt', 'D', 1], ['hr', 'D', 2]]], ['control 3', {'amount': 3, 'weights': [['it', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 3]]], ['control 4', {'amount': 10, 'weights': [['ops', 3], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['lab', 'D', 6]]], ['control 5', {'amount': 1000, 'weights': [['ops', 7], ['mkt', 5], ['fin', 7]], 'source': 'pool'}, [['pool', 'C', 1000], ['ops', 'D', 369], ['mkt', 'D', 263], ['fin', 'D', 368]]], ['control 6', {'amount': 10, 'weights': [['ops', 1], ['lab', 5]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 2], ['lab', 'D', 8]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['lab', 2], ['ops', 7], ['mkt', 2]], 'source': 'pool'}, [['pool', 'D', 7], ['lab', 'C', 1], ['ops', 'C', 5], ['mkt', 'C', 1]]], ['control 1', {'amount': 100, 'weights': [['it', 7], ['fin', 3], ['mkt', 7], ['hr', 1]], 'source': 'pool'}, [['pool', 'C', 100], ['it', 'D', 39], ['fin', 'D', 17], ['mkt', 'D', 39], ['hr', 'D', 5]]], ['control 2', {'amount': 10, 'weights': [['lab', 2], ['it', 5], ['hr', 7], ['mkt', 0], ['ops', 7]], 'source': 'pool'}, [['pool', 'C', 10], ['lab', 'D', 1], ['it', 'D', 3], ['hr', 'D', 3], ['ops', 'D', 3]]], ['control 3', {'amount': 1000, 'weights': [['fin', 7], ['mkt', 3], ['it', 3], ['lab', 1], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 1000], ['fin', 'D', 438], ['mkt', 'D', 188], ['it', 'D', 187], ['lab', 'D', 62], ['hr', 'D', 125]]], ['control 4', {'amount': 3, 'weights': [['it', 3], ['lab', 3], ['hr', 7]], 'source': 'pool'}, [['pool', 'C', 3], ['it', 'D', 1], ['lab', 'D', 1], ['hr', 'D', 1]]], ['control 5', {'amount': 99999, 'weights': [['hr', 0], ['lab', 0], ['ops', 3], ['mkt', 5], ['it', 2]], 'source': 'pool'}, [['pool', 'C', 99999], ['ops', 'D', 30000], ['mkt', 'D', 49999], ['it', 'D', 20000]]], ['control 6', {'amount': 101, 'weights': [['mkt', 3], ['hr', 0], ['fin', 5]], 'source': 'pool'}, [['pool', 'C', 101], ['mkt', 'D', 38], ['fin', 'D', 63]]]], [['regression: magnitude for shares', {'amount': -7, 'weights': [['fin', 3], ['mkt', 1]], 'source': 'pool'}, [['pool', 'D', 7], ['fin', 'C', 5], ['mkt', 'C', 2]]], ['control 1', {'amount': 100, 'weights': [['fin', 2], ['hr', 0], ['it', 0], ['mkt', 0]], 'source': 'pool'}, [['pool', 'C', 100], ['fin', 'D', 100]]], ['control 2', {'amount': 10, 'weights': [['ops', 3], ['it', 1], ['mkt', 3], ['hr', 0]], 'source': 'pool'}, [['pool', 'C', 10], ['ops', 'D', 4], ['it', 'D', 2], ['mkt', 'D', 4]]], ['control 3', {'amount': 7, 'weights': [['mkt', 1], ['it', 0]], 'source': 'pool'}, [['pool', 'C', 7], ['mkt', 'D', 7]]], ['control 4', {'amount': 10, 'weights': [['mkt', 1], ['it', 7], ['fin', 0], ['ops', 1]], 'source': 'pool'}, [['pool', 'C', 10], ['mkt', 'D', 1], ['it', 'D', 8], ['ops', 'D', 1]]], ['control 5', {'amount': 101, 'weights': [['fin', 5], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 72], ['hr', 'D', 29]]], ['control 6', {'amount': 101, 'weights': [['fin', 2], ['hr', 2]], 'source': 'pool'}, [['pool', 'C', 101], ['fin', 'D', 51], ['hr', 'D', 50]]]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, 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:
Reversal allocations produce negative or lopsided center shares.

x = {'amount': signed cents, 'weights': [[center, non-negative weight]] (some positive), 'source': account}. The pool magnitude is split across positive-weight centers by the largest remainder method: floor shares, then one extra cent to the largest remainders, ties to the earlier-listed center. A non-negative amount credits the source and debits centers; a negative amount (reversal) debits the source and credits centers, all amounts positive. Return lines [[source, side, magnitude]] + [[center, side, share]] for nonzero shares in listing order.

split: open-access