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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}], ['control 6', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 500, 'services'], [1000, 700, 'services'], [1999, 700, 'sales'], [2500, 2000, 'sales']]}, {'receivable': 5504, 'net': 4891, 'tax': {'500': 0, '700': 196, '2000': 417}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [999, 0, 'sales'], [1000, 1900, 'sales'], [5, 0, 'sales'], [2500, 500, 'sales']]}, {'receivable': 16849, 'net': 14427, 'tax': {'2100': 2143, '0': 0, '1900': 160, '500': 119}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [1000, 700, 'sales'], [15, 1900, 'services'], [105, 2000, 'sales'], [1999, 2100, 'services']]}, {'receivable': 18570, 'net': 15464, 'tax': {'2100': 3012, '700': 70, '1900': 3, '2000': 21}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 - bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}], ['control 6', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 500, 'services'], [1000, 700, 'services'], [1999, 700, 'sales'], [2500, 2000, 'sales']]}, {'receivable': 5504, 'net': 4891, 'tax': {'500': 0, '700': 196, '2000': 417}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [999, 0, 'sales'], [1000, 1900, 'sales'], [5, 0, 'sales'], [2500, 500, 'sales']]}, {'receivable': 16849, 'net': 14427, 'tax': {'2100': 2143, '0': 0, '1900': 160, '500': 119}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [1000, 700, 'sales'], [15, 1900, 'services'], [105, 2000, 'sales'], [1999, 2100, 'services']]}, {'receivable': 18570, 'net': 15464, 'tax': {'2100': 3012, '700': 70, '1900': 3, '2000': 21}}]], [['regression: inclusive line divisor', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Tax extracted from tax-inclusive prices is too high.

x = {'mode': 'inclusive'|'exclusive', 'rounding': 'line'|'invoice', 'lines': [[amount, rate_bp, account]]}. Exclusive: tax = amount*bp/10000. Inclusive: tax = amount*bp/(10000+bp). Rounding is half-up to the cent, per line ('line', summed per rate) or once per rate on the summed amounts ('invoice'). Tax is keyed by str(rate_bp). Inclusive: receivable = sum amounts, net = receivable - total tax. Exclusive: net = sum amounts, receivable = net + total tax. Return {'receivable', 'net', 'tax'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['regression: invoice-level divisor, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['regression: invoice-level divisor, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 1900, 'services'], [1000, 0, 'sales'], [2500, 700, 'services'], [1000, 1900, 'services'], [105, 2100, 'services']]}, {'receivable': 5604, 'net': 5103, 'tax': {'1900': 319, '0': 0, '700': 164, '2100': 18}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 700, 'services'], [105, 0, 'sales'], [5, 1900, 'services']]}, {'receivable': 2786, 'net': 2610, 'tax': {'700': 175, '0': 0, '1900': 1}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

meaning: Fixture-count band, not measured model difficulty

deps: stdlib

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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, denom)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['regression: invoice-level divisor, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['regression: invoice-level divisor, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 1900, 'services'], [1000, 0, 'sales'], [2500, 700, 'services'], [1000, 1900, 'services'], [105, 2100, 'services']]}, {'receivable': 5604, 'net': 5103, 'tax': {'1900': 319, '0': 0, '700': 164, '2100': 18}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}]], [['regression: invoice-level divisor', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}], ['control 6', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 700, 'services'], [105, 0, 'sales'], [5, 1900, 'services']]}, {'receivable': 2786, 'net': 2610, 'tax': {'700': 175, '0': 0, '1900': 1}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Invoice-rounded inclusive tax differs from line-rounded tax by far more than a cent.

x = {'mode': 'inclusive'|'exclusive', 'rounding': 'line'|'invoice', 'lines': [[amount, rate_bp, account]]}. Exclusive: tax = amount*bp/10000. Inclusive: tax = amount*bp/(10000+bp). Rounding is half-up to the cent, per line ('line', summed per rate) or once per rate on the summed amounts ('invoice'). Tax is keyed by str(rate_bp). Inclusive: receivable = sum amounts, net = receivable - total tax. Exclusive: net = sum amounts, receivable = net + total tax. Return {'receivable', 'net', 'tax'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r > d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 6', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 2100, 'services'], [5, 2000, 'sales'], [12345, 2000, 'sales']]}, {'receivable': 12365, 'net': 10303, 'tax': {'2100': 3, '2000': 2059}}], ['regression: half-cent tie, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['regression: half-cent tie, partial-repair probe', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [999, 2000, 'sales'], [105, 2000, 'sales']]}, {'receivable': 1209, 'net': 1006, 'tax': {'2100': 18, '2000': 185}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if r > 0 else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 6', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 2100, 'services'], [5, 2000, 'sales'], [12345, 2000, 'sales']]}, {'receivable': 12365, 'net': 10303, 'tax': {'2100': 3, '2000': 2059}}], ['regression: half-cent tie, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['regression: half-cent tie, partial-repair probe', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [999, 2000, 'sales'], [105, 2000, 'sales']]}, {'receivable': 1209, 'net': 1006, 'tax': {'2100': 18, '2000': 185}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}]], [['regression: half-cent tie', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 6', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Tax on exact half-cent amounts is rounded down.

x = {'mode': 'inclusive'|'exclusive', 'rounding': 'line'|'invoice', 'lines': [[amount, rate_bp, account]]}. Exclusive: tax = amount*bp/10000. Inclusive: tax = amount*bp/(10000+bp). Rounding is half-up to the cent, per line ('line', summed per rate) or once per rate on the summed amounts ('invoice'). Tax is keyed by str(rate_bp). Inclusive: receivable = sum amounts, net = receivable - total tax. Exclusive: net = sum amounts, receivable = net + total tax. Return {'receivable', 'net', 'tax'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 700, 'services'], [2500, 2100, 'sales'], [15, 0, 'services'], [105, 700, 'services'], [5, 700, 'services']]}, {'receivable': 5833, 'net': 5125, 'tax': {'700': 183, '2100': 525, '0': 0}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 2100, 'sales'], [15, 2100, 'sales'], [5, 500, 'sales'], [12345, 0, 'sales']]}, {'receivable': 12370, 'net': 12366, 'tax': {'2100': 4, '500': 0, '0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2000, 'sales'], [12345, 2000, 'services'], [5, 2000, 'sales'], [15, 700, 'services']]}, {'receivable': 24710, 'net': 20592, 'tax': {'2000': 4117, '700': 1}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2100, 'services'], [12345, 700, 'services'], [12345, 2100, 'services'], [105, 700, 'services']]}, {'receivable': 28266, 'net': 24800, 'tax': {'2100': 2594, '700': 872}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 500, 'services'], [2500, 2100, 'sales'], [1999, 2100, 'services']]}, {'receivable': 4514, 'net': 3732, 'tax': {'500': 1, '2100': 781}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 1900, 'services'], [105, 1900, 'sales'], [12345, 700, 'services'], [12345, 2100, 'services'], [12345, 2100, 'services']]}, {'receivable': 46184, 'net': 39640, 'tax': {'1900': 495, '700': 864, '2100': 5185}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [999, 0, 'sales'], [1000, 1900, 'sales'], [5, 0, 'sales'], [2500, 500, 'sales']]}, {'receivable': 16849, 'net': 14427, 'tax': {'2100': 2143, '0': 0, '1900': 160, '500': 119}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1000, 2000, 'sales'], [1999, 1900, 'sales'], [2500, 1900, 'sales']]}, {'receivable': 5499, 'net': 4614, 'tax': {'2000': 167, '1900': 718}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 4', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 1900, 'services'], [1000, 0, 'sales'], [2500, 700, 'services'], [1000, 1900, 'services'], [105, 2100, 'services']]}, {'receivable': 5604, 'net': 5103, 'tax': {'1900': 319, '0': 0, '700': 164, '2100': 18}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 1900, 'sales'], [999, 1900, 'sales'], [105, 700, 'sales'], [2500, 500, 'services'], [12345, 1900, 'sales']]}, {'receivable': 21591, 'net': 18449, 'tax': {'1900': 3010, '700': 7, '500': 125}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1000, 700, 'services'], [5, 2000, 'services'], [1999, 2000, 'services']]}, {'receivable': 3004, 'net': 2605, 'tax': {'700': 65, '2000': 334}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in - total_tax
    else:
        receivable = gross_in + sum(rnd(a * b, 10000) for a, b, _ in x['lines'])
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 700, 'services'], [2500, 2100, 'sales'], [15, 0, 'services'], [105, 700, 'services'], [5, 700, 'services']]}, {'receivable': 5833, 'net': 5125, 'tax': {'700': 183, '2100': 525, '0': 0}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[999, 2100, 'sales'], [105, 500, 'services'], [999, 0, 'services']]}, {'receivable': 2103, 'net': 1925, 'tax': {'2100': 173, '500': 5, '0': 0}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 2100, 'sales'], [15, 2100, 'sales'], [5, 500, 'sales'], [12345, 0, 'sales']]}, {'receivable': 12370, 'net': 12366, 'tax': {'2100': 4, '500': 0, '0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2000, 'sales'], [12345, 2000, 'services'], [5, 2000, 'sales'], [15, 700, 'services']]}, {'receivable': 24710, 'net': 20592, 'tax': {'2000': 4117, '700': 1}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2100, 'services'], [12345, 700, 'services'], [12345, 2100, 'services'], [105, 700, 'services']]}, {'receivable': 28266, 'net': 24800, 'tax': {'2100': 2594, '700': 872}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[2500, 500, 'services'], [105, 500, 'sales']]}, {'receivable': 2605, 'net': 2481, 'tax': {'500': 124}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 700, 'services'], [12345, 700, 'services'], [999, 1900, 'sales'], [1999, 2000, 'sales']]}, {'receivable': 15358, 'net': 14056, 'tax': {'700': 809, '1900': 160, '2000': 333}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 500, 'services'], [2500, 2100, 'sales'], [1999, 2100, 'services']]}, {'receivable': 4514, 'net': 3732, 'tax': {'500': 1, '2100': 781}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 1900, 'services'], [105, 1900, 'sales'], [12345, 700, 'services'], [12345, 2100, 'services'], [12345, 2100, 'services']]}, {'receivable': 46184, 'net': 39640, 'tax': {'1900': 495, '700': 864, '2100': 5185}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 4', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [999, 0, 'sales'], [1000, 1900, 'sales'], [5, 0, 'sales'], [2500, 500, 'sales']]}, {'receivable': 16849, 'net': 14427, 'tax': {'2100': 2143, '0': 0, '1900': 160, '500': 119}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1000, 2000, 'sales'], [1999, 1900, 'sales'], [2500, 1900, 'sales']]}, {'receivable': 5499, 'net': 4614, 'tax': {'2000': 167, '1900': 718}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 1', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['control 2', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[5, 0, 'services'], [105, 500, 'sales'], [1000, 700, 'sales'], [5, 2100, 'services'], [5, 0, 'services']]}, {'receivable': 1120, 'net': 1049, 'tax': {'0': 0, '500': 5, '700': 65, '2100': 1}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 700, 'sales']]}, {'receivable': 1999, 'net': 1868, 'tax': {'700': 131}}], ['control 4', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 5', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 1900, 'services'], [1000, 0, 'sales'], [2500, 700, 'services'], [1000, 1900, 'services'], [105, 2100, 'services']]}, {'receivable': 5604, 'net': 5103, 'tax': {'1900': 319, '0': 0, '700': 164, '2100': 18}}]], [['regression: exclusive receivable', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['regression: exclusive receivable, partial-repair probe', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[2500, 1900, 'sales'], [999, 1900, 'sales'], [105, 700, 'sales'], [2500, 500, 'services'], [12345, 1900, 'sales']]}, {'receivable': 21591, 'net': 18449, 'tax': {'1900': 3010, '700': 7, '500': 125}}], ['control 1', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['control 2', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[5, 700, 'services'], [2500, 1900, 'sales'], [1999, 1900, 'services'], [5, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 5508, 'net': 4617, 'tax': {'700': 0, '1900': 718, '500': 0, '2100': 173}}], ['control 3', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [15, 500, 'sales'], [1999, 700, 'sales'], [2500, 2100, 'sales'], [12345, 700, 'services']]}, {'receivable': 18858, 'net': 17138, 'tax': {'2100': 781, '500': 1, '700': 938}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1000, 700, 'services'], [5, 2000, 'services'], [1999, 2000, 'services']]}, {'receivable': 3004, 'net': 2605, 'tax': {'700': 65, '2000': 334}}], ['control 5', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Customers are billed without the tax that is posted to tax payable.

x = {'mode': 'inclusive'|'exclusive', 'rounding': 'line'|'invoice', 'lines': [[amount, rate_bp, account]]}. Exclusive: tax = amount*bp/10000. Inclusive: tax = amount*bp/(10000+bp). Rounding is half-up to the cent, per line ('line', summed per rate) or once per rate on the summed amounts ('invoice'). Tax is keyed by str(rate_bp). Inclusive: receivable = sum amounts, net = receivable - total tax. Exclusive: net = sum amounts, receivable = net + total tax. Return {'receivable', 'net', 'tax'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = gross_in
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 2100, 'services'], [5, 2000, 'sales'], [12345, 2000, 'sales']]}, {'receivable': 12365, 'net': 10303, 'tax': {'2100': 3, '2000': 2059}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 0, 'sales'], [1000, 2100, 'services'], [2500, 2000, 'sales'], [15, 2000, 'sales']]}, {'receivable': 5228, 'net': 4515, 'tax': {'0': 0, '2100': 210, '2000': 503}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [999, 2000, 'sales'], [105, 2000, 'sales']]}, {'receivable': 1209, 'net': 1006, 'tax': {'2100': 18, '2000': 185}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}], ['control 4', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [1000, 700, 'sales'], [15, 1900, 'services'], [105, 2000, 'sales'], [1999, 2100, 'services']]}, {'receivable': 18570, 'net': 15464, 'tax': {'2100': 3012, '700': 70, '1900': 3, '2000': 21}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 700, 'services'], [105, 0, 'sales'], [5, 1900, 'services']]}, {'receivable': 2786, 'net': 2610, 'tax': {'700': 175, '0': 0, '1900': 1}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def rnd(n, d):
        q, r = divmod(n, d)
        return q + (1 if 2 * r >= d else 0)
    incl = x['mode'] == 'inclusive'
    groups = {}
    tax = {}
    for amount, bp, acct in x['lines']:
        denom = 10000 + bp if incl else 10000
        key = str(bp)
        if x['rounding'] == 'line':
            tax[key] = tax.get(key, 0) + rnd(amount * bp, denom)
        groups[key] = groups.get(key, 0) + amount
    if x['rounding'] == 'invoice':
        for key, total in groups.items():
            bp = int(key)
            tax[key] = rnd(total * bp, 10000 + bp if incl else 10000)
    gross_in = sum(groups.values())
    total_tax = sum(tax.values())
    if incl:
        receivable = gross_in
        net = sum(rnd(v * 10000, 10000 + int(k)) for k, v in groups.items())
    else:
        receivable = gross_in + total_tax
        net = gross_in
    return {'receivable': receivable, 'net': net, 'tax': tax}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'services'], [12345, 2100, 'sales'], [2500, 500, 'services'], [105, 2100, 'services']]}, {'receivable': 16949, 'net': 14322, 'tax': {'2100': 2508, '500': 119}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services'], [15, 2000, 'services'], [15, 0, 'sales'], [1000, 2100, 'services'], [15, 500, 'sales']]}, {'receivable': 1060, 'net': 880, 'tax': {'1900': 2, '2000': 3, '0': 0, '2100': 174, '500': 1}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 1900, 'sales'], [15, 700, 'sales']]}, {'receivable': 2991, 'net': 2515, 'tax': {'1900': 475, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[105, 700, 'services'], [5, 700, 'sales'], [5, 500, 'services'], [105, 1900, 'services'], [15, 500, 'services']]}, {'receivable': 263, 'net': 235, 'tax': {'700': 7, '500': 1, '1900': 20}}], ['control 3', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 700, 'services'], [999, 0, 'sales']]}, {'receivable': 3138, 'net': 2998, 'tax': {'700': 140, '0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 2100, 'services'], [999, 2000, 'sales'], [999, 0, 'sales'], [1999, 2100, 'services'], [1000, 2000, 'services']]}, {'receivable': 7027, 'net': 5997, 'tax': {'2100': 630, '2000': 400, '0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 2100, 'sales'], [999, 1900, 'sales'], [999, 0, 'services'], [2500, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 6039, 'net': 5513, 'tax': {'2100': 210, '1900': 190, '0': 0, '500': 125, '700': 1}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 500, 'services'], [15, 700, 'sales']]}, {'receivable': 1014, 'net': 965, 'tax': {'500': 48, '700': 1}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 2100, 'services'], [5, 2000, 'sales'], [12345, 2000, 'sales']]}, {'receivable': 12365, 'net': 10303, 'tax': {'2100': 3, '2000': 2059}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'services']]}, {'receivable': 18, 'net': 15, 'tax': {'1900': 3}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [15, 2100, 'sales'], [1000, 700, 'sales'], [2500, 0, 'services']]}, {'receivable': 16797, 'net': 15860, 'tax': {'700': 934, '2100': 3, '0': 0}}], ['control 3', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[1999, 0, 'sales']]}, {'receivable': 1999, 'net': 1999, 'tax': {'0': 0}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 1900, 'services'], [5, 2000, 'sales']]}, {'receivable': 12, 'net': 10, 'tax': {'1900': 1, '2000': 1}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1000, 0, 'sales'], [1000, 2100, 'services'], [2500, 2000, 'sales'], [15, 2000, 'sales']]}, {'receivable': 5228, 'net': 4515, 'tax': {'0': 0, '2100': 210, '2000': 503}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[12345, 700, 'sales'], [105, 700, 'sales']]}, {'receivable': 12450, 'net': 11636, 'tax': {'700': 814}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[1999, 2100, 'sales'], [105, 2000, 'sales'], [2500, 0, 'services']]}, {'receivable': 4604, 'net': 4239, 'tax': {'2100': 347, '2000': 18, '0': 0}}], ['control 1', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1999, 2100, 'sales'], [5, 2100, 'sales'], [12345, 0, 'services'], [2500, 2000, 'sales']]}, {'receivable': 17770, 'net': 16849, 'tax': {'2100': 421, '0': 0, '2000': 500}}], ['control 2', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[5, 700, 'sales'], [1000, 1900, 'sales'], [15, 2000, 'services'], [1999, 700, 'services'], [15, 0, 'services']]}, {'receivable': 3367, 'net': 3034, 'tax': {'700': 140, '1900': 190, '2000': 3, '0': 0}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[1999, 2000, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 4778, 'net': 3998, 'tax': {'2000': 400, '1900': 380}}], ['control 4', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[999, 0, 'sales']]}, {'receivable': 999, 'net': 999, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[12345, 2100, 'sales'], [1000, 2100, 'services']]}, {'receivable': 16147, 'net': 13345, 'tax': {'2100': 2802}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'invoice', 'lines': [[15, 2100, 'sales'], [15, 2100, 'sales'], [105, 0, 'sales'], [1999, 1900, 'sales']]}, {'receivable': 2134, 'net': 1810, 'tax': {'2100': 5, '0': 0, '1900': 319}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [999, 2000, 'sales'], [105, 2000, 'sales']]}, {'receivable': 1209, 'net': 1006, 'tax': {'2100': 18, '2000': 185}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 1900, 'sales'], [2500, 1900, 'services'], [15, 700, 'sales']]}, {'receivable': 3009, 'net': 2530, 'tax': {'1900': 478, '700': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [1000, 700, 'sales'], [5, 2100, 'services'], [105, 700, 'services'], [105, 2000, 'services']]}, {'receivable': 1427, 'net': 1320, 'tax': {'700': 85, '2100': 1, '2000': 21}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 700, 'services'], [15, 0, 'services'], [1999, 0, 'sales'], [1999, 700, 'services'], [15, 1900, 'sales']]}, {'receivable': 4283, 'net': 4133, 'tax': {'700': 147, '0': 0, '1900': 3}}], ['control 4', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[1000, 0, 'sales']]}, {'receivable': 1000, 'net': 1000, 'tax': {'0': 0}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[12345, 2100, 'sales'], [1000, 700, 'sales'], [15, 1900, 'services'], [105, 2000, 'sales'], [1999, 2100, 'services']]}, {'receivable': 18570, 'net': 15464, 'tax': {'2100': 3012, '700': 70, '1900': 3, '2000': 21}}]], [['regression: inclusive net derivation', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[15, 0, 'sales'], [1999, 0, 'services'], [999, 700, 'services'], [15, 500, 'sales'], [999, 2100, 'sales']]}, {'receivable': 4027, 'net': 3788, 'tax': {'0': 0, '700': 65, '500': 1, '2100': 173}}], ['regression: inclusive net derivation, partial-repair probe', {'mode': 'inclusive', 'rounding': 'line', 'lines': [[105, 2100, 'sales'], [1000, 2000, 'sales'], [1999, 500, 'sales'], [12345, 2000, 'services'], [1000, 500, 'sales']]}, {'receivable': 16449, 'net': 14063, 'tax': {'2100': 18, '2000': 2225, '500': 143}}], ['control 1', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[15, 500, 'sales']]}, {'receivable': 16, 'net': 15, 'tax': {'500': 1}}], ['control 2', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[999, 700, 'services']]}, {'receivable': 1069, 'net': 999, 'tax': {'700': 70}}], ['control 3', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[105, 1900, 'sales'], [12345, 500, 'sales'], [1000, 2000, 'sales'], [999, 700, 'services']]}, {'receivable': 15356, 'net': 14449, 'tax': {'1900': 20, '500': 617, '2000': 200, '700': 70}}], ['control 4', {'mode': 'exclusive', 'rounding': 'invoice', 'lines': [[5, 2000, 'sales'], [5, 700, 'services'], [12345, 500, 'services'], [105, 1900, 'sales']]}, {'receivable': 13098, 'net': 12460, 'tax': {'2000': 1, '700': 0, '500': 617, '1900': 20}}], ['control 5', {'mode': 'exclusive', 'rounding': 'line', 'lines': [[2500, 700, 'services'], [105, 0, 'sales'], [5, 1900, 'services']]}, {'receivable': 2786, 'net': 2610, 'tax': {'700': 175, '0': 0, '1900': 1}}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Inclusive sales book the full gross as revenue while also booking tax.

x = {'mode': 'inclusive'|'exclusive', 'rounding': 'line'|'invoice', 'lines': [[amount, rate_bp, account]]}. Exclusive: tax = amount*bp/10000. Inclusive: tax = amount*bp/(10000+bp). Rounding is half-up to the cent, per line ('line', summed per rate) or once per rate on the summed amounts ('invoice'). Tax is keyed by str(rate_bp). Inclusive: receivable = sum amounts, net = receivable - total tax. Exclusive: net = sum amounts, receivable = net + total tax. Return {'receivable', 'net', 'tax'}.

split: open-access