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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus > x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [7, 120000, 7], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20002, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 2', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['control 3', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 4', {'disbursements': [36000, 120000, 7], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 26001, 'shortage': 31001, 'refund': 0, 'monthly': 15585}], ['control 5', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 1', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['control 2', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [250000, 99999, 120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 195833, 'shortage': 45833, 'refund': 0, 'monthly': 42987}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 5000, 'monthly': 37500}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 13000, 'cushion': 65000, 'shortage': 0, 'refund': 0, 'monthly': 11750}], ['control 1', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['control 2', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 3', {'disbursements': [7, 250000], 'low_point': 80000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20834, 'cushion': 0, 'shortage': 0, 'refund': 80000, 'monthly': 20834}], ['control 4', {'disbursements': [7, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 3001, 'cushion': 3001, 'shortage': 0, 'refund': 76999, 'monthly': 3001}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [99999, 250000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 29167, 'cushion': 145833, 'shortage': 0, 'refund': 0, 'monthly': 28820}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['control 3', {'disbursements': [36000, 7], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 3001, 'cushion': 15003, 'shortage': 0, 'refund': 134997, 'monthly': 3001}], ['control 4', {'disbursements': [120000, 250000, 99999], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 39167, 'shortage': 0, 'refund': 40833, 'monthly': 39167}], ['control 5', {'disbursements': [36000, 7, 250000], 'low_point': 150000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23834, 'cushion': 47668, 'shortage': 0, 'refund': 102332, 'monthly': 23834}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 0, 'monthly': 37084}], ['control 1', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 2', {'disbursements': [7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 1, 'cushion': 3, 'shortage': 0, 'refund': 79997, 'monthly': 1}], ['control 3', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 4', {'disbursements': [120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 20001, 'refund': 0, 'monthly': 11668}]]]
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):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if x['low_point'] >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [7, 120000, 7], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20002, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 2', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['control 3', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 4', {'disbursements': [36000, 120000, 7], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 26001, 'shortage': 31001, 'refund': 0, 'monthly': 15585}], ['control 5', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 1', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['control 2', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [250000, 99999, 120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 195833, 'shortage': 45833, 'refund': 0, 'monthly': 42987}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 5000, 'monthly': 37500}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 13000, 'cushion': 65000, 'shortage': 0, 'refund': 0, 'monthly': 11750}], ['control 1', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['control 2', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 3', {'disbursements': [7, 250000], 'low_point': 80000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20834, 'cushion': 0, 'shortage': 0, 'refund': 80000, 'monthly': 20834}], ['control 4', {'disbursements': [7, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 3001, 'cushion': 3001, 'shortage': 0, 'refund': 76999, 'monthly': 3001}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [99999, 250000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 29167, 'cushion': 145833, 'shortage': 0, 'refund': 0, 'monthly': 28820}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['control 3', {'disbursements': [36000, 7], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 3001, 'cushion': 15003, 'shortage': 0, 'refund': 134997, 'monthly': 3001}], ['control 4', {'disbursements': [120000, 250000, 99999], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 39167, 'shortage': 0, 'refund': 40833, 'monthly': 39167}], ['control 5', {'disbursements': [36000, 7, 250000], 'low_point': 150000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23834, 'cushion': 47668, 'shortage': 0, 'refund': 102332, 'monthly': 23834}]], [['regression: surplus exactly at threshold', {'disbursements': [120000], 'low_point': 25000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 5000, 'monthly': 10000}], ['regression: refund threshold test', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['regression: refund threshold test, partial-repair probe', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 0, 'monthly': 37084}], ['control 1', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 2', {'disbursements': [7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 1, 'cushion': 3, 'shortage': 0, 'refund': 79997, 'monthly': 1}], ['control 3', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 4', {'disbursements': [120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 20001, 'refund': 0, 'monthly': 11668}]]]
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:
A surplus exactly at the threshold is not refunded.

x = {'disbursements': annual amounts, 'low_point': projected lowest balance, 'cushion_months', 'refund_threshold'}. total = sum; base = round_half_up(total/12); cushion = round_half_up(total*cushion_months/12); diff = low_point - cushion; shortage = max(0, -diff) is collected over 12 months rounded up; surplus = max(0, diff) is refunded when >= refund_threshold, otherwise floor(surplus/12) is credited monthly. Return {'base', 'cushion', 'shortage', 'refund', 'monthly'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - surplus // 12
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: credit after refund', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['regression: credit after refund, partial-repair probe', {'disbursements': [7, 120000, 7], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20002, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 2', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 3', {'disbursements': [36000, 120000, 7], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 26001, 'shortage': 31001, 'refund': 0, 'monthly': 15585}], ['control 4', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}], ['control 5', {'disbursements': [36000, 250000, 250000], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 44667, 'cushion': 89333, 'shortage': 94333, 'refund': 0, 'monthly': 52529}]], [['regression: credit after refund', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['regression: credit after refund, partial-repair probe', {'disbursements': [7, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 2', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [250000, 99999, 120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 195833, 'shortage': 45833, 'refund': 0, 'monthly': 42987}], ['control 5', {'disbursements': [250000, 120000, 99999], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 39167, 'shortage': 44167, 'refund': 0, 'monthly': 42848}]], [['regression: credit after refund', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['regression: credit after refund, partial-repair probe', {'disbursements': [250000, 36000, 120000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 33833, 'cushion': 67667, 'shortage': 0, 'refund': 0, 'monthly': 32806}], ['control 1', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 2', {'disbursements': [120000, 36000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 3', {'disbursements': [120000, 120000, 7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 20001, 'cushion': 100003, 'shortage': 20003, 'refund': 0, 'monthly': 21668}], ['control 4', {'disbursements': [120000, 120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20001, 'cushion': 40001, 'shortage': 40001, 'refund': 0, 'monthly': 23335}], ['control 5', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 8333, 'shortage': 13333, 'refund': 0, 'monthly': 9445}]], [['regression: credit after refund', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['regression: credit after refund, partial-repair probe', {'disbursements': [250000, 99999], 'low_point': 40000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 0, 'monthly': 28265}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 6000, 'refund': 0, 'monthly': 3500}], ['control 3', {'disbursements': [36000, 250000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 23833, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 24250}], ['control 4', {'disbursements': [36000, 120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 5', {'disbursements': [7, 250000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20835, 'cushion': 41669, 'shortage': 41669, 'refund': 0, 'monthly': 24308}]], [['regression: credit after refund', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['regression: credit after refund, partial-repair probe', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 0, 'monthly': 37084}], ['control 1', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 2', {'disbursements': [120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 20001, 'refund': 0, 'monthly': 11668}], ['control 3', {'disbursements': [120000, 7], 'low_point': 40000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 50003, 'shortage': 10003, 'refund': 0, 'monthly': 10835}], ['control 4', {'disbursements': [36000, 7, 36000], 'low_point': 10000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 6001, 'cushion': 30003, 'shortage': 20003, 'refund': 0, 'monthly': 7668}], ['control 5', {'disbursements': [250000, 36000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23833, 'cushion': 47667, 'shortage': 7667, 'refund': 0, 'monthly': 24472}]]]
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):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else rnd(surplus, 12))
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: credit after refund', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['regression: credit after refund, partial-repair probe', {'disbursements': [7, 120000, 7], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20002, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 2', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 3', {'disbursements': [36000, 120000, 7], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 26001, 'shortage': 31001, 'refund': 0, 'monthly': 15585}], ['control 4', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}], ['control 5', {'disbursements': [36000, 250000, 250000], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 44667, 'cushion': 89333, 'shortage': 94333, 'refund': 0, 'monthly': 52529}]], [['regression: credit after refund', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['regression: credit after refund, partial-repair probe', {'disbursements': [7, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 0, 'refund': 0, 'monthly': 8335}], ['control 1', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 2', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [250000, 99999, 120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 195833, 'shortage': 45833, 'refund': 0, 'monthly': 42987}], ['control 5', {'disbursements': [250000, 120000, 99999], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 39167, 'cushion': 39167, 'shortage': 44167, 'refund': 0, 'monthly': 42848}]], [['regression: credit after refund', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['regression: credit after refund, partial-repair probe', {'disbursements': [250000, 36000, 120000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 33833, 'cushion': 67667, 'shortage': 0, 'refund': 0, 'monthly': 32806}], ['control 1', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 2', {'disbursements': [120000, 36000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 3', {'disbursements': [120000, 120000, 7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 20001, 'cushion': 100003, 'shortage': 20003, 'refund': 0, 'monthly': 21668}], ['control 4', {'disbursements': [120000, 120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20001, 'cushion': 40001, 'shortage': 40001, 'refund': 0, 'monthly': 23335}], ['control 5', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 8333, 'shortage': 13333, 'refund': 0, 'monthly': 9445}]], [['regression: credit after refund', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['regression: credit after refund, partial-repair probe', {'disbursements': [250000, 99999], 'low_point': 40000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 0, 'monthly': 28265}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 6000, 'refund': 0, 'monthly': 3500}], ['control 3', {'disbursements': [36000, 250000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 23833, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 24250}], ['control 4', {'disbursements': [36000, 120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 5', {'disbursements': [7, 250000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20835, 'cushion': 41669, 'shortage': 41669, 'refund': 0, 'monthly': 24308}]], [['regression: credit after refund', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['regression: credit after refund, partial-repair probe', {'disbursements': [99999, 99999, 250000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 37500, 'cushion': 75000, 'shortage': 0, 'refund': 0, 'monthly': 37084}], ['control 1', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 2', {'disbursements': [120000, 7], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 20001, 'shortage': 20001, 'refund': 0, 'monthly': 11668}], ['control 3', {'disbursements': [120000, 7], 'low_point': 40000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10001, 'cushion': 50003, 'shortage': 10003, 'refund': 0, 'monthly': 10835}], ['control 4', {'disbursements': [36000, 7, 36000], 'low_point': 10000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 6001, 'cushion': 30003, 'shortage': 20003, 'refund': 0, 'monthly': 7668}], ['control 5', {'disbursements': [250000, 36000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23833, 'cushion': 47667, 'shortage': 7667, 'refund': 0, 'monthly': 24472}]]]
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:
Borrowers receive a refund and a monthly credit for the same surplus.

x = {'disbursements': annual amounts, 'low_point': projected lowest balance, 'cushion_months', 'refund_threshold'}. total = sum; base = round_half_up(total/12); cushion = round_half_up(total*cushion_months/12); diff = low_point - cushion; shortage = max(0, -diff) is collected over 12 months rounded up; surplus = max(0, diff) is refunded when >= refund_threshold, otherwise floor(surplus/12) is credited monthly. Return {'base', 'cushion', 'shortage', 'refund', 'monthly'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = total // 12
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: monthly base rounding', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 1', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 2', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}], ['control 3', {'disbursements': [36000, 36000, 120000], 'low_point': 150000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 16000, 'cushion': 32000, 'shortage': 0, 'refund': 118000, 'monthly': 16000}], ['control 4', {'disbursements': [99999], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 16667, 'shortage': 6667, 'refund': 0, 'monthly': 8889}], ['control 5', {'disbursements': [36000, 36000, 99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 14333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 14750}]], [['regression: monthly base rounding', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [250000, 7, 99999], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 29167, 'refund': 0, 'monthly': 31598}], ['control 1', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['control 2', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 140000, 'monthly': 10000}], ['control 5', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}]], [['regression: monthly base rounding', {'disbursements': [7, 250000], 'low_point': 80000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20834, 'cushion': 0, 'shortage': 0, 'refund': 80000, 'monthly': 20834}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['control 1', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 2', {'disbursements': [120000, 36000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 3', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['control 4', {'disbursements': [7, 36000, 7], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 3001, 'cushion': 3001, 'shortage': 0, 'refund': 76999, 'monthly': 3001}], ['control 5', {'disbursements': [36000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 74000, 'monthly': 3000}]], [['regression: monthly base rounding', {'disbursements': [36000, 7], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 3001, 'cushion': 15003, 'shortage': 0, 'refund': 134997, 'monthly': 3001}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 4000, 'monthly': 3000}], ['control 3', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 6000, 'refund': 0, 'monthly': 3500}], ['control 4', {'disbursements': [99999], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 8333, 'shortage': 0, 'refund': 141667, 'monthly': 8333}], ['control 5', {'disbursements': [36000, 99999], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 11333, 'cushion': 56666, 'shortage': 0, 'refund': 93334, 'monthly': 11333}]], [['regression: monthly base rounding', {'disbursements': [7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 1, 'cushion': 3, 'shortage': 0, 'refund': 79997, 'monthly': 1}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 1', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 2', {'disbursements': [120000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 30000, 'monthly': 10000}], ['control 3', {'disbursements': [120000, 36000, 99999], 'low_point': 40000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 21333, 'cushion': 0, 'shortage': 0, 'refund': 40000, 'monthly': 21333}], ['control 4', {'disbursements': [36000, 99999], 'low_point': 40000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 28667, 'monthly': 11333}], ['control 5', {'disbursements': [250000, 36000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23833, 'cushion': 47667, 'shortage': 7667, 'refund': 0, 'monthly': 24472}]]]
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):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = (total + 11) // 12
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - cushion
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: monthly base rounding', {'disbursements': [99999, 250000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 29167, 'cushion': 29167, 'shortage': 0, 'refund': 50833, 'monthly': 29167}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 1', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 10000, 'refund': 0, 'monthly': 10834}], ['control 2', {'disbursements': [99999, 250000, 7], 'low_point': -5000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 34167, 'refund': 0, 'monthly': 32015}], ['control 3', {'disbursements': [36000, 36000, 120000], 'low_point': 150000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 16000, 'cushion': 32000, 'shortage': 0, 'refund': 118000, 'monthly': 16000}], ['control 4', {'disbursements': [99999], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 16667, 'shortage': 6667, 'refund': 0, 'monthly': 8889}], ['control 5', {'disbursements': [36000, 36000, 99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 14333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 14750}]], [['regression: monthly base rounding', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [250000, 7, 99999], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 29167, 'cushion': 29167, 'shortage': 29167, 'refund': 0, 'monthly': 31598}], ['control 1', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['control 2', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 3', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 4', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 140000, 'monthly': 10000}], ['control 5', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}]], [['regression: monthly base rounding', {'disbursements': [7, 250000], 'low_point': 80000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20834, 'cushion': 0, 'shortage': 0, 'refund': 80000, 'monthly': 20834}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['control 1', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 2', {'disbursements': [120000, 36000], 'low_point': 0, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 13000, 'refund': 0, 'monthly': 14084}], ['control 3', {'disbursements': [120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 20000, 'shortage': 0, 'refund': 20000, 'monthly': 10000}], ['control 4', {'disbursements': [7, 36000, 7], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 3001, 'cushion': 3001, 'shortage': 0, 'refund': 76999, 'monthly': 3001}], ['control 5', {'disbursements': [36000], 'low_point': 80000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 74000, 'monthly': 3000}]], [['regression: monthly base rounding', {'disbursements': [36000, 7], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 3001, 'cushion': 15003, 'shortage': 0, 'refund': 134997, 'monthly': 3001}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [99999, 36000], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 138667, 'monthly': 11333}], ['control 1', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 2', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 4000, 'monthly': 3000}], ['control 3', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 3000, 'cushion': 6000, 'shortage': 6000, 'refund': 0, 'monthly': 3500}], ['control 4', {'disbursements': [99999], 'low_point': 150000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 8333, 'cushion': 8333, 'shortage': 0, 'refund': 141667, 'monthly': 8333}], ['control 5', {'disbursements': [36000, 99999], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 0}, {'base': 11333, 'cushion': 56666, 'shortage': 0, 'refund': 93334, 'monthly': 11333}]], [['regression: monthly base rounding', {'disbursements': [7], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 1, 'cushion': 3, 'shortage': 0, 'refund': 79997, 'monthly': 1}], ['regression: monthly base rounding, partial-repair probe', {'disbursements': [99999], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 8750}], ['control 1', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 2', {'disbursements': [120000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 30000, 'monthly': 10000}], ['control 3', {'disbursements': [120000, 36000, 99999], 'low_point': 40000, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 21333, 'cushion': 0, 'shortage': 0, 'refund': 40000, 'monthly': 21333}], ['control 4', {'disbursements': [36000, 99999], 'low_point': 40000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 11333, 'cushion': 11333, 'shortage': 0, 'refund': 28667, 'monthly': 11333}], ['control 5', {'disbursements': [250000, 36000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 23833, 'cushion': 47667, 'shortage': 7667, 'refund': 0, 'monthly': 24472}]]]
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 monthly escrow deposit is a cent short for many tax bills.

x = {'disbursements': annual amounts, 'low_point': projected lowest balance, 'cushion_months', 'refund_threshold'}. total = sum; base = round_half_up(total/12); cushion = round_half_up(total*cushion_months/12); diff = low_point - cushion; shortage = max(0, -diff) is collected over 12 months rounded up; surplus = max(0, diff) is refunded when >= refund_threshold, otherwise floor(surplus/12) is credited monthly. Return {'base', 'cushion', 'shortage', 'refund', 'monthly'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = cushion - x['low_point']
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: low point comparison', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 1', {'disbursements': [36000, 7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 6001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 6001}], ['control 2', {'disbursements': [7, 36000, 250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 23834, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 23834}], ['control 3', {'disbursements': [120000, 7, 7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 10001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10001}], ['control 4', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 5', {'disbursements': [120000, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 28333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 28333}], ['control 6', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10000}]], [['regression: low point comparison', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['regression: low point comparison, partial-repair probe', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 1', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 2', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 3', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 4', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 5', {'disbursements': [7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 1, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 1}]], [['regression: low point comparison', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['regression: low point comparison, partial-repair probe', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 1', {'disbursements': [36000, 99999, 250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 32167, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 32167}], ['control 2', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 3', {'disbursements': [120000, 7, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 18334, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 18334}], ['control 4', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 5', {'disbursements': [250000, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 39167, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 39167}]], [['regression: low point comparison', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 1', {'disbursements': [120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 18333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 18333}], ['control 2', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 3', {'disbursements': [36000, 7, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 13001}], ['control 4', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 5', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 6', {'disbursements': [99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 8333}]], [['regression: low point comparison', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 1', {'disbursements': [7, 7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 1, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 1}], ['control 2', {'disbursements': [7, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10001}], ['control 3', {'disbursements': [250000, 250000, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 51667, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 51667}], ['control 4', {'disbursements': [99999, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 26667, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 26667}], ['control 5', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3000}], ['control 6', {'disbursements': [36000, 120000, 36000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 16000, 'cushion': 80000, 'shortage': 0, 'refund': 0, 'monthly': 16000}]]]
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):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    total = sum(x['disbursements'])
    base = rnd(total, 12)
    cushion = rnd(total * x['cushion_months'], 12)
    diff = x['low_point'] - base
    shortage = max(0, -diff)
    surplus = max(0, diff)
    refund = surplus if surplus >= x['refund_threshold'] else 0
    monthly = base + (shortage + 11) // 12 - (0 if refund else surplus // 12)
    return {'base': base, 'cushion': cushion, 'shortage': shortage, 'refund': refund, 'monthly': monthly}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: low point comparison', {'disbursements': [120000, 36000, 99999], 'low_point': -5000, 'cushion_months': 2, 'refund_threshold': 20000}, {'base': 21333, 'cushion': 42667, 'shortage': 47667, 'refund': 0, 'monthly': 25306}], ['control 1', {'disbursements': [36000, 7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 6001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 6001}], ['control 2', {'disbursements': [7, 36000, 250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 23834, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 23834}], ['control 3', {'disbursements': [120000, 7, 7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 10001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10001}], ['control 4', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 0}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 5', {'disbursements': [120000, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 28333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 28333}], ['control 6', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10000}]], [['regression: low point comparison', {'disbursements': [120000, 36000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 0}, {'base': 13000, 'cushion': 13000, 'shortage': 0, 'refund': 67000, 'monthly': 13000}], ['regression: low point comparison, partial-repair probe', {'disbursements': [36000], 'low_point': 10000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 6000, 'shortage': 0, 'refund': 0, 'monthly': 2667}], ['control 1', {'disbursements': [7, 36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3001}], ['control 2', {'disbursements': [120000, 120000], 'low_point': 40000, 'cushion_months': 2, 'refund_threshold': 5000}, {'base': 20000, 'cushion': 40000, 'shortage': 0, 'refund': 0, 'monthly': 20000}], ['control 3', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 4', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 5', {'disbursements': [7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 1, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 1}]], [['regression: low point comparison', {'disbursements': [250000, 120000], 'low_point': 80000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 30833, 'cushion': 30833, 'shortage': 0, 'refund': 49167, 'monthly': 30833}], ['regression: low point comparison, partial-repair probe', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 5, 'refund_threshold': 5000}, {'base': 3000, 'cushion': 15000, 'shortage': 15000, 'refund': 0, 'monthly': 4250}], ['control 1', {'disbursements': [36000, 99999, 250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 32167, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 32167}], ['control 2', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 3', {'disbursements': [120000, 7, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 18334, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 18334}], ['control 4', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 5', {'disbursements': [250000, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 39167, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 39167}]], [['regression: low point comparison', {'disbursements': [36000], 'low_point': -5000, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 5000, 'refund': 0, 'monthly': 3417}], ['control 1', {'disbursements': [120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 5000}, {'base': 18333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 18333}], ['control 2', {'disbursements': [250000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 20833, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 20833}], ['control 3', {'disbursements': [36000, 7, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 13001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 13001}], ['control 4', {'disbursements': [120000], 'low_point': 10000, 'cushion_months': 1, 'refund_threshold': 5000}, {'base': 10000, 'cushion': 10000, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 5', {'disbursements': [120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10000}], ['control 6', {'disbursements': [99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 20000}, {'base': 8333, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 8333}]], [['regression: low point comparison', {'disbursements': [120000], 'low_point': 150000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 10000, 'cushion': 50000, 'shortage': 0, 'refund': 100000, 'monthly': 10000}], ['control 1', {'disbursements': [7, 7], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 1, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 1}], ['control 2', {'disbursements': [7, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 10001, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 10001}], ['control 3', {'disbursements': [250000, 250000, 120000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 51667, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 51667}], ['control 4', {'disbursements': [99999, 120000, 99999], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 26667, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 26667}], ['control 5', {'disbursements': [36000], 'low_point': 0, 'cushion_months': 0, 'refund_threshold': 0}, {'base': 3000, 'cushion': 0, 'shortage': 0, 'refund': 0, 'monthly': 3000}], ['control 6', {'disbursements': [36000, 120000, 36000], 'low_point': 80000, 'cushion_months': 5, 'refund_threshold': 20000}, {'base': 16000, 'cushion': 80000, 'shortage': 0, 'refund': 0, 'monthly': 16000}]]]
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:
Escrow accounts with plenty of funds are told they have a shortage.

x = {'disbursements': annual amounts, 'low_point': projected lowest balance, 'cushion_months', 'refund_threshold'}. total = sum; base = round_half_up(total/12); cushion = round_half_up(total*cushion_months/12); diff = low_point - cushion; shortage = max(0, -diff) is collected over 12 months rounded up; surplus = max(0, diff) is refunded when >= refund_threshold, otherwise floor(surplus/12) is credited monthly. Return {'base', 'cushion', 'shortage', 'refund', 'monthly'}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    P = x['principal']
    monthly = level(P, x['months'])
    pay = monthly // 2
    bal = P
    k = 0
    total = 0
    final = 0
    while bal > 0 and k < 26 * x['months']:
        k += 1
        interest = rnd(bal * bp, 260000)
        total += interest
        if bal + interest <= pay:
            final = bal + interest
            bal = 0
        else:
            bal -= pay - interest
    return {'payment': pay, 'periods': k, 'final_payment': final, 'total_interest': total}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: biweekly payment derivation', {'principal': 5000, 'rate_bp': 725, 'months': 36}, {'payment': 78, 'periods': 71, 'final_payment': 58, 'total_interest': 518}], ['control 1', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 725, 'months': 7}, {'payment': 73165, 'periods': 14, 'final_payment': 69832, 'total_interest': 20977}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 5', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly payment derivation', {'principal': 100000, 'rate_bp': 1200, 'months': 36}, {'payment': 1661, 'periods': 71, 'final_payment': 1165, 'total_interest': 17435}], ['control 1', {'principal': 250000, 'rate_bp': 725, 'months': 36}, {'payment': 3874, 'periods': 72, 'final_payment': 951, 'total_interest': 26005}], ['control 2', {'principal': 100000, 'rate_bp': 725, 'months': 12}, {'payment': 4332, 'periods': 24, 'final_payment': 3874, 'total_interest': 3510}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 6', {'principal': 1000000, 'rate_bp': 725, 'months': 7}, {'payment': 73165, 'periods': 14, 'final_payment': 69832, 'total_interest': 20977}]], [['regression: biweekly payment derivation', {'principal': 250000, 'rate_bp': 1200, 'months': 7}, {'payment': 18579, 'periods': 14, 'final_payment': 17172, 'total_interest': 8699}], ['control 1', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 2', {'principal': 1000000, 'rate_bp': 725, 'months': 12}, {'payment': 43321, 'periods': 24, 'final_payment': 38698, 'total_interest': 35081}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 5000, 'rate_bp': 600, 'months': 12}, {'payment': 215, 'periods': 24, 'final_payment': 199, 'total_interest': 144}], ['control 5', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 7}, {'payment': 18216, 'periods': 14, 'final_payment': 17530, 'total_interest': 4338}]], [['regression: biweekly payment derivation', {'principal': 250000, 'rate_bp': 600, 'months': 60}, {'payment': 2417, 'periods': 119, 'final_payment': 764, 'total_interest': 35970}], ['control 1', {'principal': 5000, 'rate_bp': 1200, 'months': 36}, {'payment': 83, 'periods': 71, 'final_payment': 64, 'total_interest': 874}], ['control 2', {'principal': 250000, 'rate_bp': 725, 'months': 60}, {'payment': 2490, 'periods': 118, 'final_payment': 2377, 'total_interest': 43707}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 1200, 'months': 60}, {'payment': 11122, 'periods': 117, 'final_payment': 4717, 'total_interest': 294869}], ['control 5', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['control 6', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}]], [['regression: biweekly payment derivation', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 1', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 2', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 1200, 'months': 60}, {'payment': 1112, 'periods': 117, 'final_payment': 503, 'total_interest': 29495}], ['control 5', {'principal': 250000, 'rate_bp': 1200, 'months': 36}, {'payment': 4152, 'periods': 71, 'final_payment': 2947, 'total_interest': 43587}], ['control 6', {'principal': 1000000, 'rate_bp': 725, 'months': 36}, {'payment': 15496, 'periods': 72, 'final_payment': 3794, 'total_interest': 104010}]]]
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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    P = x['principal']
    monthly = level(P, x['months'])
    pay = rnd(monthly * 12, 26)
    bal = P
    k = 0
    total = 0
    final = 0
    while bal > 0 and k < 26 * x['months']:
        k += 1
        interest = rnd(bal * bp, 260000)
        total += interest
        if bal + interest <= pay:
            final = bal + interest
            bal = 0
        else:
            bal -= pay - interest
    return {'payment': pay, 'periods': k, 'final_payment': final, 'total_interest': total}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: biweekly payment derivation', {'principal': 5000, 'rate_bp': 725, 'months': 36}, {'payment': 78, 'periods': 71, 'final_payment': 58, 'total_interest': 518}], ['control 1', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 725, 'months': 7}, {'payment': 73165, 'periods': 14, 'final_payment': 69832, 'total_interest': 20977}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 5', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly payment derivation', {'principal': 100000, 'rate_bp': 1200, 'months': 36}, {'payment': 1661, 'periods': 71, 'final_payment': 1165, 'total_interest': 17435}], ['control 1', {'principal': 250000, 'rate_bp': 725, 'months': 36}, {'payment': 3874, 'periods': 72, 'final_payment': 951, 'total_interest': 26005}], ['control 2', {'principal': 100000, 'rate_bp': 725, 'months': 12}, {'payment': 4332, 'periods': 24, 'final_payment': 3874, 'total_interest': 3510}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 5', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 6', {'principal': 1000000, 'rate_bp': 725, 'months': 7}, {'payment': 73165, 'periods': 14, 'final_payment': 69832, 'total_interest': 20977}]], [['regression: biweekly payment derivation', {'principal': 250000, 'rate_bp': 1200, 'months': 7}, {'payment': 18579, 'periods': 14, 'final_payment': 17172, 'total_interest': 8699}], ['control 1', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 2', {'principal': 1000000, 'rate_bp': 725, 'months': 12}, {'payment': 43321, 'periods': 24, 'final_payment': 38698, 'total_interest': 35081}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 5000, 'rate_bp': 600, 'months': 12}, {'payment': 215, 'periods': 24, 'final_payment': 199, 'total_interest': 144}], ['control 5', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 6', {'principal': 250000, 'rate_bp': 600, 'months': 7}, {'payment': 18216, 'periods': 14, 'final_payment': 17530, 'total_interest': 4338}]], [['regression: biweekly payment derivation', {'principal': 250000, 'rate_bp': 600, 'months': 60}, {'payment': 2417, 'periods': 119, 'final_payment': 764, 'total_interest': 35970}], ['control 1', {'principal': 5000, 'rate_bp': 1200, 'months': 36}, {'payment': 83, 'periods': 71, 'final_payment': 64, 'total_interest': 874}], ['control 2', {'principal': 250000, 'rate_bp': 725, 'months': 60}, {'payment': 2490, 'periods': 118, 'final_payment': 2377, 'total_interest': 43707}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 1200, 'months': 60}, {'payment': 11122, 'periods': 117, 'final_payment': 4717, 'total_interest': 294869}], ['control 5', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['control 6', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}]], [['regression: biweekly payment derivation', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 1', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 2', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 1200, 'months': 60}, {'payment': 1112, 'periods': 117, 'final_payment': 503, 'total_interest': 29495}], ['control 5', {'principal': 250000, 'rate_bp': 1200, 'months': 36}, {'payment': 4152, 'periods': 71, 'final_payment': 2947, 'total_interest': 43587}], ['control 6', {'principal': 1000000, 'rate_bp': 725, 'months': 36}, {'payment': 15496, 'periods': 72, 'final_payment': 3794, 'total_interest': 104010}]]]
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:
Odd-cent monthly payments produce a biweekly payment a half cent short.

x = {'principal', 'rate_bp', 'months'}. The monthly payment is the exact annuity rounded half-up; the biweekly payment is round_half_up(monthly/2), paid 26 times a year. Periodic interest is round_half_up(balance*bp/260000). When balance + interest <= payment the loan pays off with that amount. Return {'payment', 'periods', 'final_payment', 'total_interest'}.

split: open-access


broken_source:
"""Failure Map reference implementation. Python standard library only."""
import json
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    P = x['principal']
    monthly = level(P, x['months'])
    pay = rnd(monthly, 2)
    bal = P
    k = 0
    total = 0
    final = 0
    while bal > 0 and k < 26 * x['months']:
        k += 1
        interest = rnd(bal * bp, 240000)
        total += interest
        if bal + interest <= pay:
            final = bal + interest
            bal = 0
        else:
            bal -= pay - interest
    return {'payment': pay, 'periods': k, 'final_payment': final, 'total_interest': total}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['regression: biweekly periodic rate, partial-repair probe', {'principal': 5000, 'rate_bp': 725, 'months': 36}, {'payment': 78, 'periods': 71, 'final_payment': 58, 'total_interest': 518}], ['control 1', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 0, 'months': 60}, {'payment': 8334, 'periods': 120, 'final_payment': 8254, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 250000, 'rate_bp': 725, 'months': 36}, {'payment': 3874, 'periods': 72, 'final_payment': 951, 'total_interest': 26005}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 60}, {'payment': 2084, 'periods': 120, 'final_payment': 2004, 'total_interest': 0}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 0, 'months': 12}, {'payment': 4167, 'periods': 24, 'final_payment': 4159, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 12}, {'payment': 209, 'periods': 24, 'final_payment': 193, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 2', {'principal': 1000000, 'rate_bp': 0, 'months': 7}, {'payment': 71429, 'periods': 14, 'final_payment': 71423, 'total_interest': 0}], ['control 3', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 12}, {'payment': 10417, 'periods': 24, 'final_payment': 10409, 'total_interest': 0}], ['control 5', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 6', {'principal': 100000, 'rate_bp': 0, 'months': 60}, {'payment': 834, 'periods': 120, 'final_payment': 754, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 1200, 'months': 36}, {'payment': 83, 'periods': 71, 'final_payment': 64, 'total_interest': 874}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 12}, {'payment': 10417, 'periods': 24, 'final_payment': 10409, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 0, 'months': 12}, {'payment': 41667, 'periods': 24, 'final_payment': 41659, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 12}, {'payment': 209, 'periods': 24, 'final_payment': 193, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 3', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 60}, {'payment': 2084, 'periods': 120, 'final_payment': 2004, 'total_interest': 0}], ['control 5', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 6', {'principal': 1000000, 'rate_bp': 0, 'months': 12}, {'payment': 41667, 'periods': 24, 'final_payment': 41659, 'total_interest': 0}]]]
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
import math
from fractions import Fraction
N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    def level(P, n):
        if n <= 0 or P <= 0:
            return 0
        if bp == 0:
            exact = Fraction(P, n)
        else:
            r = Fraction(bp, 120000)
            exact = P * r / (1 - (1 + r) ** -n)
        return math.floor(exact + Fraction(1, 2))
    bp = x['rate_bp']
    P = x['principal']
    monthly = level(P, x['months'])
    pay = rnd(monthly, 2)
    bal = P
    k = 0
    total = 0
    final = 0
    while bal > 0 and k < 26 * x['months']:
        k += 1
        interest = rnd(bal * bp * 14, 3650000)
        total += interest
        if bal + interest <= pay:
            final = bal + interest
            bal = 0
        else:
            bal -= pay - interest
    return {'payment': pay, 'periods': k, 'final_payment': final, 'total_interest': total}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 1200, 'months': 12}, {'payment': 222, 'periods': 24, 'final_payment': 187, 'total_interest': 293}], ['regression: biweekly periodic rate, partial-repair probe', {'principal': 5000, 'rate_bp': 725, 'months': 36}, {'payment': 78, 'periods': 71, 'final_payment': 58, 'total_interest': 518}], ['control 1', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 0, 'months': 60}, {'payment': 8334, 'periods': 120, 'final_payment': 8254, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 250000, 'rate_bp': 725, 'months': 36}, {'payment': 3874, 'periods': 72, 'final_payment': 951, 'total_interest': 26005}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 60}, {'payment': 2084, 'periods': 120, 'final_payment': 2004, 'total_interest': 0}], ['control 3', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 4', {'principal': 100000, 'rate_bp': 0, 'months': 12}, {'payment': 4167, 'periods': 24, 'final_payment': 4159, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 12}, {'payment': 209, 'periods': 24, 'final_payment': 193, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 100000, 'rate_bp': 600, 'months': 36}, {'payment': 1521, 'periods': 72, 'final_payment': 590, 'total_interest': 8581}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 2', {'principal': 1000000, 'rate_bp': 0, 'months': 7}, {'payment': 71429, 'periods': 14, 'final_payment': 71423, 'total_interest': 0}], ['control 3', {'principal': 250000, 'rate_bp': 0, 'months': 7}, {'payment': 17857, 'periods': 15, 'final_payment': 2, 'total_interest': 0}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 12}, {'payment': 10417, 'periods': 24, 'final_payment': 10409, 'total_interest': 0}], ['control 5', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 6', {'principal': 100000, 'rate_bp': 0, 'months': 60}, {'payment': 834, 'periods': 120, 'final_payment': 754, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 1200, 'months': 36}, {'payment': 83, 'periods': 71, 'final_payment': 64, 'total_interest': 874}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 250000, 'rate_bp': 0, 'months': 12}, {'payment': 10417, 'periods': 24, 'final_payment': 10409, 'total_interest': 0}], ['control 3', {'principal': 1000000, 'rate_bp': 0, 'months': 36}, {'payment': 13889, 'periods': 72, 'final_payment': 13881, 'total_interest': 0}], ['control 4', {'principal': 1000000, 'rate_bp': 0, 'months': 12}, {'payment': 41667, 'periods': 24, 'final_payment': 41659, 'total_interest': 0}], ['control 5', {'principal': 5000, 'rate_bp': 0, 'months': 12}, {'payment': 209, 'periods': 24, 'final_payment': 193, 'total_interest': 0}], ['control 6', {'principal': 5000, 'rate_bp': 0, 'months': 7}, {'payment': 357, 'periods': 15, 'final_payment': 2, 'total_interest': 0}]], [['regression: biweekly periodic rate', {'principal': 5000, 'rate_bp': 725, 'months': 60}, {'payment': 50, 'periods': 118, 'final_payment': 21, 'total_interest': 871}], ['control 1', {'principal': 100000, 'rate_bp': 0, 'months': 7}, {'payment': 7143, 'periods': 14, 'final_payment': 7141, 'total_interest': 0}], ['control 2', {'principal': 100000, 'rate_bp': 0, 'months': 36}, {'payment': 1389, 'periods': 72, 'final_payment': 1381, 'total_interest': 0}], ['control 3', {'principal': 5000, 'rate_bp': 0, 'months': 36}, {'payment': 70, 'periods': 72, 'final_payment': 30, 'total_interest': 0}], ['control 4', {'principal': 250000, 'rate_bp': 0, 'months': 60}, {'payment': 2084, 'periods': 120, 'final_payment': 2004, 'total_interest': 0}], ['control 5', {'principal': 250000, 'rate_bp': 0, 'months': 36}, {'payment': 3472, 'periods': 73, 'final_payment': 16, 'total_interest': 0}], ['control 6', {'principal': 1000000, 'rate_bp': 0, 'months': 12}, {'payment': 41667, 'periods': 24, 'final_payment': 41659, 'total_interest': 0}]]]
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:
Biweekly interest is computed as if there were 24 periods a year.

x = {'principal', 'rate_bp', 'months'}. The monthly payment is the exact annuity rounded half-up; the biweekly payment is round_half_up(monthly/2), paid 26 times a year. Periodic interest is round_half_up(balance*bp/260000). When balance + interest <= payment the loan pays off with that amount. Return {'payment', 'periods', 'final_payment', 'total_interest'}.

split: open-access