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 - allowance, 'net_ar': ar - required}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: write-off allowance sign', {'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', 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}], ['control 3', {'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 4', {'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 5', {'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 6', {'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}]], [['regression: write-off allowance sign', {'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', 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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 69, 1000], ['I1', 55, 2500], ['I2', 110, 1000], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [1000, 0, 3500, 0, 333], 'required': 785, 'adjustment': 935, 'net_ar': 4048}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 39, 5], ['I2', 99, 4000], ['I3', 100, 2500], ['I4', 70, 2500], ['I5', 70, 1000], ['I6', 55, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 8505, 5], 'required': 1116, 'adjustment': 1266, 'net_ar': 9894}], ['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', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}]], [['regression: write-off allowance sign', {'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}], ['regression: write-off allowance sign, partial-repair probe', {'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', 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 2', {'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}], ['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', 55, 2500], ['I1', 54, 4000], ['I2', 70, 333], ['I3', 70, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 5333, 4000], 'required': 2667, 'adjustment': 2817, 'net_ar': 6666}], ['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}]], [['regression: write-off allowance sign', {'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', 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}], ['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', 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}], ['control 4', {'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 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: write-off allowance sign', {'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', 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}], ['control 2', {'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 3', {'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 4', {'as_of': 100, 'invoices': [['I0', 9, 333], ['I1', 40, 1000], ['I2', 110, 5], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [5, 0, 1000, 0, 666], 'required': 650, 'adjustment': 800, 'net_ar': 1021}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 100, 4000], ['I2', 40, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [4000, 0, 2833, 0, 0], 'required': 465, 'adjustment': 615, 'net_ar': 6368}], ['control 6', {'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}]]]
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 if amt <= allowance else allowance
            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: write-off allowance sign', {'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', 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}], ['control 3', {'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 4', {'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 5', {'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 6', {'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}]], [['regression: write-off allowance sign', {'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', 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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 69, 1000], ['I1', 55, 2500], ['I2', 110, 1000], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [1000, 0, 3500, 0, 333], 'required': 785, 'adjustment': 935, 'net_ar': 4048}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 39, 5], ['I2', 99, 4000], ['I3', 100, 2500], ['I4', 70, 2500], ['I5', 70, 1000], ['I6', 55, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 8505, 5], 'required': 1116, 'adjustment': 1266, 'net_ar': 9894}], ['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', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}]], [['regression: write-off allowance sign', {'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}], ['regression: write-off allowance sign, partial-repair probe', {'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', 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 2', {'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}], ['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', 55, 2500], ['I1', 54, 4000], ['I2', 70, 333], ['I3', 70, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 5333, 4000], 'required': 2667, 'adjustment': 2817, 'net_ar': 6666}], ['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}]], [['regression: write-off allowance sign', {'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', 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}], ['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', 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}], ['control 4', {'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 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: write-off allowance sign', {'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', 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}], ['control 2', {'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 3', {'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 4', {'as_of': 100, 'invoices': [['I0', 9, 333], ['I1', 40, 1000], ['I2', 110, 5], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [5, 0, 1000, 0, 666], 'required': 650, 'adjustment': 800, 'net_ar': 1021}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 100, 4000], ['I2', 40, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [4000, 0, 2833, 0, 0], 'required': 465, 'adjustment': 615, 'net_ar': 6368}], ['control 6', {'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}]]]
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:
Writing off a bad debt increases the allowance and shrinks the period adjustment.

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):
    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
        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: write-off exclusion', {'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', 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}], ['control 3', {'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 4', {'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 5', {'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 6', {'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}]], [['regression: write-off exclusion', {'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', 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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 69, 1000], ['I1', 55, 2500], ['I2', 110, 1000], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [1000, 0, 3500, 0, 333], 'required': 785, 'adjustment': 935, 'net_ar': 4048}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 39, 5], ['I2', 99, 4000], ['I3', 100, 2500], ['I4', 70, 2500], ['I5', 70, 1000], ['I6', 55, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 8505, 5], 'required': 1116, 'adjustment': 1266, 'net_ar': 9894}], ['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', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}]], [['regression: write-off exclusion', {'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 1', {'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 2', {'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}], ['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', 55, 2500], ['I1', 54, 4000], ['I2', 70, 333], ['I3', 70, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 5333, 4000], 'required': 2667, 'adjustment': 2817, 'net_ar': 6666}], ['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: write-off exclusion', {'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', 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}], ['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', 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}], ['control 4', {'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 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: write-off exclusion', {'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', 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}], ['control 2', {'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 3', {'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 4', {'as_of': 100, 'invoices': [['I0', 9, 333], ['I1', 40, 1000], ['I2', 110, 5], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [5, 0, 1000, 0, 666], 'required': 650, 'adjustment': 800, 'net_ar': 1021}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 100, 4000], ['I2', 40, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [4000, 0, 2833, 0, 0], 'required': 465, 'adjustment': 615, 'net_ar': 6368}], ['control 6', {'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}]]]
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)

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:
            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: write-off exclusion', {'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', 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}], ['control 3', {'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 4', {'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 5', {'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 6', {'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}]], [['regression: write-off exclusion', {'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', 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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 69, 1000], ['I1', 55, 2500], ['I2', 110, 1000], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [1000, 0, 3500, 0, 333], 'required': 785, 'adjustment': 935, 'net_ar': 4048}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 39, 5], ['I2', 99, 4000], ['I3', 100, 2500], ['I4', 70, 2500], ['I5', 70, 1000], ['I6', 55, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 8505, 5], 'required': 1116, 'adjustment': 1266, 'net_ar': 9894}], ['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', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}]], [['regression: write-off exclusion', {'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 1', {'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 2', {'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}], ['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', 55, 2500], ['I1', 54, 4000], ['I2', 70, 333], ['I3', 70, 2500]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 5333, 4000], 'required': 2667, 'adjustment': 2817, 'net_ar': 6666}], ['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: write-off exclusion', {'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', 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}], ['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', 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}], ['control 4', {'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 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: write-off exclusion', {'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', 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}], ['control 2', {'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 3', {'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 4', {'as_of': 100, 'invoices': [['I0', 9, 333], ['I1', 40, 1000], ['I2', 110, 5], ['I3', -100, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [5, 0, 1000, 0, 666], 'required': 650, 'adjustment': 800, 'net_ar': 1021}], ['control 5', {'as_of': 100, 'invoices': [['I0', 69, 2500], ['I1', 100, 4000], ['I2', 40, 333]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [4000, 0, 2833, 0, 0], 'required': 465, 'adjustment': 615, 'net_ar': 6368}], ['control 6', {'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}]]]
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:
Written-off invoices are still aged and reserved.

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):
    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
    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: per-bucket rounding', {'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', 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}], ['control 3', {'as_of': 100, 'invoices': [['I0', 10, 1000], ['I1', 110, 2500], ['I2', 69, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 4000, 1000, 0], 'required': 1000, 'adjustment': 3300, 'net_ar': 4000}], ['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', 55, 2500], ['I1', 71, 1000], ['I2', 10, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [0, 0, 2500, 4000, 0], 'required': 1975, 'adjustment': 2975, 'net_ar': 4525}], ['control 6', {'as_of': 100, 'invoices': [['I0', 55, 4000], ['I1', 54, 333], ['I2', 40, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [0, 0, 6500, 0, 0], 'required': 975, 'adjustment': 1308, 'net_ar': 5525}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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}], ['control 1', {'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}], ['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', 55, 333], ['I1', 10, 150], ['I2', 99, 1000], ['I3', 54, 5], ['I4', 69, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0']}, {'buckets': [0, 1000, 155, 150, 0], 'required': 133, 'adjustment': 266, 'net_ar': 1172}], ['control 4', {'as_of': 100, 'invoices': [['I0', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}], ['control 5', {'as_of': 100, 'invoices': [['I0', 10, 333], ['I1', 110, 5], ['I2', -100, 1000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1', 'I2']}, {'buckets': [0, 0, 0, 333, 0], 'required': 133, 'adjustment': 938, 'net_ar': 200}]], [['regression: per-bucket rounding', {'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 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}], ['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', 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 4', {'as_of': 100, 'invoices': [['I0', 110, 5], ['I1', 10, 5], ['I2', 100, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I0', 'I2']}, {'buckets': [0, 0, 0, 5, 0], 'required': 2, 'adjustment': 2657, 'net_ar': 3}], ['control 5', {'as_of': 100, 'invoices': [['I0', 10, 1000], ['I1', 10, 150], ['I2', 39, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 0, 5000, 0], 'required': 2000, 'adjustment': 1950, 'net_ar': 3000}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 5], ['I1', 100, 4000], ['I2', 99, 5], ['I3', 54, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I3']}, {'buckets': [4000, 5, 0, 0, 0], 'required': 40, 'adjustment': 50, 'net_ar': 3965}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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 1', {'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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 9, 1000], ['I2', -100, 2500], ['I3', -100, 150], ['I4', 70, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I0']}, {'buckets': [0, 4000, 3650], 'required': 2325, 'adjustment': 6325, 'net_ar': 5325}], ['control 4', {'as_of': 100, 'invoices': [['I0', -100, 2500], ['I1', 54, 4000], ['I2', 55, 4000], ['I3', 9, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 4000, 7500], 'required': 4250, 'adjustment': 4400, 'net_ar': 7250}], ['control 5', {'as_of': 100, 'invoices': [['I0', 39, 333], ['I1', 39, 1000], ['I2', 100, 150], ['I3', 54, 4000], ['I4', -100, 5], ['I5', 71, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I5']}, {'buckets': [150, 0, 5338], 'required': 2672, 'adjustment': 2822, 'net_ar': 2816}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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}], ['control 1', {'as_of': 100, 'invoices': [['I0', 39, 5], ['I1', 99, 4000], ['I2', 55, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I1']}, {'buckets': [0, 0, 4000, 0, 0], 'required': 600, 'adjustment': 4605, 'net_ar': 3400}], ['control 2', {'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 3', {'as_of': 100, 'invoices': [['I0', -100, 2500], ['I1', 39, 4000], ['I2', 55, 1000], ['I3', 40, 4000], ['I4', 40, 4000], ['I5', 54, 2500], ['I6', 99, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5']}, {'buckets': [0, 4000, 9000, 4000, 2500], 'required': 5025, 'adjustment': 7525, 'net_ar': 14475}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 100, 150], ['I2', 99, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I1', 'I2']}, {'buckets': [0, 0, 0, 0, 0], 'required': 0, 'adjustment': 305, 'net_ar': 0}], ['control 5', {'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}]]]
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 r 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: per-bucket rounding', {'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', 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}], ['control 3', {'as_of': 100, 'invoices': [['I0', 10, 1000], ['I1', 110, 2500], ['I2', 69, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 4000, 1000, 0], 'required': 1000, 'adjustment': 3300, 'net_ar': 4000}], ['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', 55, 2500], ['I1', 71, 1000], ['I2', 10, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [0, 0, 2500, 4000, 0], 'required': 1975, 'adjustment': 2975, 'net_ar': 4525}], ['control 6', {'as_of': 100, 'invoices': [['I0', 55, 4000], ['I1', 54, 333], ['I2', 40, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I1']}, {'buckets': [0, 0, 6500, 0, 0], 'required': 975, 'adjustment': 1308, 'net_ar': 5525}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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}], ['control 1', {'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}], ['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', 55, 333], ['I1', 10, 150], ['I2', 99, 1000], ['I3', 54, 5], ['I4', 69, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I0']}, {'buckets': [0, 1000, 155, 150, 0], 'required': 133, 'adjustment': 266, 'net_ar': 1172}], ['control 4', {'as_of': 100, 'invoices': [['I0', 9, 1000], ['I1', 55, 2500], ['I2', 110, 2500], ['I3', 70, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': []}, {'buckets': [2500, 2500, 2500, 0, 1000], 'required': 1275, 'adjustment': 1425, 'net_ar': 7225}], ['control 5', {'as_of': 100, 'invoices': [['I0', 10, 333], ['I1', 110, 5], ['I2', -100, 1000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1', 'I2']}, {'buckets': [0, 0, 0, 333, 0], 'required': 133, 'adjustment': 938, 'net_ar': 200}]], [['regression: per-bucket rounding', {'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 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}], ['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', 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 4', {'as_of': 100, 'invoices': [['I0', 110, 5], ['I1', 10, 5], ['I2', 100, 2500]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': -150, 'writeoffs': ['I0', 'I2']}, {'buckets': [0, 0, 0, 5, 0], 'required': 2, 'adjustment': 2657, 'net_ar': 3}], ['control 5', {'as_of': 100, 'invoices': [['I0', 10, 1000], ['I1', 10, 150], ['I2', 39, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 200, 'writeoffs': ['I1']}, {'buckets': [0, 0, 0, 5000, 0], 'required': 2000, 'adjustment': 1950, 'net_ar': 3000}], ['control 6', {'as_of': 100, 'invoices': [['I0', 70, 5], ['I1', 100, 4000], ['I2', 99, 5], ['I3', 54, 5]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I3']}, {'buckets': [4000, 5, 0, 0, 0], 'required': 40, 'adjustment': 50, 'net_ar': 3965}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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 1', {'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 2', {'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 3', {'as_of': 100, 'invoices': [['I0', 10, 4000], ['I1', 9, 1000], ['I2', -100, 2500], ['I3', -100, 150], ['I4', 70, 4000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I0']}, {'buckets': [0, 4000, 3650], 'required': 2325, 'adjustment': 6325, 'net_ar': 5325}], ['control 4', {'as_of': 100, 'invoices': [['I0', -100, 2500], ['I1', 54, 4000], ['I2', 55, 4000], ['I3', 9, 1000]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': -150, 'writeoffs': []}, {'buckets': [0, 4000, 7500], 'required': 4250, 'adjustment': 4400, 'net_ar': 7250}], ['control 5', {'as_of': 100, 'invoices': [['I0', 39, 333], ['I1', 39, 1000], ['I2', 100, 150], ['I3', 54, 4000], ['I4', -100, 5], ['I5', 71, 150]], 'rates': [[0, 200], [45, 1250], [None, 5000]], 'allowance': 0, 'writeoffs': ['I5']}, {'buckets': [150, 0, 5338], 'required': 2672, 'adjustment': 2822, 'net_ar': 2816}]], [['regression: per-bucket rounding', {'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}], ['regression: per-bucket rounding, partial-repair probe', {'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}], ['control 1', {'as_of': 100, 'invoices': [['I0', 39, 5], ['I1', 99, 4000], ['I2', 55, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I1']}, {'buckets': [0, 0, 4000, 0, 0], 'required': 600, 'adjustment': 4605, 'net_ar': 3400}], ['control 2', {'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 3', {'as_of': 100, 'invoices': [['I0', -100, 2500], ['I1', 39, 4000], ['I2', 55, 1000], ['I3', 40, 4000], ['I4', 40, 4000], ['I5', 54, 2500], ['I6', 99, 4000]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I5']}, {'buckets': [0, 4000, 9000, 4000, 2500], 'required': 5025, 'adjustment': 7525, 'net_ar': 14475}], ['control 4', {'as_of': 100, 'invoices': [['I0', 55, 5], ['I1', 100, 150], ['I2', 99, 150]], 'rates': [[0, 100], [30, 500], [60, 1500], [90, 4000], [None, 7500]], 'allowance': 0, 'writeoffs': ['I0', 'I1', 'I2']}, {'buckets': [0, 0, 0, 0, 0], 'required': 0, 'adjustment': 305, 'net_ar': 0}], ['control 5', {'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}]]]
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:
Required allowance is a few cents short.

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