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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day >= x['today']:
            break
        if status in ('canceled', 'unpaid'):
            break
        if kind == 'pay':
            status = 'active'; nxt = None; break
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11]), ('partial-repair probe', {'events': [[33, 'pay'], [13, 'fail']], 'due': 12, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 22}, ['past_due', 15]), ('partial-repair probe', {'events': [[30, 'pay'], [23, 'pay'], [19, 'fail']], 'due': 6, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 9]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None])], [('regression', {'events': [[33, 'fail'], [31, 'fail'], [18, 'pay'], [14, 'fail'], [20, 'pay']], 'due': 10, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['past_due', 11]), ('regression', {'events': [[21, 'fail'], [43, 'fail'], [28, 'fail'], [35, 'fail'], [24, 'fail']], 'due': 20, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 21}, ['past_due', 23]), ('partial-repair probe', {'events': [[32, 'fail'], [39, 'fail'], [27, 'pay'], [20, 'fail'], [37, 'pay']], 'due': 20, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 23]), ('partial-repair probe', {'events': [[22, 'fail'], [35, 'fail'], [31, 'pay'], [32, 'fail'], [37, 'fail'], [31, 'fail']], 'due': 12, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 24}, ['past_due', 14]), ('normal control', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None])], [('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[20, 'fail']], 'due': 18, 'retry': [5], 'final': 'canceled', 'today': 20}, ['past_due', 23]), ('partial-repair probe', {'events': [[28, 'fail'], [40, 'pay'], [35, 'pay'], [35, 'fail']], 'due': 16, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 30}, ['past_due', 18]), ('partial-repair probe', {'events': [[17, 'fail'], [33, 'pay']], 'due': 15, 'retry': [5], 'final': 'unpaid', 'today': 27}, ['past_due', 20]), ('normal control', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[33, 'fail'], [31, 'fail'], [18, 'pay'], [14, 'fail'], [20, 'pay']], 'due': 10, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['past_due', 11]), ('regression', {'events': [[20, 'fail'], [43, 'fail'], [25, 'pay']], 'due': 19, 'retry': [1, 3], 'final': 'canceled', 'today': 25}, ['active', None]), ('partial-repair probe', {'events': [[38, 'pay'], [28, 'fail']], 'due': 18, 'retry': [1, 3], 'final': 'unpaid', 'today': 32}, ['past_due', 19]), ('partial-repair probe', {'events': [[18, 'pay'], [19, 'fail'], [4, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 17}, ['past_due', 3]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None])], [('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[20, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 20}, ['past_due', 8]), ('partial-repair probe', {'events': [[32, 'fail'], [38, 'fail'], [40, 'fail'], [27, 'fail'], [31, 'pay'], [43, 'fail']], 'due': 18, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 28}, ['past_due', 21]), ('partial-repair probe', {'events': [[15, 'fail'], [28, 'pay']], 'due': 5, 'retry': [5], 'final': 'canceled', 'today': 19}, ['past_due', 10]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [[27, 'pay'], [21, 'fail'], [28, 'fail'], [31, 'fail'], [19, 'pay'], [21, 'fail']], 'due': 15, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None]), ('normal control', {'events': [[20, 'fail'], [17, 'fail'], [24, 'pay'], [6, 'pay'], [7, 'fail'], [27, 'fail']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 16}, ['active', None])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day > x['today'] and kind == 'fail':
            break
        if status in ('canceled', 'unpaid'):
            break
        if kind == 'pay':
            status = 'active'; nxt = None; break
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11]), ('partial-repair probe', {'events': [[33, 'pay'], [13, 'fail']], 'due': 12, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 22}, ['past_due', 15]), ('partial-repair probe', {'events': [[30, 'pay'], [23, 'pay'], [19, 'fail']], 'due': 6, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 9]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None])], [('regression', {'events': [[33, 'fail'], [31, 'fail'], [18, 'pay'], [14, 'fail'], [20, 'pay']], 'due': 10, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['past_due', 11]), ('regression', {'events': [[21, 'fail'], [43, 'fail'], [28, 'fail'], [35, 'fail'], [24, 'fail']], 'due': 20, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 21}, ['past_due', 23]), ('partial-repair probe', {'events': [[32, 'fail'], [39, 'fail'], [27, 'pay'], [20, 'fail'], [37, 'pay']], 'due': 20, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 23]), ('partial-repair probe', {'events': [[22, 'fail'], [35, 'fail'], [31, 'pay'], [32, 'fail'], [37, 'fail'], [31, 'fail']], 'due': 12, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 24}, ['past_due', 14]), ('normal control', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None])], [('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[20, 'fail']], 'due': 18, 'retry': [5], 'final': 'canceled', 'today': 20}, ['past_due', 23]), ('partial-repair probe', {'events': [[28, 'fail'], [40, 'pay'], [35, 'pay'], [35, 'fail']], 'due': 16, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 30}, ['past_due', 18]), ('partial-repair probe', {'events': [[17, 'fail'], [33, 'pay']], 'due': 15, 'retry': [5], 'final': 'unpaid', 'today': 27}, ['past_due', 20]), ('normal control', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[33, 'fail'], [31, 'fail'], [18, 'pay'], [14, 'fail'], [20, 'pay']], 'due': 10, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['past_due', 11]), ('regression', {'events': [[20, 'fail'], [43, 'fail'], [25, 'pay']], 'due': 19, 'retry': [1, 3], 'final': 'canceled', 'today': 25}, ['active', None]), ('partial-repair probe', {'events': [[38, 'pay'], [28, 'fail']], 'due': 18, 'retry': [1, 3], 'final': 'unpaid', 'today': 32}, ['past_due', 19]), ('partial-repair probe', {'events': [[18, 'pay'], [19, 'fail'], [4, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 17}, ['past_due', 3]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None])], [('regression', {'events': [[32, 'pay'], [17, 'fail'], [8, 'fail']], 'due': 8, 'retry': [1, 3], 'final': 'canceled', 'today': 17}, ['past_due', 11]), ('regression', {'events': [[20, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 20}, ['past_due', 8]), ('partial-repair probe', {'events': [[32, 'fail'], [38, 'fail'], [40, 'fail'], [27, 'fail'], [31, 'pay'], [43, 'fail']], 'due': 18, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 28}, ['past_due', 21]), ('partial-repair probe', {'events': [[15, 'fail'], [28, 'pay']], 'due': 5, 'retry': [5], 'final': 'canceled', 'today': 19}, ['past_due', 10]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [[27, 'pay'], [21, 'fail'], [28, 'fail'], [31, 'fail'], [19, 'pay'], [21, 'fail']], 'due': 15, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None]), ('normal control', {'events': [[20, 'fail'], [17, 'fail'], [24, 'pay'], [6, 'pay'], [7, 'fail'], [27, 'fail']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 16}, ['active', None])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Status ignores a payment or failure that happened today.

Input {events: [[day, fail|pay]], due, retry: offsets from due for retries, final canceled|unpaid, today}. Events up to today are processed by day (stable). Payment settles: status active, no retry, stop. Each failure increments a counter; after more failures than scheduled retries the status becomes final and processing stops; otherwise past_due with next retry at due + retry[failures-1]. Return [status, next_retry].

split: open-access


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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day > x['today']:
            break
        if status in ('canceled', 'unpaid'):
            break
        if kind == 'pay':
            status = 'active'; nxt = None
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('regression', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('partial-repair probe', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('partial-repair probe', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8])], [('regression', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('regression', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None]), ('partial-repair probe', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('partial-repair probe', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('regression', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None]), ('partial-repair probe', {'events': [[13, 'pay'], [24, 'fail'], [8, 'fail'], [23, 'fail']], 'due': 1, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 27}, ['active', None]), ('partial-repair probe', {'events': [[25, 'fail'], [26, 'pay']], 'due': 20, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 40}, ['active', None]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None])], [('regression', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('regression', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('partial-repair probe', {'events': [[16, 'fail'], [19, 'pay'], [40, 'fail'], [35, 'pay']], 'due': 16, 'retry': [5], 'final': 'unpaid', 'today': 21}, ['active', None]), ('partial-repair probe', {'events': [[23, 'pay'], [34, 'pay'], [37, 'pay'], [21, 'fail'], [33, 'fail'], [34, 'pay']], 'due': 18, 'retry': [5], 'final': 'canceled', 'today': 45}, ['active', None]), ('normal control', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11]), ('normal control', {'events': [[33, 'pay'], [13, 'fail']], 'due': 12, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 22}, ['past_due', 15]), ('normal control', {'events': [[15, 'fail'], [21, 'fail']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 31}, ['past_due', 12]), ('normal control', {'events': [[25, 'fail'], [23, 'fail']], 'due': 2, 'retry': [1, 3], 'final': 'unpaid', 'today': 28}, ['past_due', 5])], [('regression', {'events': [[13, 'pay'], [24, 'fail'], [8, 'fail'], [23, 'fail']], 'due': 1, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 27}, ['active', None]), ('regression', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('partial-repair probe', {'events': [[9, 'fail'], [12, 'pay'], [32, 'fail'], [20, 'fail'], [19, 'fail'], [31, 'fail']], 'due': 7, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 36}, ['active', None]), ('partial-repair probe', {'events': [[29, 'pay'], [27, 'fail']], 'due': 18, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 47}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [30, 'fail'], [22, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 37}, ['canceled', None]), ('normal control', {'events': [[25, 'pay'], [19, 'fail'], [14, 'fail'], [18, 'fail'], [11, 'fail'], [19, 'pay']], 'due': 4, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 8}, ['active', None]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 14}, ['active', None]), ('normal control', {'events': [[30, 'pay'], [23, 'pay'], [19, 'fail']], 'due': 6, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 9])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day > x['today']:
            break
        if status in ('canceled', 'unpaid'):
            break
        if kind == 'pay':
            status = 'active'; break
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('regression', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('partial-repair probe', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('partial-repair probe', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8])], [('regression', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('regression', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None]), ('partial-repair probe', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('partial-repair probe', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('regression', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None]), ('partial-repair probe', {'events': [[13, 'pay'], [24, 'fail'], [8, 'fail'], [23, 'fail']], 'due': 1, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 27}, ['active', None]), ('partial-repair probe', {'events': [[25, 'fail'], [26, 'pay']], 'due': 20, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 40}, ['active', None]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None])], [('regression', {'events': [[18, 'pay'], [16, 'fail'], [28, 'fail'], [22, 'fail']], 'due': 7, 'retry': [1, 3], 'final': 'unpaid', 'today': 37}, ['active', None]), ('regression', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('partial-repair probe', {'events': [[16, 'fail'], [19, 'pay'], [40, 'fail'], [35, 'pay']], 'due': 16, 'retry': [5], 'final': 'unpaid', 'today': 21}, ['active', None]), ('partial-repair probe', {'events': [[23, 'pay'], [34, 'pay'], [37, 'pay'], [21, 'fail'], [33, 'fail'], [34, 'pay']], 'due': 18, 'retry': [5], 'final': 'canceled', 'today': 45}, ['active', None]), ('normal control', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11]), ('normal control', {'events': [[33, 'pay'], [13, 'fail']], 'due': 12, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 22}, ['past_due', 15]), ('normal control', {'events': [[15, 'fail'], [21, 'fail']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 31}, ['past_due', 12]), ('normal control', {'events': [[25, 'fail'], [23, 'fail']], 'due': 2, 'retry': [1, 3], 'final': 'unpaid', 'today': 28}, ['past_due', 5])], [('regression', {'events': [[13, 'pay'], [24, 'fail'], [8, 'fail'], [23, 'fail']], 'due': 1, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 27}, ['active', None]), ('regression', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('partial-repair probe', {'events': [[9, 'fail'], [12, 'pay'], [32, 'fail'], [20, 'fail'], [19, 'fail'], [31, 'fail']], 'due': 7, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 36}, ['active', None]), ('partial-repair probe', {'events': [[29, 'pay'], [27, 'fail']], 'due': 18, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 47}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [30, 'fail'], [22, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 37}, ['canceled', None]), ('normal control', {'events': [[25, 'pay'], [19, 'fail'], [14, 'fail'], [18, 'fail'], [11, 'fail'], [19, 'pay']], 'due': 4, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 8}, ['active', None]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 14}, ['active', None]), ('normal control', {'events': [[30, 'pay'], [23, 'pay'], [19, 'fail']], 'due': 6, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 21}, ['past_due', 9])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Failures logged after a successful payment put a paid subscription back into dunning.

Input {events: [[day, fail|pay]], due, retry: offsets from due for retries, final canceled|unpaid, today}. Events up to today are processed by day (stable). Payment settles: status active, no retry, stop. Each failure increments a counter; after more failures than scheduled retries the status becomes final and processing stops; otherwise past_due with next retry at due + retry[failures-1]. Return [status, next_retry].

split: open-access


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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day > x['today']:
            break
        if status == 'canceled':
            break
        if kind == 'pay':
            status = 'active'; nxt = None; break
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('regression', {'events': [[13, 'fail'], [6, 'fail'], [27, 'fail'], [20, 'fail'], [6, 'fail'], [19, 'pay']], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 28}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('partial-repair probe', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None])], [('regression', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('regression', {'events': [[13, 'fail'], [6, 'fail'], [27, 'fail'], [20, 'fail'], [6, 'fail'], [19, 'pay']], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 28}, ['unpaid', None]), ('partial-repair probe', {'events': [[43, 'pay'], [34, 'fail'], [29, 'fail']], 'due': 20, 'retry': [5], 'final': 'canceled', 'today': 48}, ['canceled', None]), ('partial-repair probe', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('normal control', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None])], [('regression', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('regression', {'events': [[20, 'fail'], [27, 'pay'], [16, 'fail'], [22, 'fail'], [23, 'fail'], [23, 'fail']], 'due': 6, 'retry': [1, 3], 'final': 'unpaid', 'today': 34}, ['unpaid', None]), ('partial-repair probe', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('normal control', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('regression', {'events': [[21, 'fail'], [21, 'fail'], [28, 'fail'], [21, 'fail'], [21, 'pay'], [39, 'fail']], 'due': 14, 'retry': [5], 'final': 'unpaid', 'today': 25}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('partial-repair probe', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None])], [('regression', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('regression', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('partial-repair probe', {'events': [[43, 'pay'], [34, 'fail'], [29, 'fail']], 'due': 20, 'retry': [5], 'final': 'canceled', 'today': 48}, ['canceled', None]), ('partial-repair probe', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [[27, 'pay'], [21, 'fail'], [28, 'fail'], [31, 'fail'], [19, 'pay'], [21, 'fail']], 'due': 15, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None]), ('normal control', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    status = 'active'
    fails = 0
    nxt = None
    for day, kind in sorted(x['events'], key=lambda e: e[0]):
        if day > x['today']:
            break
        if status == x['final'] and kind == 'fail':
            break
        if kind == 'pay':
            status = 'active'; nxt = None; break
        fails += 1
        if fails > len(x['retry']):
            status = x['final']; nxt = None
        else:
            status = 'past_due'; nxt = x['due'] + x['retry'][fails - 1]
    return [status, nxt]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('regression', {'events': [[13, 'fail'], [6, 'fail'], [27, 'fail'], [20, 'fail'], [6, 'fail'], [19, 'pay']], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 28}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('partial-repair probe', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('normal control', {'events': [], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 4}, ['active', None]), ('normal control', {'events': [[19, 'fail'], [32, 'fail'], [37, 'fail'], [29, 'fail'], [33, 'pay'], [42, 'fail']], 'due': 19, 'retry': [3, 5, 7], 'final': 'canceled', 'today': 34}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [8, 'fail'], [4, 'fail'], [23, 'fail']], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 15}, ['past_due', 5]), ('normal control', {'events': [[18, 'fail'], [12, 'fail'], [21, 'fail'], [19, 'fail']], 'due': 10, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 23}, ['unpaid', None])], [('regression', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('regression', {'events': [[13, 'fail'], [6, 'fail'], [27, 'fail'], [20, 'fail'], [6, 'fail'], [19, 'pay']], 'due': 2, 'retry': [5], 'final': 'unpaid', 'today': 28}, ['unpaid', None]), ('partial-repair probe', {'events': [[43, 'pay'], [34, 'fail'], [29, 'fail']], 'due': 20, 'retry': [5], 'final': 'canceled', 'today': 48}, ['canceled', None]), ('partial-repair probe', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('normal control', {'events': [[24, 'pay'], [14, 'fail'], [13, 'pay'], [21, 'pay'], [16, 'fail'], [6, 'fail']], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 35}, ['active', None]), ('normal control', {'events': [[4, 'fail'], [11, 'fail']], 'due': 3, 'retry': [5], 'final': 'canceled', 'today': 8}, ['past_due', 8]), ('normal control', {'events': [], 'due': 5, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 24}, ['active', None]), ('normal control', {'events': [[15, 'pay'], [8, 'pay'], [23, 'fail'], [23, 'fail']], 'due': 2, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 9}, ['active', None])], [('regression', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('regression', {'events': [[20, 'fail'], [27, 'pay'], [16, 'fail'], [22, 'fail'], [23, 'fail'], [23, 'fail']], 'due': 6, 'retry': [1, 3], 'final': 'unpaid', 'today': 34}, ['unpaid', None]), ('partial-repair probe', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('normal control', {'events': [[5, 'pay']], 'due': 3, 'retry': [1, 3], 'final': 'canceled', 'today': 18}, ['active', None]), ('normal control', {'events': [[17, 'fail'], [31, 'pay']], 'due': 8, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 16}, ['active', None]), ('normal control', {'events': [[5, 'pay'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 2}, ['active', None]), ('normal control', {'events': [[15, 'fail']], 'due': 1, 'retry': [1, 3], 'final': 'canceled', 'today': 14}, ['active', None])], [('regression', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('regression', {'events': [[21, 'fail'], [21, 'fail'], [28, 'fail'], [21, 'fail'], [21, 'pay'], [39, 'fail']], 'due': 14, 'retry': [5], 'final': 'unpaid', 'today': 25}, ['unpaid', None]), ('partial-repair probe', {'events': [[33, 'pay'], [20, 'fail'], [22, 'fail'], [34, 'fail'], [35, 'fail'], [36, 'fail']], 'due': 15, 'retry': [5], 'final': 'canceled', 'today': 41}, ['canceled', None]), ('partial-repair probe', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('normal control', {'events': [], 'due': 13, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 26}, ['active', None]), ('normal control', {'events': [[22, 'fail'], [10, 'pay'], [11, 'fail'], [25, 'fail'], [15, 'fail'], [16, 'fail']], 'due': 10, 'retry': [1, 3], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [[21, 'fail'], [22, 'pay'], [30, 'fail'], [27, 'fail']], 'due': 16, 'retry': [5], 'final': 'canceled', 'today': 32}, ['active', None]), ('normal control', {'events': [[28, 'fail']], 'due': 17, 'retry': [1, 3], 'final': 'unpaid', 'today': 21}, ['active', None])], [('regression', {'events': [[32, 'pay'], [16, 'fail'], [17, 'fail']], 'due': 12, 'retry': [5], 'final': 'unpaid', 'today': 36}, ['unpaid', None]), ('regression', {'events': [[15, 'pay'], [6, 'fail'], [14, 'fail'], [22, 'fail'], [5, 'fail'], [15, 'fail']], 'due': 0, 'retry': [1, 3], 'final': 'unpaid', 'today': 19}, ['unpaid', None]), ('partial-repair probe', {'events': [[43, 'pay'], [34, 'fail'], [29, 'fail']], 'due': 20, 'retry': [5], 'final': 'canceled', 'today': 48}, ['canceled', None]), ('partial-repair probe', {'events': [[37, 'pay'], [38, 'fail'], [34, 'fail'], [27, 'fail'], [32, 'fail']], 'due': 14, 'retry': [1, 3], 'final': 'unpaid', 'today': 42}, ['unpaid', None]), ('normal control', {'events': [[25, 'fail'], [22, 'fail']], 'due': 19, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 41}, ['past_due', 23]), ('normal control', {'events': [[27, 'pay'], [21, 'fail'], [28, 'fail'], [31, 'fail'], [19, 'pay'], [21, 'fail']], 'due': 15, 'retry': [2, 4, 8, 16], 'final': 'unpaid', 'today': 39}, ['active', None]), ('normal control', {'events': [], 'due': 1, 'retry': [2, 4, 8, 16], 'final': 'canceled', 'today': 30}, ['active', None]), ('normal control', {'events': [[8, 'fail'], [9, 'fail'], [33, 'pay']], 'due': 8, 'retry': [3, 5, 7], 'final': 'unpaid', 'today': 8}, ['past_due', 11])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Unpaid subscriptions keep counting failures or are silently reactivated.

Input {events: [[day, fail|pay]], due, retry: offsets from due for retries, final canceled|unpaid, today}. Events up to today are processed by day (stable). Payment settles: status active, no retry, stop. Each failure increments a counter; after more failures than scheduled retries the status becomes final and processing stops; otherwise past_due with next retry at due + retry[failures-1]. Return [status, next_retry].

split: open-access


prompt:
Two changes submitted in the same second produce two sets of proration lines.

Input {start, end, initial price, changes: [[t, price]]}. Changes are ordered by time; at equal times only the last applies. A change outside [start, end) or to the current price is ignored. Each effective change emits a credit line -(current*(end-t)/span) and a debit line new*(end-t)/span, magnitudes half-up, and the new price becomes current. Return the list of line amounts.

split: open-access


prompt:
The second plan change credits the original plan instead of the plan in effect.

Input {start, end, initial price, changes: [[t, price]]}. Changes are ordered by time; at equal times only the last applies. A change outside [start, end) or to the current price is ignored. Each effective change emits a credit line -(current*(end-t)/span) and a debit line new*(end-t)/span, magnitudes half-up, and the new price becomes current. Return the list of line amounts.

split: open-access


prompt:
Switching to the same plan emits a pair of offsetting lines.

Input {start, end, initial price, changes: [[t, price]]}. Changes are ordered by time; at equal times only the last applies. A change outside [start, end) or to the current price is ignored. Each effective change emits a credit line -(current*(end-t)/span) and a debit line new*(end-t)/span, magnitudes half-up, and the new price becomes current. Return the list of line amounts.

split: open-access


prompt:
Changes timestamped before the period start produce prorations larger than the period price.

Input {start, end, initial price, changes: [[t, price]]}. Changes are ordered by time; at equal times only the last applies. A change outside [start, end) or to the current price is ignored. Each effective change emits a credit line -(current*(end-t)/span) and a debit line new*(end-t)/span, magnitudes half-up, and the new price becomes current. Return the list of line amounts.

split: open-access


prompt:
Terms that include Feb 29 prorate at a daily rate that sums to more than the annual price.

Input {price, start (annual term start), from, to (exclusive)}. The term is [start, same date next year); a Feb 29 start ends on Mar 1. The requested range is clipped to the term; days = max(0, clipped length). Amount = price*days/term_days half-up. Return [term_days, amount].

split: open-access


prompt:
Ranges that begin before the term start are billed for days outside the term.

Input {price, start (annual term start), from, to (exclusive)}. The term is [start, same date next year); a Feb 29 start ends on Mar 1. The requested range is clipped to the term; days = max(0, clipped length). Amount = price*days/term_days half-up. Return [term_days, amount].

split: open-access


prompt:
Every prorated range is billed one day too many.

Input {price, start (annual term start), from, to (exclusive)}. The term is [start, same date next year); a Feb 29 start ends on Mar 1. The requested range is clipped to the term; days = max(0, clipped length). Amount = price*days/term_days half-up. Return [term_days, amount].

split: open-access


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

N = 1
observations = []
def solve(x):
    pending = sum(x['lines'])
    if pending >= x['threshold']:
        return [pending, 0, 0]
    if pending <= -x['threshold']:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0]), ('normal control', {'lines': [207], 'threshold': 500, 'carried': 0}, [0, 0, 207]), ('normal control', {'lines': [429], 'threshold': 100, 'carried': 0}, [429, 0, 0]), ('normal control', {'lines': [97], 'threshold': 50, 'carried': 0}, [97, 0, 0])], [('regression', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [344], 'threshold': 100, 'carried': 0}, [344, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [326, 112, 108, 591], 'threshold': 50, 'carried': 0}, [1137, 0, 0]), ('normal control', {'lines': [127], 'threshold': 100, 'carried': 0}, [127, 0, 0])], [('regression', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('regression (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('partial-repair probe', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [341], 'threshold': 500, 'carried': 0}, [0, 0, 341]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [], 'threshold': 100, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [225], 'threshold': 500, 'carried': 0}, [0, 0, 225])], [('regression', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('regression', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('partial-repair probe', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('partial-repair probe', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [120, 341, 183, 393], 'threshold': 500, 'carried': 0}, [1037, 0, 0]), ('normal control', {'lines': [376], 'threshold': 500, 'carried': 0}, [0, 0, 376]), ('normal control', {'lines': [458], 'threshold': 100, 'carried': 0}, [458, 0, 0]), ('normal control', {'lines': [239], 'threshold': 100, 'carried': 0}, [239, 0, 0])], [('regression', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202]), ('regression', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('partial-repair probe', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('partial-repair probe', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [336], 'threshold': 100, 'carried': 0}, [336, 0, 0]), ('normal control', {'lines': [41], 'threshold': 500, 'carried': 0}, [0, 0, 41]), ('normal control', {'lines': [360], 'threshold': 500, 'carried': 0}, [0, 0, 360]), ('normal control', {'lines': [263, 589], 'threshold': 100, 'carried': 0}, [852, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    pending = x['carried'] + sum(l for l in x['lines'] if l > 0)
    if pending >= x['threshold']:
        return [pending, 0, 0]
    if pending <= -x['threshold']:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0]), ('normal control', {'lines': [207], 'threshold': 500, 'carried': 0}, [0, 0, 207]), ('normal control', {'lines': [429], 'threshold': 100, 'carried': 0}, [429, 0, 0]), ('normal control', {'lines': [97], 'threshold': 50, 'carried': 0}, [97, 0, 0])], [('regression', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [344], 'threshold': 100, 'carried': 0}, [344, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [326, 112, 108, 591], 'threshold': 50, 'carried': 0}, [1137, 0, 0]), ('normal control', {'lines': [127], 'threshold': 100, 'carried': 0}, [127, 0, 0])], [('regression', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('regression (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('partial-repair probe', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [341], 'threshold': 500, 'carried': 0}, [0, 0, 341]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [], 'threshold': 100, 'carried': 0}, [0, 0, 0]), ('normal control', {'lines': [225], 'threshold': 500, 'carried': 0}, [0, 0, 225])], [('regression', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('regression', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('partial-repair probe', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('partial-repair probe', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [120, 341, 183, 393], 'threshold': 500, 'carried': 0}, [1037, 0, 0]), ('normal control', {'lines': [376], 'threshold': 500, 'carried': 0}, [0, 0, 376]), ('normal control', {'lines': [458], 'threshold': 100, 'carried': 0}, [458, 0, 0]), ('normal control', {'lines': [239], 'threshold': 100, 'carried': 0}, [239, 0, 0])], [('regression', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202]), ('regression', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('partial-repair probe', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('partial-repair probe', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [336], 'threshold': 100, 'carried': 0}, [336, 0, 0]), ('normal control', {'lines': [41], 'threshold': 500, 'carried': 0}, [0, 0, 41]), ('normal control', {'lines': [360], 'threshold': 500, 'carried': 0}, [0, 0, 360]), ('normal control', {'lines': [263, 589], 'threshold': 100, 'carried': 0}, [852, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Small deferred prorations from earlier cycles are never billed.

Input {lines, threshold, carried}. Pending = carried + sum(lines). Pending >= threshold is invoiced now; pending <= -threshold is credited to the balance now; anything in between is carried to the next cycle. Return [invoiced, credited, carry].

split: open-access


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

N = 1
observations = []
def solve(x):
    pending = x['carried'] + sum(x['lines'])
    if pending > x['threshold']:
        return [pending, 0, 0]
    if pending <= -x['threshold']:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('normal control', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('normal control', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('normal control', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265])], [('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('regression (boundary)', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('partial-repair probe', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('partial-repair probe', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('normal control', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('normal control', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('normal control', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202])], [('regression (boundary)', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('regression (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('partial-repair probe', {'lines': [2, -134, -347, 119], 'threshold': 50, 'carried': 0}, [0, 360, 0]), ('partial-repair probe', {'lines': [-325], 'threshold': 500, 'carried': -499}, [0, 824, 0]), ('normal control', {'lines': [3], 'threshold': 100, 'carried': 49}, [0, 0, 52]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0]), ('normal control', {'lines': [184, -104], 'threshold': 50, 'carried': 0}, [80, 0, 0]), ('normal control', {'lines': [131], 'threshold': 100, 'carried': 99}, [230, 0, 0])], [('regression (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('regression (boundary)', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('partial-repair probe', {'lines': [218, -553], 'threshold': 100, 'carried': -99}, [0, 434, 0]), ('partial-repair probe', {'lines': [-336, -428, -165, 586], 'threshold': 50, 'carried': 49}, [0, 294, 0]), ('normal control', {'lines': [140, 140], 'threshold': 100, 'carried': -99}, [181, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 49}, [0, 0, 49]), ('normal control', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('normal control', {'lines': [489], 'threshold': 50, 'carried': -49}, [440, 0, 0])], [('regression (boundary)', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('partial-repair probe', {'lines': [111, -322], 'threshold': 500, 'carried': -499}, [0, 710, 0]), ('partial-repair probe', {'lines': [-510, 83, 385, -249], 'threshold': 100, 'carried': -99}, [0, 390, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('normal control', {'lines': [462, -187], 'threshold': 50, 'carried': 49}, [324, 0, 0]), ('normal control', {'lines': [425, 134, -443, 185], 'threshold': 100, 'carried': -99}, [202, 0, 0]), ('normal control', {'lines': [-98, 187, -568, 507], 'threshold': 100, 'carried': 99}, [127, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    pending = x['carried'] + sum(x['lines'])
    if abs(pending) >= x['threshold']:
        return [pending, 0, 0]
    if pending <= -x['threshold']:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('partial-repair probe (boundary)', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('normal control', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('normal control', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('normal control', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265])], [('regression (boundary)', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('regression (boundary)', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('partial-repair probe', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0]), ('partial-repair probe', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('normal control', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('normal control', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('normal control', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202])], [('regression (boundary)', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('regression (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('partial-repair probe', {'lines': [2, -134, -347, 119], 'threshold': 50, 'carried': 0}, [0, 360, 0]), ('partial-repair probe', {'lines': [-325], 'threshold': 500, 'carried': -499}, [0, 824, 0]), ('normal control', {'lines': [3], 'threshold': 100, 'carried': 49}, [0, 0, 52]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0]), ('normal control', {'lines': [184, -104], 'threshold': 50, 'carried': 0}, [80, 0, 0]), ('normal control', {'lines': [131], 'threshold': 100, 'carried': 99}, [230, 0, 0])], [('regression (boundary)', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('regression (boundary)', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('partial-repair probe', {'lines': [218, -553], 'threshold': 100, 'carried': -99}, [0, 434, 0]), ('partial-repair probe', {'lines': [-336, -428, -165, 586], 'threshold': 50, 'carried': 49}, [0, 294, 0]), ('normal control', {'lines': [140, 140], 'threshold': 100, 'carried': -99}, [181, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 49}, [0, 0, 49]), ('normal control', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('normal control', {'lines': [489], 'threshold': 50, 'carried': -49}, [440, 0, 0])], [('regression (boundary)', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('regression (boundary)', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('partial-repair probe', {'lines': [111, -322], 'threshold': 500, 'carried': -499}, [0, 710, 0]), ('partial-repair probe', {'lines': [-510, 83, 385, -249], 'threshold': 100, 'carried': -99}, [0, 390, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('normal control', {'lines': [462, -187], 'threshold': 50, 'carried': 49}, [324, 0, 0]), ('normal control', {'lines': [425, 134, -443, 185], 'threshold': 100, 'carried': -99}, [202, 0, 0]), ('normal control', {'lines': [-98, 187, -568, 507], 'threshold': 100, 'carried': 99}, [127, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Pending amounts exactly at the threshold are carried instead of invoiced.

Input {lines, threshold, carried}. Pending = carried + sum(lines). Pending >= threshold is invoiced now; pending <= -threshold is credited to the balance now; anything in between is carried to the next cycle. Return [invoiced, credited, carry].

split: open-access


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

N = 1
observations = []
def solve(x):
    pending = x['carried'] + sum(x['lines'])
    if pending >= x['threshold']:
        return [pending, 0, 0]
    if pending < 0:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265]), ('regression', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [-336, -428, -165, 586], 'threshold': 50, 'carried': 49}, [0, 294, 0]), ('boundary control', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('boundary control', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('normal control', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('normal control', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('normal control', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0])], [('regression', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('regression', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('partial-repair probe', {'lines': [-451, 120, 343, -367], 'threshold': 100, 'carried': 99}, [0, 256, 0]), ('partial-repair probe', {'lines': [179, 224, -295, -260], 'threshold': 50, 'carried': 26}, [0, 126, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('normal control', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('normal control', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('normal control', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('normal control', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202])], [('regression', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('regression', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('partial-repair probe', {'lines': [-362, -501], 'threshold': 100, 'carried': 99}, [0, 764, 0]), ('partial-repair probe', {'lines': [-584], 'threshold': 100, 'carried': 99}, [0, 485, 0]), ('boundary control', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [2, -134, -347, 119], 'threshold': 50, 'carried': 0}, [0, 360, 0]), ('normal control', {'lines': [-325], 'threshold': 500, 'carried': -499}, [0, 824, 0]), ('normal control', {'lines': [3], 'threshold': 100, 'carried': 49}, [0, 0, 52]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0])], [('regression', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('regression', {'lines': [295], 'threshold': 500, 'carried': -499}, [0, 0, -204]), ('partial-repair probe', {'lines': [-209, -185, -52], 'threshold': 50, 'carried': 49}, [0, 397, 0]), ('partial-repair probe', {'lines': [-560, 416], 'threshold': 50, 'carried': 49}, [0, 95, 0]), ('boundary control', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('boundary control', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('normal control', {'lines': [184, -104], 'threshold': 50, 'carried': 0}, [80, 0, 0]), ('normal control', {'lines': [131], 'threshold': 100, 'carried': 99}, [230, 0, 0]), ('normal control', {'lines': [140, 140], 'threshold': 100, 'carried': -99}, [181, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 49}, [0, 0, 49])], [('regression', {'lines': [295], 'threshold': 500, 'carried': -499}, [0, 0, -204]), ('regression', {'lines': [], 'threshold': 50, 'carried': -49}, [0, 0, -49]), ('partial-repair probe', {'lines': [-500, -443, -151], 'threshold': 100, 'carried': 79}, [0, 1015, 0]), ('partial-repair probe', {'lines': [-319, -173, -554], 'threshold': 500, 'carried': 499}, [0, 547, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('normal control', {'lines': [218, -553], 'threshold': 100, 'carried': -99}, [0, 434, 0]), ('normal control', {'lines': [489], 'threshold': 50, 'carried': -49}, [440, 0, 0]), ('normal control', {'lines': [462, -187], 'threshold': 50, 'carried': 49}, [324, 0, 0]), ('normal control', {'lines': [425, 134, -443, 185], 'threshold': 100, 'carried': -99}, [202, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    pending = x['carried'] + sum(x['lines'])
    if pending >= x['threshold']:
        return [pending, 0, 0]
    if pending <= -x['threshold'] and x['carried'] <= 0:
        return [0, -pending, 0]
    return [0, 0, pending]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'lines': [-381, 116], 'threshold': 500, 'carried': 0}, [0, 0, -265]), ('regression', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('partial-repair probe (boundary)', {'lines': [-60], 'threshold': 50, 'carried': 10}, [0, 50, 0]), ('partial-repair probe', {'lines': [-336, -428, -165, 586], 'threshold': 50, 'carried': 49}, [0, 294, 0]), ('boundary control', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('boundary control', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('normal control', {'lines': [61, 426, -138, 400], 'threshold': 50, 'carried': 0}, [749, 0, 0]), ('normal control', {'lines': [], 'threshold': 500, 'carried': 499}, [0, 0, 499]), ('normal control', {'lines': [518, -386], 'threshold': 500, 'carried': 499}, [631, 0, 0]), ('normal control', {'lines': [340, -379, -241], 'threshold': 50, 'carried': -6}, [0, 286, 0])], [('regression', {'lines': [], 'threshold': 500, 'carried': -499}, [0, 0, -499]), ('regression', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('partial-repair probe', {'lines': [-451, 120, 343, -367], 'threshold': 100, 'carried': 99}, [0, 256, 0]), ('partial-repair probe', {'lines': [179, 224, -295, -260], 'threshold': 50, 'carried': 26}, [0, 126, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('normal control', {'lines': [-497, -497, -32, -352], 'threshold': 50, 'carried': -49}, [0, 1427, 0]), ('normal control', {'lines': [-95, 305], 'threshold': 100, 'carried': 0}, [210, 0, 0]), ('normal control', {'lines': [145, -201, 592, 509], 'threshold': 500, 'carried': 0}, [1045, 0, 0]), ('normal control', {'lines': [382, -579, 100], 'threshold': 500, 'carried': 299}, [0, 0, 202])], [('regression', {'lines': [], 'threshold': 100, 'carried': -99}, [0, 0, -99]), ('regression', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('partial-repair probe', {'lines': [-362, -501], 'threshold': 100, 'carried': 99}, [0, 764, 0]), ('partial-repair probe', {'lines': [-584], 'threshold': 100, 'carried': 99}, [0, 485, 0]), ('boundary control', {'lines': [-100], 'threshold': 100, 'carried': 0}, [0, 100, 0]), ('boundary control', {'lines': [40, 10], 'threshold': 50, 'carried': 0}, [50, 0, 0]), ('normal control', {'lines': [2, -134, -347, 119], 'threshold': 50, 'carried': 0}, [0, 360, 0]), ('normal control', {'lines': [-325], 'threshold': 500, 'carried': -499}, [0, 824, 0]), ('normal control', {'lines': [3], 'threshold': 100, 'carried': 49}, [0, 0, 52]), ('normal control', {'lines': [481], 'threshold': 100, 'carried': 0}, [481, 0, 0])], [('regression', {'lines': [], 'threshold': 50, 'carried': -41}, [0, 0, -41]), ('regression', {'lines': [295], 'threshold': 500, 'carried': -499}, [0, 0, -204]), ('partial-repair probe', {'lines': [-209, -185, -52], 'threshold': 50, 'carried': 49}, [0, 397, 0]), ('partial-repair probe', {'lines': [-560, 416], 'threshold': 50, 'carried': 49}, [0, 95, 0]), ('boundary control', {'lines': [30], 'threshold': 50, 'carried': 20}, [50, 0, 0]), ('boundary control', {'lines': [], 'threshold': 100, 'carried': 100}, [100, 0, 0]), ('normal control', {'lines': [184, -104], 'threshold': 50, 'carried': 0}, [80, 0, 0]), ('normal control', {'lines': [131], 'threshold': 100, 'carried': 99}, [230, 0, 0]), ('normal control', {'lines': [140, 140], 'threshold': 100, 'carried': -99}, [181, 0, 0]), ('normal control', {'lines': [], 'threshold': 50, 'carried': 49}, [0, 0, 49])], [('regression', {'lines': [295], 'threshold': 500, 'carried': -499}, [0, 0, -204]), ('regression', {'lines': [], 'threshold': 50, 'carried': -49}, [0, 0, -49]), ('partial-repair probe', {'lines': [-500, -443, -151], 'threshold': 100, 'carried': 79}, [0, 1015, 0]), ('partial-repair probe', {'lines': [-319, -173, -554], 'threshold': 500, 'carried': 499}, [0, 547, 0]), ('boundary control', {'lines': [250, 250], 'threshold': 500, 'carried': 0}, [500, 0, 0]), ('boundary control', {'lines': [-50, 150], 'threshold': 100, 'carried': 0}, [100, 0, 0]), ('normal control', {'lines': [218, -553], 'threshold': 100, 'carried': -99}, [0, 434, 0]), ('normal control', {'lines': [489], 'threshold': 50, 'carried': -49}, [440, 0, 0]), ('normal control', {'lines': [462, -187], 'threshold': 50, 'carried': 49}, [324, 0, 0]), ('normal control', {'lines': [425, 134, -443, 185], 'threshold': 100, 'carried': -99}, [202, 0, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Tiny credits are applied to balances immediately instead of being carried.

Input {lines, threshold, carried}. Pending = carried + sum(lines). Pending >= threshold is invoiced now; pending <= -threshold is credited to the balance now; anything in between is carried to the next cycle. Return [invoiced, credited, carry].

split: open-access


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

N = 1
observations = []
def solve(x):
    end = x['period_days'] if x['cancel'] is None else min(x['cancel'], x['period_days'])
    active = max(0, end - x['from'])
    flat = (x['flat'] * active * 2 + x['period_days']) // (2 * x['period_days'])
    usage = sum(q for d, q in x['events'] if x['from'] <= d < end) * x['unit'] * active // x['period_days']
    return [flat, usage]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'flat': 2900, 'period_days': 30, 'from': 15, 'cancel': None, 'unit': 5, 'events': [[26, 260], [12, 213]]}, [1450, 1300]), ('regression', {'flat': 12170, 'period_days': 30, 'from': 3, 'cancel': None, 'unit': 12, 'events': [[23, 155], [28, 348], [21, 236]]}, [10953, 8868]), ('partial-repair probe', {'flat': 2900, 'period_days': 31, 'from': 23, 'cancel': None, 'unit': 12, 'events': [[13, 491]]}, [748, 0]), ('partial-repair probe', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('normal control', {'flat': 8044, 'period_days': 31, 'from': 14, 'cancel': None, 'unit': 5, 'events': []}, [4411, 0]), ('normal control', {'flat': 2900, 'period_days': 31, 'from': 0, 'cancel': None, 'unit': 5, 'events': [[10, 43], [0, 320]]}, [2900, 1815]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 10, 'cancel': None, 'unit': 5, 'events': []}, [1933, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 26, 'cancel': 17, 'unit': 1, 'events': []}, [0, 0])], [('regression', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('regression', {'flat': 12170, 'period_days': 30, 'from': 3, 'cancel': None, 'unit': 12, 'events': [[23, 155], [28, 348], [21, 236]]}, [10953, 8868]), ('partial-repair probe', {'flat': 6972, 'period_days': 31, 'from': 29, 'cancel': 18, 'unit': 12, 'events': [[29, 170]]}, [0, 0]), ('partial-repair probe', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 7, 'cancel': 17, 'unit': 12, 'events': []}, [967, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 29, 'cancel': 2, 'unit': 5, 'events': []}, [0, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 11, 'cancel': None, 'unit': 1, 'events': []}, [6270, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 9, 'cancel': None, 'unit': 1, 'events': []}, [7026, 0])], [('regression', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('regression', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 2, 'cancel': 7, 'unit': 12, 'events': [[8, 9], [1, 189]]}, [1597, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 7, 'cancel': 9, 'unit': 12, 'events': []}, [639, 0]), ('normal control', {'flat': 11296, 'period_days': 30, 'from': 23, 'cancel': None, 'unit': 1, 'events': []}, [2636, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 25, 'cancel': 29, 'unit': 12, 'events': []}, [1320, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 9, 'cancel': None, 'unit': 5, 'events': []}, [7026, 0])], [('regression', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('regression', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 2, 'cancel': 7, 'unit': 12, 'events': [[8, 9], [1, 189]]}, [1597, 0]), ('partial-repair probe', {'flat': 11245, 'period_days': 30, 'from': 4, 'cancel': None, 'unit': 5, 'events': [[15, 26], [32, 318], [2, 320], [5, 358]]}, [9746, 1920]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 29, 'cancel': 16, 'unit': 12, 'events': []}, [0, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 26, 'cancel': None, 'unit': 12, 'events': []}, [387, 0]), ('normal control', {'flat': 8533, 'period_days': 30, 'from': 5, 'cancel': 4, 'unit': 12, 'events': []}, [0, 0]), ('normal control', {'flat': 3099, 'period_days': 30, 'from': 5, 'cancel': None, 'unit': 1, 'events': []}, [2583, 0])], [('regression', {'flat': 11245, 'period_days': 30, 'from': 4, 'cancel': None, 'unit': 5, 'events': [[15, 26], [32, 318], [2, 320], [5, 358]]}, [9746, 1920]), ('regression', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('partial-repair probe', {'flat': 6064, 'period_days': 30, 'from': 12, 'cancel': 13, 'unit': 12, 'events': [[26, 133], [7, 193]]}, [202, 0]), ('partial-repair probe', {'flat': 2900, 'period_days': 30, 'from': 26, 'cancel': None, 'unit': 5, 'events': [[15, 372], [6, 377], [28, 0]]}, [387, 0]), ('normal control', {'flat': 19748, 'period_days': 30, 'from': 10, 'cancel': None, 'unit': 5, 'events': []}, [13165, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 28, 'cancel': None, 'unit': 12, 'events': []}, [958, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 22, 'cancel': None, 'unit': 5, 'events': []}, [773, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 8, 'cancel': None, 'unit': 1, 'events': []}, [2127, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    end = x['period_days'] if x['cancel'] is None else min(x['cancel'], x['period_days'])
    active = max(0, end - x['from'])
    flat = (x['flat'] * active * 2 + x['period_days']) // (2 * x['period_days'])
    usage = sum(q for d, q in x['events']) * x['unit']
    return [flat, usage]
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[('regression', {'flat': 2900, 'period_days': 30, 'from': 15, 'cancel': None, 'unit': 5, 'events': [[26, 260], [12, 213]]}, [1450, 1300]), ('regression', {'flat': 12170, 'period_days': 30, 'from': 3, 'cancel': None, 'unit': 12, 'events': [[23, 155], [28, 348], [21, 236]]}, [10953, 8868]), ('partial-repair probe', {'flat': 2900, 'period_days': 31, 'from': 23, 'cancel': None, 'unit': 12, 'events': [[13, 491]]}, [748, 0]), ('partial-repair probe', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('normal control', {'flat': 8044, 'period_days': 31, 'from': 14, 'cancel': None, 'unit': 5, 'events': []}, [4411, 0]), ('normal control', {'flat': 2900, 'period_days': 31, 'from': 0, 'cancel': None, 'unit': 5, 'events': [[10, 43], [0, 320]]}, [2900, 1815]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 10, 'cancel': None, 'unit': 5, 'events': []}, [1933, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 26, 'cancel': 17, 'unit': 1, 'events': []}, [0, 0])], [('regression', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('regression', {'flat': 12170, 'period_days': 30, 'from': 3, 'cancel': None, 'unit': 12, 'events': [[23, 155], [28, 348], [21, 236]]}, [10953, 8868]), ('partial-repair probe', {'flat': 6972, 'period_days': 31, 'from': 29, 'cancel': 18, 'unit': 12, 'events': [[29, 170]]}, [0, 0]), ('partial-repair probe', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 7, 'cancel': 17, 'unit': 12, 'events': []}, [967, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 29, 'cancel': 2, 'unit': 5, 'events': []}, [0, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 11, 'cancel': None, 'unit': 1, 'events': []}, [6270, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 9, 'cancel': None, 'unit': 1, 'events': []}, [7026, 0])], [('regression', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('regression', {'flat': 7431, 'period_days': 31, 'from': 30, 'cancel': None, 'unit': 12, 'events': [[6, 380], [30, 5], [32, 257], [4, 247]]}, [240, 60]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 2, 'cancel': 7, 'unit': 12, 'events': [[8, 9], [1, 189]]}, [1597, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 7, 'cancel': 9, 'unit': 12, 'events': []}, [639, 0]), ('normal control', {'flat': 11296, 'period_days': 30, 'from': 23, 'cancel': None, 'unit': 1, 'events': []}, [2636, 0]), ('normal control', {'flat': 9900, 'period_days': 30, 'from': 25, 'cancel': 29, 'unit': 12, 'events': []}, [1320, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 9, 'cancel': None, 'unit': 5, 'events': []}, [7026, 0])], [('regression', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('regression', {'flat': 2900, 'period_days': 31, 'from': 7, 'cancel': None, 'unit': 5, 'events': [[19, 421], [6, 32], [33, 481]]}, [2245, 2105]), ('partial-repair probe', {'flat': 9900, 'period_days': 31, 'from': 2, 'cancel': 7, 'unit': 12, 'events': [[8, 9], [1, 189]]}, [1597, 0]), ('partial-repair probe', {'flat': 11245, 'period_days': 30, 'from': 4, 'cancel': None, 'unit': 5, 'events': [[15, 26], [32, 318], [2, 320], [5, 358]]}, [9746, 1920]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 29, 'cancel': 16, 'unit': 12, 'events': []}, [0, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 26, 'cancel': None, 'unit': 12, 'events': []}, [387, 0]), ('normal control', {'flat': 8533, 'period_days': 30, 'from': 5, 'cancel': 4, 'unit': 12, 'events': []}, [0, 0]), ('normal control', {'flat': 3099, 'period_days': 30, 'from': 5, 'cancel': None, 'unit': 1, 'events': []}, [2583, 0])], [('regression', {'flat': 11245, 'period_days': 30, 'from': 4, 'cancel': None, 'unit': 5, 'events': [[15, 26], [32, 318], [2, 320], [5, 358]]}, [9746, 1920]), ('regression', {'flat': 9900, 'period_days': 31, 'from': 22, 'cancel': None, 'unit': 12, 'events': [[18, 186], [30, 275], [20, 173], [26, 168]]}, [2874, 5316]), ('partial-repair probe', {'flat': 6064, 'period_days': 30, 'from': 12, 'cancel': 13, 'unit': 12, 'events': [[26, 133], [7, 193]]}, [202, 0]), ('partial-repair probe', {'flat': 2900, 'period_days': 30, 'from': 26, 'cancel': None, 'unit': 5, 'events': [[15, 372], [6, 377], [28, 0]]}, [387, 0]), ('normal control', {'flat': 19748, 'period_days': 30, 'from': 10, 'cancel': None, 'unit': 5, 'events': []}, [13165, 0]), ('normal control', {'flat': 9900, 'period_days': 31, 'from': 28, 'cancel': None, 'unit': 12, 'events': []}, [958, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 22, 'cancel': None, 'unit': 5, 'events': []}, [773, 0]), ('normal control', {'flat': 2900, 'period_days': 30, 'from': 8, 'cancel': None, 'unit': 1, 'events': []}, [2127, 0])]]
for i, (label, args, expected) in enumerate(fixtures[N-1]):
    check("%s %d" % (label, i), solve(args), expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Late joiners are billed for only a fraction of the usage they consumed.

Input {flat, period_days, from, cancel|None, unit, events: [[day, qty]]}. Active window is [from, end) with end = min(cancel, period_days) or period_days. The flat fee is prorated by active days half-up; usage is not prorated: every event inside the window is billed qty*unit. Return [flat_charge, usage_charge].

split: open-access