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

N = 1
observations = []
def solve(x):
    def to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * den, num)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['bid'] if kind == 'asset' else x['ask']
        new = to_home(foreign, rate)
        delta = new - carried
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rate orientation', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 1', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', False, 101, 101], ['a2', 'liability', True, 3, 500]]}, {'balances': [['a0', 250], ['a1', 101], ['a2', 3]], 'unrealized_gain': 747}], ['control 2', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'liability', True, 1000, 500], ['a2', 'asset', False, 3, 6], ['a3', 'liability', True, 5, 500]]}, {'balances': [['a0', 1], ['a1', 1000], ['a2', 6], ['a3', 5]], 'unrealized_gain': -4}], ['control 3', {'bid': [5, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 101, 500], ['a1', 'asset', False, 101, 101], ['a2', 'liability', True, 101, 104], ['a3', 'liability', True, 7, 14], ['a4', 'asset', False, 7, 500]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 101], ['a3', 7], ['a4', 500]], 'unrealized_gain': 10}], ['control 4', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', False, 7, 10], ['a1', 'asset', True, 999, 999], ['a2', 'asset', False, 250, 500], ['a3', 'asset', False, 999, 500], ['a4', 'asset', True, 101, 500]]}, {'balances': [['a0', 10], ['a1', 999], ['a2', 500], ['a3', 500], ['a4', 101]], 'unrealized_gain': -399}], ['control 5', {'bid': [6, 4], 'ask': [8, 4], 'accounts': [['a0', 'asset', False, 5, 10], ['a1', 'asset', False, 7, 500]]}, {'balances': [['a0', 10], ['a1', 500]], 'unrealized_gain': 0}], ['control 6', {'bid': [5, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'liability', False, 3, 6]]}, {'balances': [['a0', 6], ['a1', 6]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['regression: rate orientation, partial-repair probe', {'bid': [15, 8], 'ask': [17, 8], 'accounts': [['a0', 'liability', True, 999, 999], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'liability', True, 101, 202]]}, {'balances': [['a0', 2123], ['a1', 215], ['a2', 13], ['a3', 215]], 'unrealized_gain': -1245}], ['control 1', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 5], ['a1', 'asset', True, 1000, 500], ['a2', 'liability', False, 5, 5], ['a3', 'asset', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 1000], ['a2', 5], ['a3', 1]], 'unrealized_gain': 499}], ['control 3', {'bid': [99, 100], 'ask': [100, 100], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'asset', False, 101, 202], ['a2', 'liability', True, 101, 500], ['a3', 'liability', True, 101, 500], ['a4', 'liability', False, 999, 1002]]}, {'balances': [['a0', 5], ['a1', 202], ['a2', 101], ['a3', 101], ['a4', 1002]], 'unrealized_gain': 798}], ['control 4', {'bid': [2, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', False, 999, 999], ['a1', 'liability', True, 3, 3], ['a2', 'liability', False, 250, 253]]}, {'balances': [['a0', 999], ['a1', 3], ['a2', 253]], 'unrealized_gain': 0}], ['control 5', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', True, 1000, 1000], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 999, 500]]}, {'balances': [['a0', 8], ['a1', 1000], ['a2', 999], ['a3', 500]], 'unrealized_gain': -999}]], [['regression: rate orientation', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['regression: rate orientation, partial-repair probe', {'bid': [12, 10], 'ask': [14, 10], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'liability', True, 1, 2], ['a2', 'liability', True, 3, 3], ['a3', 'liability', True, 999, 1002]]}, {'balances': [['a0', 2000], ['a1', 1], ['a2', 4], ['a3', 1399]], 'unrealized_gain': -397}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'liability', True, 5, 500]]}, {'balances': [['a0', 1], ['a1', 5]], 'unrealized_gain': 496}], ['control 3', {'bid': [10, 10], 'ask': [11, 10], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'liability', False, 1000, 2000], ['a2', 'asset', True, 7, 10]]}, {'balances': [['a0', 999], ['a1', 2000], ['a2', 7]], 'unrealized_gain': -1002}], ['control 4', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'liability', False, 1000, 500], ['a1', 'liability', False, 1, 500], ['a2', 'asset', False, 1, 1], ['a3', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 500], ['a2', 1], ['a3', 999]], 'unrealized_gain': -3}], ['control 5', {'bid': [105, 100], 'ask': [106, 100], 'accounts': [['a0', 'asset', False, 5, 10], ['a1', 'asset', False, 3, 6], ['a2', 'asset', False, 101, 101], ['a3', 'liability', False, 3, 6]]}, {'balances': [['a0', 10], ['a1', 6], ['a2', 101], ['a3', 6]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['regression: rate orientation, partial-repair probe', {'bid': [8, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', False, 250, 253], ['a1', 'liability', True, 5, 8], ['a2', 'asset', False, 5, 10], ['a3', 'liability', True, 5, 8], ['a4', 'liability', True, 999, 1998]]}, {'balances': [['a0', 253], ['a1', 11], ['a2', 10], ['a3', 11], ['a4', 2248]], 'unrealized_gain': -256}], ['control 1', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [9, 8], 'ask': [11, 8], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 2000], ['a1', 1]], 'unrealized_gain': -1}], ['control 3', {'bid': [7, 10], 'ask': [10, 10], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', False, 250, 253]]}, {'balances': [['a0', 8], ['a1', 253]], 'unrealized_gain': 0}], ['control 4', {'bid': [10, 10], 'ask': [11, 10], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 3], ['a1', 250]], 'unrealized_gain': -250}], ['control 5', {'bid': [84, 100], 'ask': [87, 100], 'accounts': [['a0', 'asset', False, 1, 1], ['a1', 'liability', False, 1, 2], ['a2', 'asset', False, 999, 1998]]}, {'balances': [['a0', 1], ['a1', 2], ['a2', 1998]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 1', {'bid': [9, 10], 'ask': [10, 10], 'accounts': [['a0', 'liability', False, 101, 500], ['a1', 'asset', False, 7, 14], ['a2', 'liability', True, 7, 10], ['a3', 'asset', False, 101, 101]]}, {'balances': [['a0', 500], ['a1', 14], ['a2', 7], ['a3', 101]], 'unrealized_gain': 3}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'liability', False, 101, 202]]}, {'balances': [['a0', 7], ['a1', 202]], 'unrealized_gain': -3}], ['control 4', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 1000, 2000], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1000], ['a1', 7]], 'unrealized_gain': -1493}], ['control 5', {'bid': [102, 100], 'ask': [103, 100], 'accounts': [['a0', 'liability', False, 999, 1002], ['a1', 'liability', True, 3, 3]]}, {'balances': [['a0', 1002], ['a1', 3]], 'unrealized_gain': 0}], ['control 6', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', False, 250, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * num // den, 1)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['bid'] if kind == 'asset' else x['ask']
        new = to_home(foreign, rate)
        delta = new - carried
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: rate orientation', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 1', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', False, 101, 101], ['a2', 'liability', True, 3, 500]]}, {'balances': [['a0', 250], ['a1', 101], ['a2', 3]], 'unrealized_gain': 747}], ['control 2', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'liability', True, 1000, 500], ['a2', 'asset', False, 3, 6], ['a3', 'liability', True, 5, 500]]}, {'balances': [['a0', 1], ['a1', 1000], ['a2', 6], ['a3', 5]], 'unrealized_gain': -4}], ['control 3', {'bid': [5, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 101, 500], ['a1', 'asset', False, 101, 101], ['a2', 'liability', True, 101, 104], ['a3', 'liability', True, 7, 14], ['a4', 'asset', False, 7, 500]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 101], ['a3', 7], ['a4', 500]], 'unrealized_gain': 10}], ['control 4', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', False, 7, 10], ['a1', 'asset', True, 999, 999], ['a2', 'asset', False, 250, 500], ['a3', 'asset', False, 999, 500], ['a4', 'asset', True, 101, 500]]}, {'balances': [['a0', 10], ['a1', 999], ['a2', 500], ['a3', 500], ['a4', 101]], 'unrealized_gain': -399}], ['control 5', {'bid': [6, 4], 'ask': [8, 4], 'accounts': [['a0', 'asset', False, 5, 10], ['a1', 'asset', False, 7, 500]]}, {'balances': [['a0', 10], ['a1', 500]], 'unrealized_gain': 0}], ['control 6', {'bid': [5, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'liability', False, 3, 6]]}, {'balances': [['a0', 6], ['a1', 6]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['regression: rate orientation, partial-repair probe', {'bid': [15, 8], 'ask': [17, 8], 'accounts': [['a0', 'liability', True, 999, 999], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'liability', True, 101, 202]]}, {'balances': [['a0', 2123], ['a1', 215], ['a2', 13], ['a3', 215]], 'unrealized_gain': -1245}], ['control 1', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 5], ['a1', 'asset', True, 1000, 500], ['a2', 'liability', False, 5, 5], ['a3', 'asset', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 1000], ['a2', 5], ['a3', 1]], 'unrealized_gain': 499}], ['control 3', {'bid': [99, 100], 'ask': [100, 100], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'asset', False, 101, 202], ['a2', 'liability', True, 101, 500], ['a3', 'liability', True, 101, 500], ['a4', 'liability', False, 999, 1002]]}, {'balances': [['a0', 5], ['a1', 202], ['a2', 101], ['a3', 101], ['a4', 1002]], 'unrealized_gain': 798}], ['control 4', {'bid': [2, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', False, 999, 999], ['a1', 'liability', True, 3, 3], ['a2', 'liability', False, 250, 253]]}, {'balances': [['a0', 999], ['a1', 3], ['a2', 253]], 'unrealized_gain': 0}], ['control 5', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', True, 1000, 1000], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 999, 500]]}, {'balances': [['a0', 8], ['a1', 1000], ['a2', 999], ['a3', 500]], 'unrealized_gain': -999}]], [['regression: rate orientation', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['regression: rate orientation, partial-repair probe', {'bid': [12, 10], 'ask': [14, 10], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'liability', True, 1, 2], ['a2', 'liability', True, 3, 3], ['a3', 'liability', True, 999, 1002]]}, {'balances': [['a0', 2000], ['a1', 1], ['a2', 4], ['a3', 1399]], 'unrealized_gain': -397}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'liability', True, 5, 500]]}, {'balances': [['a0', 1], ['a1', 5]], 'unrealized_gain': 496}], ['control 3', {'bid': [10, 10], 'ask': [11, 10], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'liability', False, 1000, 2000], ['a2', 'asset', True, 7, 10]]}, {'balances': [['a0', 999], ['a1', 2000], ['a2', 7]], 'unrealized_gain': -1002}], ['control 4', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'liability', False, 1000, 500], ['a1', 'liability', False, 1, 500], ['a2', 'asset', False, 1, 1], ['a3', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 500], ['a2', 1], ['a3', 999]], 'unrealized_gain': -3}], ['control 5', {'bid': [105, 100], 'ask': [106, 100], 'accounts': [['a0', 'asset', False, 5, 10], ['a1', 'asset', False, 3, 6], ['a2', 'asset', False, 101, 101], ['a3', 'liability', False, 3, 6]]}, {'balances': [['a0', 10], ['a1', 6], ['a2', 101], ['a3', 6]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['regression: rate orientation, partial-repair probe', {'bid': [8, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', False, 250, 253], ['a1', 'liability', True, 5, 8], ['a2', 'asset', False, 5, 10], ['a3', 'liability', True, 5, 8], ['a4', 'liability', True, 999, 1998]]}, {'balances': [['a0', 253], ['a1', 11], ['a2', 10], ['a3', 11], ['a4', 2248]], 'unrealized_gain': -256}], ['control 1', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [9, 8], 'ask': [11, 8], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 2000], ['a1', 1]], 'unrealized_gain': -1}], ['control 3', {'bid': [7, 10], 'ask': [10, 10], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', False, 250, 253]]}, {'balances': [['a0', 8], ['a1', 253]], 'unrealized_gain': 0}], ['control 4', {'bid': [10, 10], 'ask': [11, 10], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 3], ['a1', 250]], 'unrealized_gain': -250}], ['control 5', {'bid': [84, 100], 'ask': [87, 100], 'accounts': [['a0', 'asset', False, 1, 1], ['a1', 'liability', False, 1, 2], ['a2', 'asset', False, 999, 1998]]}, {'balances': [['a0', 1], ['a1', 2], ['a2', 1998]], 'unrealized_gain': 0}]], [['regression: rate orientation', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 1', {'bid': [9, 10], 'ask': [10, 10], 'accounts': [['a0', 'liability', False, 101, 500], ['a1', 'asset', False, 7, 14], ['a2', 'liability', True, 7, 10], ['a3', 'asset', False, 101, 101]]}, {'balances': [['a0', 500], ['a1', 14], ['a2', 7], ['a3', 101]], 'unrealized_gain': 3}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'liability', False, 101, 202]]}, {'balances': [['a0', 7], ['a1', 202]], 'unrealized_gain': -3}], ['control 4', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 1000, 2000], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1000], ['a1', 7]], 'unrealized_gain': -1493}], ['control 5', {'bid': [102, 100], 'ask': [103, 100], 'accounts': [['a0', 'liability', False, 999, 1002], ['a1', 'liability', True, 3, 3]]}, {'balances': [['a0', 1002], ['a1', 3]], 'unrealized_gain': 0}], ['control 6', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', False, 250, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Revalued balances move inversely to the exchange rate.

x = {'bid': [num, den], 'ask': [num, den], 'accounts': [[name, 'asset'|'liability', monetary, foreign units, carried home cents]]}. Monetary assets are revalued at the bid rate and monetary liabilities at the ask rate: home = foreign * num / den rounded half-up per account. Non-monetary accounts keep their carried amount. Unrealized gain sums (new - carried) for assets and (carried - new) for liabilities. Return {'balances': [[name, home]], 'unrealized_gain'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * num, den)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['bid'] if kind == 'asset' else x['ask']
        new = to_home(foreign, rate)
        delta = new - foreign
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: delta baseline', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 1', {'bid': [8, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 1000, 1000], ['a1', 'asset', False, 7, 7], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 5, 8], ['a4', 'liability', False, 1000, 1000]]}, {'balances': [['a0', 2000], ['a1', 7], ['a2', 500], ['a3', 8], ['a4', 1000]], 'unrealized_gain': 1250}], ['control 2', {'bid': [5, 8], 'ask': [6, 8], 'accounts': [['a0', 'asset', True, 101, 101], ['a1', 'asset', False, 1, 4]]}, {'balances': [['a0', 63], ['a1', 4]], 'unrealized_gain': -38}], ['control 3', {'bid': [17, 10], 'ask': [20, 10], 'accounts': [['a0', 'asset', False, 101, 104], ['a1', 'liability', True, 5, 5], ['a2', 'asset', True, 7, 500], ['a3', 'liability', True, 7, 500]]}, {'balances': [['a0', 104], ['a1', 10], ['a2', 12], ['a3', 14]], 'unrealized_gain': -7}], ['control 4', {'bid': [4, 8], 'ask': [5, 8], 'accounts': [['a0', 'asset', False, 101, 500], ['a1', 'asset', True, 250, 250], ['a2', 'liability', True, 3, 3]]}, {'balances': [['a0', 500], ['a1', 125], ['a2', 2]], 'unrealized_gain': -124}], ['control 5', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', False, 5, 8], ['a2', 'liability', True, 3, 6], ['a3', 'liability', False, 3, 6]]}, {'balances': [['a0', 2], ['a1', 8], ['a2', 8], ['a3', 6]], 'unrealized_gain': -4}], ['control 6', {'bid': [17, 10], 'ask': [20, 10], 'accounts': [['a0', 'liability', True, 250, 250], ['a1', 'liability', False, 7, 500], ['a2', 'asset', True, 250, 250], ['a3', 'asset', True, 1000, 1000]]}, {'balances': [['a0', 500], ['a1', 500], ['a2', 425], ['a3', 1700]], 'unrealized_gain': 625}]], [['regression: delta baseline', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['control 1', {'bid': [9, 4], 'ask': [10, 4], 'accounts': [['a0', 'asset', False, 1, 500], ['a1', 'liability', True, 999, 999]]}, {'balances': [['a0', 500], ['a1', 2498]], 'unrealized_gain': -1499}], ['control 2', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [4, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 5, 8], ['a1', 'asset', False, 999, 1002], ['a2', 'asset', False, 7, 10], ['a3', 'liability', True, 7, 7]]}, {'balances': [['a0', 8], ['a1', 1002], ['a2', 10], ['a3', 12]], 'unrealized_gain': -5}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'liability', True, 250, 250], ['a1', 'asset', True, 7, 7]]}, {'balances': [['a0', 450], ['a1', 11]], 'unrealized_gain': -196}], ['control 5', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 5], ['a1', 'liability', True, 1000, 1000], ['a2', 'liability', True, 3, 3], ['a3', 'asset', False, 999, 999]]}, {'balances': [['a0', 5], ['a1', 2500], ['a2', 8], ['a3', 999]], 'unrealized_gain': -1505}], ['control 6', {'bid': [10, 8], 'ask': [13, 8], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'liability', False, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 2000]], 'unrealized_gain': -1}]], [['regression: delta baseline', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [5, 10], 'ask': [8, 10], 'accounts': [['a0', 'liability', False, 101, 202], ['a1', 'liability', False, 1000, 1003], ['a2', 'liability', True, 5, 5], ['a3', 'asset', False, 5, 500], ['a4', 'liability', False, 101, 202]]}, {'balances': [['a0', 202], ['a1', 1003], ['a2', 4], ['a3', 500], ['a4', 202]], 'unrealized_gain': 1}], ['control 3', {'bid': [9, 8], 'ask': [10, 8], 'accounts': [['a0', 'asset', False, 250, 253], ['a1', 'liability', False, 101, 101], ['a2', 'liability', True, 3, 3], ['a3', 'liability', False, 3, 6], ['a4', 'liability', True, 1000, 1000]]}, {'balances': [['a0', 253], ['a1', 101], ['a2', 4], ['a3', 6], ['a4', 1250]], 'unrealized_gain': -251}], ['control 4', {'bid': [7, 8], 'ask': [8, 8], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'liability', True, 3, 6], ['a2', 'liability', False, 101, 101], ['a3', 'liability', False, 1, 2]]}, {'balances': [['a0', 6], ['a1', 3], ['a2', 101], ['a3', 2]], 'unrealized_gain': -1}], ['control 5', {'bid': [127, 100], 'ask': [130, 100], 'accounts': [['a0', 'asset', False, 250, 250], ['a1', 'liability', True, 5, 5], ['a2', 'asset', False, 5, 500]]}, {'balances': [['a0', 250], ['a1', 7], ['a2', 500]], 'unrealized_gain': -2}], ['control 6', {'bid': [95, 100], 'ask': [98, 100], 'accounts': [['a0', 'asset', False, 1000, 1000], ['a1', 'asset', True, 250, 250]]}, {'balances': [['a0', 1000], ['a1', 238]], 'unrealized_gain': -12}]], [['regression: delta baseline', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['control 1', {'bid': [14, 10], 'ask': [17, 10], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 253], ['a2', 'liability', False, 999, 1002], ['a3', 'asset', False, 7, 10], ['a4', 'asset', False, 101, 101]]}, {'balances': [['a0', 2], ['a1', 350], ['a2', 1002], ['a3', 10], ['a4', 101]], 'unrealized_gain': 99}], ['control 2', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [7, 10], 'ask': [9, 10], 'accounts': [['a0', 'asset', False, 250, 500], ['a1', 'asset', True, 250, 250], ['a2', 'asset', False, 7, 14]]}, {'balances': [['a0', 500], ['a1', 175], ['a2', 14]], 'unrealized_gain': -75}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'liability', False, 250, 250], ['a2', 'liability', False, 250, 500], ['a3', 'liability', False, 7, 500]]}, {'balances': [['a0', 8], ['a1', 250], ['a2', 500], ['a3', 500]], 'unrealized_gain': 3}], ['control 5', {'bid': [7, 10], 'ask': [10, 10], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', False, 250, 253]]}, {'balances': [['a0', 8], ['a1', 253]], 'unrealized_gain': 0}], ['control 6', {'bid': [165, 100], 'ask': [166, 100], 'accounts': [['a0', 'liability', True, 7, 7], ['a1', 'asset', False, 250, 250]]}, {'balances': [['a0', 12], ['a1', 250]], 'unrealized_gain': -5}]], [['regression: delta baseline', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 1', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 14], ['a1', 'asset', True, 7, 7], ['a2', 'liability', True, 101, 104], ['a3', 'asset', True, 3, 6]]}, {'balances': [['a0', 14], ['a1', 7], ['a2', 253], ['a3', 3]], 'unrealized_gain': -152}], ['control 3', {'bid': [7, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', True, 250, 253], ['a1', 'liability', True, 1, 4]]}, {'balances': [['a0', 219], ['a1', 1]], 'unrealized_gain': -31}], ['control 4', {'bid': [7, 8], 'ask': [9, 8], 'accounts': [['a0', 'liability', True, 7, 7], ['a1', 'asset', True, 250, 250], ['a2', 'liability', True, 1000, 1000], ['a3', 'asset', False, 250, 500]]}, {'balances': [['a0', 8], ['a1', 219], ['a2', 1125], ['a3', 500]], 'unrealized_gain': -157}], ['control 5', {'bid': [102, 100], 'ask': [103, 100], 'accounts': [['a0', 'liability', False, 999, 1002], ['a1', 'liability', True, 3, 3]]}, {'balances': [['a0', 1002], ['a1', 3]], 'unrealized_gain': 0}], ['control 6', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', False, 250, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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 to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * num, den)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['bid'] if kind == 'asset' else x['ask']
        new = to_home(foreign, rate)
        delta = new - to_home(carried, rate)
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: delta baseline', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 1', {'bid': [8, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 1000, 1000], ['a1', 'asset', False, 7, 7], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 5, 8], ['a4', 'liability', False, 1000, 1000]]}, {'balances': [['a0', 2000], ['a1', 7], ['a2', 500], ['a3', 8], ['a4', 1000]], 'unrealized_gain': 1250}], ['control 2', {'bid': [5, 8], 'ask': [6, 8], 'accounts': [['a0', 'asset', True, 101, 101], ['a1', 'asset', False, 1, 4]]}, {'balances': [['a0', 63], ['a1', 4]], 'unrealized_gain': -38}], ['control 3', {'bid': [17, 10], 'ask': [20, 10], 'accounts': [['a0', 'asset', False, 101, 104], ['a1', 'liability', True, 5, 5], ['a2', 'asset', True, 7, 500], ['a3', 'liability', True, 7, 500]]}, {'balances': [['a0', 104], ['a1', 10], ['a2', 12], ['a3', 14]], 'unrealized_gain': -7}], ['control 4', {'bid': [4, 8], 'ask': [5, 8], 'accounts': [['a0', 'asset', False, 101, 500], ['a1', 'asset', True, 250, 250], ['a2', 'liability', True, 3, 3]]}, {'balances': [['a0', 500], ['a1', 125], ['a2', 2]], 'unrealized_gain': -124}], ['control 5', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', False, 5, 8], ['a2', 'liability', True, 3, 6], ['a3', 'liability', False, 3, 6]]}, {'balances': [['a0', 2], ['a1', 8], ['a2', 8], ['a3', 6]], 'unrealized_gain': -4}], ['control 6', {'bid': [17, 10], 'ask': [20, 10], 'accounts': [['a0', 'liability', True, 250, 250], ['a1', 'liability', False, 7, 500], ['a2', 'asset', True, 250, 250], ['a3', 'asset', True, 1000, 1000]]}, {'balances': [['a0', 500], ['a1', 500], ['a2', 425], ['a3', 1700]], 'unrealized_gain': 625}]], [['regression: delta baseline', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['control 1', {'bid': [9, 4], 'ask': [10, 4], 'accounts': [['a0', 'asset', False, 1, 500], ['a1', 'liability', True, 999, 999]]}, {'balances': [['a0', 500], ['a1', 2498]], 'unrealized_gain': -1499}], ['control 2', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [4, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 5, 8], ['a1', 'asset', False, 999, 1002], ['a2', 'asset', False, 7, 10], ['a3', 'liability', True, 7, 7]]}, {'balances': [['a0', 8], ['a1', 1002], ['a2', 10], ['a3', 12]], 'unrealized_gain': -5}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'liability', True, 250, 250], ['a1', 'asset', True, 7, 7]]}, {'balances': [['a0', 450], ['a1', 11]], 'unrealized_gain': -196}], ['control 5', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 5, 5], ['a1', 'liability', True, 1000, 1000], ['a2', 'liability', True, 3, 3], ['a3', 'asset', False, 999, 999]]}, {'balances': [['a0', 5], ['a1', 2500], ['a2', 8], ['a3', 999]], 'unrealized_gain': -1505}], ['control 6', {'bid': [10, 8], 'ask': [13, 8], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'liability', False, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 2000]], 'unrealized_gain': -1}]], [['regression: delta baseline', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [5, 10], 'ask': [8, 10], 'accounts': [['a0', 'liability', False, 101, 202], ['a1', 'liability', False, 1000, 1003], ['a2', 'liability', True, 5, 5], ['a3', 'asset', False, 5, 500], ['a4', 'liability', False, 101, 202]]}, {'balances': [['a0', 202], ['a1', 1003], ['a2', 4], ['a3', 500], ['a4', 202]], 'unrealized_gain': 1}], ['control 3', {'bid': [9, 8], 'ask': [10, 8], 'accounts': [['a0', 'asset', False, 250, 253], ['a1', 'liability', False, 101, 101], ['a2', 'liability', True, 3, 3], ['a3', 'liability', False, 3, 6], ['a4', 'liability', True, 1000, 1000]]}, {'balances': [['a0', 253], ['a1', 101], ['a2', 4], ['a3', 6], ['a4', 1250]], 'unrealized_gain': -251}], ['control 4', {'bid': [7, 8], 'ask': [8, 8], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'liability', True, 3, 6], ['a2', 'liability', False, 101, 101], ['a3', 'liability', False, 1, 2]]}, {'balances': [['a0', 6], ['a1', 3], ['a2', 101], ['a3', 2]], 'unrealized_gain': -1}], ['control 5', {'bid': [127, 100], 'ask': [130, 100], 'accounts': [['a0', 'asset', False, 250, 250], ['a1', 'liability', True, 5, 5], ['a2', 'asset', False, 5, 500]]}, {'balances': [['a0', 250], ['a1', 7], ['a2', 500]], 'unrealized_gain': -2}], ['control 6', {'bid': [95, 100], 'ask': [98, 100], 'accounts': [['a0', 'asset', False, 1000, 1000], ['a1', 'asset', True, 250, 250]]}, {'balances': [['a0', 1000], ['a1', 238]], 'unrealized_gain': -12}]], [['regression: delta baseline', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['control 1', {'bid': [14, 10], 'ask': [17, 10], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 253], ['a2', 'liability', False, 999, 1002], ['a3', 'asset', False, 7, 10], ['a4', 'asset', False, 101, 101]]}, {'balances': [['a0', 2], ['a1', 350], ['a2', 1002], ['a3', 10], ['a4', 101]], 'unrealized_gain': 99}], ['control 2', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [7, 10], 'ask': [9, 10], 'accounts': [['a0', 'asset', False, 250, 500], ['a1', 'asset', True, 250, 250], ['a2', 'asset', False, 7, 14]]}, {'balances': [['a0', 500], ['a1', 175], ['a2', 14]], 'unrealized_gain': -75}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'liability', False, 250, 250], ['a2', 'liability', False, 250, 500], ['a3', 'liability', False, 7, 500]]}, {'balances': [['a0', 8], ['a1', 250], ['a2', 500], ['a3', 500]], 'unrealized_gain': 3}], ['control 5', {'bid': [7, 10], 'ask': [10, 10], 'accounts': [['a0', 'asset', False, 5, 8], ['a1', 'asset', False, 250, 253]]}, {'balances': [['a0', 8], ['a1', 253]], 'unrealized_gain': 0}], ['control 6', {'bid': [165, 100], 'ask': [166, 100], 'accounts': [['a0', 'liability', True, 7, 7], ['a1', 'asset', False, 250, 250]]}, {'balances': [['a0', 12], ['a1', 250]], 'unrealized_gain': -5}]], [['regression: delta baseline', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 1', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}], ['control 2', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 14], ['a1', 'asset', True, 7, 7], ['a2', 'liability', True, 101, 104], ['a3', 'asset', True, 3, 6]]}, {'balances': [['a0', 14], ['a1', 7], ['a2', 253], ['a3', 3]], 'unrealized_gain': -152}], ['control 3', {'bid': [7, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', True, 250, 253], ['a1', 'liability', True, 1, 4]]}, {'balances': [['a0', 219], ['a1', 1]], 'unrealized_gain': -31}], ['control 4', {'bid': [7, 8], 'ask': [9, 8], 'accounts': [['a0', 'liability', True, 7, 7], ['a1', 'asset', True, 250, 250], ['a2', 'liability', True, 1000, 1000], ['a3', 'asset', False, 250, 500]]}, {'balances': [['a0', 8], ['a1', 219], ['a2', 1125], ['a3', 500]], 'unrealized_gain': -157}], ['control 5', {'bid': [102, 100], 'ask': [103, 100], 'accounts': [['a0', 'liability', False, 999, 1002], ['a1', 'liability', True, 3, 3]]}, {'balances': [['a0', 1002], ['a1', 3]], 'unrealized_gain': 0}], ['control 6', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', False, 250, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Unrealized gains equal the full converted balance minus foreign units.

x = {'bid': [num, den], 'ask': [num, den], 'accounts': [[name, 'asset'|'liability', monetary, foreign units, carried home cents]]}. Monetary assets are revalued at the bid rate and monetary liabilities at the ask rate: home = foreign * num / den rounded half-up per account. Non-monetary accounts keep their carried amount. Unrealized gain sums (new - carried) for assets and (carried - new) for liabilities. Return {'balances': [[name, home]], 'unrealized_gain'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    def to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * num, den)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['bid']
        new = to_home(foreign, rate)
        delta = new - carried
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bid and ask selection', {'bid': [9, 8], 'ask': [12, 8], 'accounts': [['a0', 'liability', True, 5, 10], ['a1', 'asset', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'asset', False, 250, 500]]}, {'balances': [['a0', 8], ['a1', 114], ['a2', 8], ['a3', 500]], 'unrealized_gain': 16}], ['control 1', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 2', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'asset', False, 7, 500]]}, {'balances': [['a0', 5], ['a1', 500]], 'unrealized_gain': -5}], ['control 3', {'bid': [18, 10], 'ask': [19, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'liability', False, 999, 1002], ['a2', 'liability', True, 1, 500], ['a3', 'liability', True, 1, 500], ['a4', 'asset', False, 1000, 1003]]}, {'balances': [['a0', 500], ['a1', 1002], ['a2', 2], ['a3', 2], ['a4', 1003]], 'unrealized_gain': 996}], ['control 4', {'bid': [8, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 1000, 1000], ['a1', 'asset', False, 7, 7], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 5, 8], ['a4', 'liability', False, 1000, 1000]]}, {'balances': [['a0', 2000], ['a1', 7], ['a2', 500], ['a3', 8], ['a4', 1000]], 'unrealized_gain': 1250}], ['control 5', {'bid': [5, 8], 'ask': [6, 8], 'accounts': [['a0', 'asset', True, 101, 101], ['a1', 'asset', False, 1, 4]]}, {'balances': [['a0', 63], ['a1', 4]], 'unrealized_gain': -38}], ['control 6', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'liability', False, 999, 999], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 500], ['a1', 999], ['a2', 125]], 'unrealized_gain': -1873}]], [['regression: bid and ask selection', {'bid': [15, 8], 'ask': [17, 8], 'accounts': [['a0', 'liability', True, 999, 999], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'liability', True, 101, 202]]}, {'balances': [['a0', 2123], ['a1', 215], ['a2', 13], ['a3', 215]], 'unrealized_gain': -1245}], ['control 1', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['control 2', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [188, 100], 'ask': [189, 100], 'accounts': [['a0', 'asset', False, 1000, 1000], ['a1', 'asset', False, 1000, 1000], ['a2', 'asset', False, 5, 5], ['a3', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1000], ['a1', 1000], ['a2', 5], ['a3', 1880]], 'unrealized_gain': 877}], ['control 4', {'bid': [15, 8], 'ask': [16, 8], 'accounts': [['a0', 'asset', True, 999, 500], ['a1', 'asset', False, 3, 6], ['a2', 'asset', True, 999, 1998], ['a3', 'asset', True, 3, 3], ['a4', 'asset', True, 1, 4]]}, {'balances': [['a0', 1873], ['a1', 6], ['a2', 1873], ['a3', 6], ['a4', 2]], 'unrealized_gain': 1249}], ['control 5', {'bid': [4, 8], 'ask': [5, 8], 'accounts': [['a0', 'liability', False, 1, 2], ['a1', 'liability', True, 5, 500], ['a2', 'asset', True, 1, 4], ['a3', 'asset', True, 999, 1002], ['a4', 'liability', True, 5, 5]]}, {'balances': [['a0', 2], ['a1', 3], ['a2', 1], ['a3', 500], ['a4', 3]], 'unrealized_gain': -6}], ['control 6', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'asset', True, 7, 500], ['a1', 'liability', True, 1, 1], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 11], ['a1', 2], ['a2', 375]], 'unrealized_gain': -615}]], [['regression: bid and ask selection', {'bid': [12, 10], 'ask': [14, 10], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'liability', True, 1, 2], ['a2', 'liability', True, 3, 3], ['a3', 'liability', True, 999, 1002]]}, {'balances': [['a0', 2000], ['a1', 1], ['a2', 4], ['a3', 1399]], 'unrealized_gain': -397}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['control 3', {'bid': [81, 100], 'ask': [83, 100], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', False, 1000, 500], ['a2', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1], ['a1', 500], ['a2', 810]], 'unrealized_gain': -692}], ['control 4', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 999, 999], ['a1', 'asset', False, 999, 1002], ['a2', 'asset', True, 1, 4]]}, {'balances': [['a0', 999], ['a1', 1002], ['a2', 2]], 'unrealized_gain': -2}], ['control 5', {'bid': [12, 10], 'ask': [13, 10], 'accounts': [['a0', 'liability', False, 1000, 2000], ['a1', 'asset', True, 1000, 2000], ['a2', 'liability', True, 1, 2], ['a3', 'asset', True, 3, 500]]}, {'balances': [['a0', 2000], ['a1', 1200], ['a2', 1], ['a3', 4]], 'unrealized_gain': -1295}], ['control 6', {'bid': [9, 8], 'ask': [12, 8], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', True, 101, 500], ['a2', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1], ['a1', 114], ['a2', 1125]], 'unrealized_gain': -763}]], [['regression: bid and ask selection', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['control 1', {'bid': [8, 10], 'ask': [9, 10], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500], ['a2', 'asset', False, 101, 104], ['a3', 'asset', True, 1, 1]]}, {'balances': [['a0', 1], ['a1', 200], ['a2', 104], ['a3', 1]], 'unrealized_gain': -297}], ['control 2', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [7, 10], 'ask': [9, 10], 'accounts': [['a0', 'asset', False, 250, 500], ['a1', 'asset', True, 250, 250], ['a2', 'asset', False, 7, 14]]}, {'balances': [['a0', 500], ['a1', 175], ['a2', 14]], 'unrealized_gain': -75}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'liability', False, 250, 250], ['a2', 'liability', False, 250, 500], ['a3', 'liability', False, 7, 500]]}, {'balances': [['a0', 8], ['a1', 250], ['a2', 500], ['a3', 500]], 'unrealized_gain': 3}], ['control 5', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'asset', True, 250, 250], ['a2', 'asset', True, 999, 999]]}, {'balances': [['a0', 2498], ['a1', 625], ['a2', 2498]], 'unrealized_gain': 2374}], ['control 6', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 5, 10], ['a1', 'asset', False, 1, 1]]}, {'balances': [['a0', 10], ['a1', 1]], 'unrealized_gain': 0}]], [['regression: bid and ask selection', {'bid': [130, 100], 'ask': [131, 100], 'accounts': [['a0', 'asset', False, 3, 6], ['a1', 'liability', True, 250, 253]]}, {'balances': [['a0', 6], ['a1', 328]], 'unrealized_gain': -75}], ['control 1', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 2', {'bid': [2, 4], 'ask': [3, 4], 'accounts': [['a0', 'asset', True, 5, 500], ['a1', 'asset', True, 5, 500], ['a2', 'asset', True, 5, 8], ['a3', 'asset', True, 3, 500]]}, {'balances': [['a0', 3], ['a1', 3], ['a2', 3], ['a3', 2]], 'unrealized_gain': -1497}], ['control 3', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', True, 999, 999], ['a2', 'asset', True, 101, 104]]}, {'balances': [['a0', 500], ['a1', 1499], ['a2', 152]], 'unrealized_gain': 548}], ['control 4', {'bid': [4, 2], 'ask': [7, 2], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 101, 101]]}, {'balances': [['a0', 2], ['a1', 202]], 'unrealized_gain': 99}], ['control 5', {'bid': [9, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 7, 14], ['a1', 'asset', True, 3, 6], ['a2', 'asset', True, 3, 6], ['a3', 'asset', False, 250, 253]]}, {'balances': [['a0', 16], ['a1', 7], ['a2', 7], ['a3', 253]], 'unrealized_gain': 4}], ['control 6', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

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

N = 1
observations = []
def solve(x):
    def to_home(amount, rate):
        num, den = rate
        q, r = divmod(amount * num, den)
        return q + (1 if 2 * r >= den else 0)
    gain = 0
    out = []
    for name, kind, monetary, foreign, carried in x['accounts']:
        if not monetary:
            out.append([name, carried])
            continue
        rate = x['ask'] if kind == 'asset' else x['bid']
        new = to_home(foreign, rate)
        delta = new - carried
        gain += delta if kind == 'asset' else -delta
        out.append([name, new])
    return {'balances': out, 'unrealized_gain': gain}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: bid and ask selection', {'bid': [9, 8], 'ask': [12, 8], 'accounts': [['a0', 'liability', True, 5, 10], ['a1', 'asset', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'asset', False, 250, 500]]}, {'balances': [['a0', 8], ['a1', 114], ['a2', 8], ['a3', 500]], 'unrealized_gain': 16}], ['control 1', {'bid': [124, 100], 'ask': [125, 100], 'accounts': [['a0', 'liability', True, 7, 14], ['a1', 'liability', False, 7, 7], ['a2', 'liability', True, 5, 8]]}, {'balances': [['a0', 9], ['a1', 7], ['a2', 6]], 'unrealized_gain': 7}], ['control 2', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 7, 10], ['a1', 'asset', False, 7, 500]]}, {'balances': [['a0', 5], ['a1', 500]], 'unrealized_gain': -5}], ['control 3', {'bid': [18, 10], 'ask': [19, 10], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'liability', False, 999, 1002], ['a2', 'liability', True, 1, 500], ['a3', 'liability', True, 1, 500], ['a4', 'asset', False, 1000, 1003]]}, {'balances': [['a0', 500], ['a1', 1002], ['a2', 2], ['a3', 2], ['a4', 1003]], 'unrealized_gain': 996}], ['control 4', {'bid': [8, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 1000, 1000], ['a1', 'asset', False, 7, 7], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 5, 8], ['a4', 'liability', False, 1000, 1000]]}, {'balances': [['a0', 2000], ['a1', 7], ['a2', 500], ['a3', 8], ['a4', 1000]], 'unrealized_gain': 1250}], ['control 5', {'bid': [5, 8], 'ask': [6, 8], 'accounts': [['a0', 'asset', True, 101, 101], ['a1', 'asset', False, 1, 4]]}, {'balances': [['a0', 63], ['a1', 4]], 'unrealized_gain': -38}], ['control 6', {'bid': [1, 2], 'ask': [2, 2], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'liability', False, 999, 999], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 500], ['a1', 999], ['a2', 125]], 'unrealized_gain': -1873}]], [['regression: bid and ask selection', {'bid': [15, 8], 'ask': [17, 8], 'accounts': [['a0', 'liability', True, 999, 999], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 7, 7], ['a3', 'liability', True, 101, 202]]}, {'balances': [['a0', 2123], ['a1', 215], ['a2', 13], ['a3', 215]], 'unrealized_gain': -1245}], ['control 1', {'bid': [133, 100], 'ask': [135, 100], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 1, 1], ['a2', 'liability', True, 7, 500], ['a3', 'liability', True, 3, 6]]}, {'balances': [['a0', 6], ['a1', 1], ['a2', 9], ['a3', 4]], 'unrealized_gain': 493}], ['control 2', {'bid': [119, 100], 'ask': [120, 100], 'accounts': [['a0', 'asset', False, 5, 500], ['a1', 'liability', False, 999, 500]]}, {'balances': [['a0', 500], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [188, 100], 'ask': [189, 100], 'accounts': [['a0', 'asset', False, 1000, 1000], ['a1', 'asset', False, 1000, 1000], ['a2', 'asset', False, 5, 5], ['a3', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1000], ['a1', 1000], ['a2', 5], ['a3', 1880]], 'unrealized_gain': 877}], ['control 4', {'bid': [15, 8], 'ask': [16, 8], 'accounts': [['a0', 'asset', True, 999, 500], ['a1', 'asset', False, 3, 6], ['a2', 'asset', True, 999, 1998], ['a3', 'asset', True, 3, 3], ['a4', 'asset', True, 1, 4]]}, {'balances': [['a0', 1873], ['a1', 6], ['a2', 1873], ['a3', 6], ['a4', 2]], 'unrealized_gain': 1249}], ['control 5', {'bid': [4, 8], 'ask': [5, 8], 'accounts': [['a0', 'liability', False, 1, 2], ['a1', 'liability', True, 5, 500], ['a2', 'asset', True, 1, 4], ['a3', 'asset', True, 999, 1002], ['a4', 'liability', True, 5, 5]]}, {'balances': [['a0', 2], ['a1', 3], ['a2', 1], ['a3', 500], ['a4', 3]], 'unrealized_gain': -6}], ['control 6', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'asset', True, 7, 500], ['a1', 'liability', True, 1, 1], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 11], ['a1', 2], ['a2', 375]], 'unrealized_gain': -615}]], [['regression: bid and ask selection', {'bid': [12, 10], 'ask': [14, 10], 'accounts': [['a0', 'asset', False, 1000, 2000], ['a1', 'liability', True, 1, 2], ['a2', 'liability', True, 3, 3], ['a3', 'liability', True, 999, 1002]]}, {'balances': [['a0', 2000], ['a1', 1], ['a2', 4], ['a3', 1399]], 'unrealized_gain': -397}], ['control 1', {'bid': [2, 2], 'ask': [3, 2], 'accounts': [['a0', 'asset', True, 3, 3], ['a1', 'asset', False, 5, 500], ['a2', 'asset', False, 999, 1998], ['a3', 'asset', True, 5, 5], ['a4', 'liability', False, 7, 14]]}, {'balances': [['a0', 3], ['a1', 500], ['a2', 1998], ['a3', 5], ['a4', 14]], 'unrealized_gain': 0}], ['control 2', {'bid': [130, 100], 'ask': [132, 100], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 7, 500]]}, {'balances': [['a0', 1], ['a1', 9]], 'unrealized_gain': -494}], ['control 3', {'bid': [81, 100], 'ask': [83, 100], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', False, 1000, 500], ['a2', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1], ['a1', 500], ['a2', 810]], 'unrealized_gain': -692}], ['control 4', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'liability', False, 999, 999], ['a1', 'asset', False, 999, 1002], ['a2', 'asset', True, 1, 4]]}, {'balances': [['a0', 999], ['a1', 1002], ['a2', 2]], 'unrealized_gain': -2}], ['control 5', {'bid': [12, 10], 'ask': [13, 10], 'accounts': [['a0', 'liability', False, 1000, 2000], ['a1', 'asset', True, 1000, 2000], ['a2', 'liability', True, 1, 2], ['a3', 'asset', True, 3, 500]]}, {'balances': [['a0', 2000], ['a1', 1200], ['a2', 1], ['a3', 4]], 'unrealized_gain': -1295}], ['control 6', {'bid': [9, 8], 'ask': [12, 8], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', True, 101, 500], ['a2', 'asset', True, 1000, 1003]]}, {'balances': [['a0', 1], ['a1', 114], ['a2', 1125]], 'unrealized_gain': -763}]], [['regression: bid and ask selection', {'bid': [3, 2], 'ask': [4, 2], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', False, 3, 3], ['a2', 'liability', True, 5, 10]]}, {'balances': [['a0', 1998], ['a1', 3], ['a2', 10]], 'unrealized_gain': -1498}], ['control 1', {'bid': [8, 10], 'ask': [9, 10], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500], ['a2', 'asset', False, 101, 104], ['a3', 'asset', True, 1, 1]]}, {'balances': [['a0', 1], ['a1', 200], ['a2', 104], ['a3', 1]], 'unrealized_gain': -297}], ['control 2', {'bid': [8, 8], 'ask': [9, 8], 'accounts': [['a0', 'asset', False, 101, 101], ['a1', 'asset', False, 5, 500]]}, {'balances': [['a0', 101], ['a1', 500]], 'unrealized_gain': 0}], ['control 3', {'bid': [7, 10], 'ask': [9, 10], 'accounts': [['a0', 'asset', False, 250, 500], ['a1', 'asset', True, 250, 250], ['a2', 'asset', False, 7, 14]]}, {'balances': [['a0', 500], ['a1', 175], ['a2', 14]], 'unrealized_gain': -75}], ['control 4', {'bid': [15, 10], 'ask': [18, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'liability', False, 250, 250], ['a2', 'liability', False, 250, 500], ['a3', 'liability', False, 7, 500]]}, {'balances': [['a0', 8], ['a1', 250], ['a2', 500], ['a3', 500]], 'unrealized_gain': 3}], ['control 5', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'asset', True, 999, 1998], ['a1', 'asset', True, 250, 250], ['a2', 'asset', True, 999, 999]]}, {'balances': [['a0', 2498], ['a1', 625], ['a2', 2498]], 'unrealized_gain': 2374}], ['control 6', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', True, 5, 10], ['a1', 'asset', False, 1, 1]]}, {'balances': [['a0', 10], ['a1', 1]], 'unrealized_gain': 0}]], [['regression: bid and ask selection', {'bid': [130, 100], 'ask': [131, 100], 'accounts': [['a0', 'asset', False, 3, 6], ['a1', 'liability', True, 250, 253]]}, {'balances': [['a0', 6], ['a1', 328]], 'unrealized_gain': -75}], ['control 1', {'bid': [158, 100], 'ask': [159, 100], 'accounts': [['a0', 'liability', True, 1, 1], ['a1', 'asset', False, 999, 1002], ['a2', 'liability', True, 1, 2], ['a3', 'liability', False, 1000, 1003]]}, {'balances': [['a0', 2], ['a1', 1002], ['a2', 2], ['a3', 1003]], 'unrealized_gain': -1}], ['control 2', {'bid': [2, 4], 'ask': [3, 4], 'accounts': [['a0', 'asset', True, 5, 500], ['a1', 'asset', True, 5, 500], ['a2', 'asset', True, 5, 8], ['a3', 'asset', True, 3, 500]]}, {'balances': [['a0', 3], ['a1', 3], ['a2', 3], ['a3', 2]], 'unrealized_gain': -1497}], ['control 3', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', False, 999, 500], ['a1', 'asset', True, 999, 999], ['a2', 'asset', True, 101, 104]]}, {'balances': [['a0', 500], ['a1', 1499], ['a2', 152]], 'unrealized_gain': 548}], ['control 4', {'bid': [4, 2], 'ask': [7, 2], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'asset', True, 101, 101]]}, {'balances': [['a0', 2], ['a1', 202]], 'unrealized_gain': 99}], ['control 5', {'bid': [9, 4], 'ask': [11, 4], 'accounts': [['a0', 'asset', True, 7, 14], ['a1', 'asset', True, 3, 6], ['a2', 'asset', True, 3, 6], ['a3', 'asset', False, 250, 253]]}, {'balances': [['a0', 16], ['a1', 7], ['a2', 7], ['a3', 253]], 'unrealized_gain': 4}], ['control 6', {'bid': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 7, 7], ['a1', 'liability', False, 101, 500]]}, {'balances': [['a0', 7], ['a1', 500]], 'unrealized_gain': 0}]]]
for label, args, expected in fixtures[N-1]:
    try:
        actual = solve(args)
    except Exception as exc:
        actual = 'raised ' + type(exc).__name__
    check(label, actual, expected)
print(json.dumps({"observations": observations, "passed": all(x["passed"] for x in observations)}, ensure_ascii=False))
raise SystemExit(0 if all(x["passed"] for x in observations) else 1)

prompt:
Foreign payables are understated at period end.

x = {'bid': [num, den], 'ask': [num, den], 'accounts': [[name, 'asset'|'liability', monetary, foreign units, carried home cents]]}. Monetary assets are revalued at the bid rate and monetary liabilities at the ask rate: home = foreign * num / den rounded half-up per account. Non-monetary accounts keep their carried amount. Unrealized gain sums (new - carried) for assets and (carried - new) for liabilities. Return {'balances': [[name, home]], 'unrealized_gain'}.

split: open-access


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

N = 1
observations = []
def solve(x):
    end = x['period_end']
    rev = []
    for eid, day, kind, acct, amt in x['entries']:
        if kind == 'accrual' and day <= end:
            rev.append(['R-' + eid, end, kind, acct, -amt])
    bal = {}
    for eid, day, kind, acct, amt in x['entries'] + rev:
        if day <= x['as_of']:
            bal[acct] = bal.get(acct, 0) + amt
    return {'reversals': [[r[0], r[1], r[4]] for r in rev], 'balances': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: reversal date', {'period_end': 28, 'entries': [['E0', 5, 'accrual', 'rent', 250], ['E1', 27, 'regular', 'rent', 250], ['E2', 30, 'accrual', 'cash', -100], ['E3', 30, 'deferral', 'accrued_interest', 100]], 'as_of': 29}, {'reversals': [['R-E0', 29, -250]], 'balances': {'rent': 250}}], ['control 1', {'period_end': 31, 'entries': [['E0', 33, 'accrual', 'rent', 700], ['E1', 31, 'deferral', 'rent', -40], ['E2', 32, 'deferral', 'rent', 100]], 'as_of': 32}, {'reversals': [], 'balances': {'rent': 60}}], ['control 2', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_interest', 700], ['E1', 5, 'deferral', 'accrued_interest', -100], ['E2', 5, 'regular', 'cash', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': -100, 'cash': -40}}], ['control 3', {'period_end': 30, 'entries': [['E0', 30, 'deferral', 'accrued_interest', 100], ['E1', 32, 'accrual', 'cash', -100], ['E2', 31, 'deferral', 'accrued_interest', 700], ['E3', 31, 'regular', 'rent', 250], ['E4', 30, 'deferral', 'accrued_wages', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_interest': 100, 'accrued_wages': 100}}], ['control 4', {'period_end': 28, 'entries': [['E0', 25, 'deferral', 'cash', 100], ['E1', 27, 'regular', 'cash', 250], ['E2', 30, 'regular', 'accrued_wages', 100], ['E3', 28, 'regular', 'cash', -100], ['E4', 30, 'regular', 'accrued_interest', 100], ['E5', 27, 'regular', 'accrued_interest', 100], ['E6', 5, 'deferral', 'rent', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'cash': 250, 'accrued_wages': 100, 'accrued_interest': 200, 'rent': 100}}], ['control 5', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -100], ['E1', 31, 'regular', 'accrued_wages', -40], ['E2', 30, 'deferral', 'rent', 100], ['E3', 33, 'accrual', 'accrued_interest', -100], ['E4', 5, 'deferral', 'cash', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'rent': 100, 'cash': -40}}], ['control 6', {'period_end': 30, 'entries': [['E0', 31, 'deferral', 'accrued_interest', -40], ['E1', 30, 'deferral', 'rent', -40], ['E2', 30, 'regular', 'cash', 100], ['E3', 32, 'accrual', 'cash', 250]], 'as_of': 31}, {'reversals': [], 'balances': {'accrued_interest': -40, 'rent': -40, 'cash': 100}}]], [['regression: reversal date', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'accrued_wages', 250], ['E1', 28, 'accrual', 'cash', 100], ['E2', 5, 'accrual', 'accrued_wages', -40], ['E3', 31, 'regular', 'cash', 100], ['E4', 5, 'accrual', 'cash', -40], ['E5', 31, 'accrual', 'accrued_interest', 250], ['E6', 33, 'accrual', 'accrued_interest', -100]], 'as_of': 33}, {'reversals': [['R-E1', 32, -100], ['R-E2', 32, 40], ['R-E4', 32, 40], ['R-E5', 32, -250]], 'balances': {'accrued_wages': 250, 'cash': 100, 'accrued_interest': -100}}], ['control 1', {'period_end': 30, 'entries': [['E0', 32, 'deferral', 'accrued_wages', 100], ['E1', 29, 'regular', 'accrued_wages', 100], ['E2', 32, 'deferral', 'accrued_wages', 700], ['E3', 27, 'deferral', 'rent', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 100, 'rent': -40}}], ['control 2', {'period_end': 28, 'entries': [['E0', 5, 'regular', 'accrued_interest', -40], ['E1', 30, 'accrual', 'accrued_interest', 250], ['E2', 29, 'regular', 'cash', 700], ['E3', 30, 'accrual', 'cash', 700], ['E4', 27, 'deferral', 'rent', 250], ['E5', 29, 'accrual', 'accrued_interest', -100], ['E6', 27, 'deferral', 'rent', 700]], 'as_of': 28}, {'reversals': [], 'balances': {'accrued_interest': -40, 'rent': 950}}], ['control 3', {'period_end': 28, 'entries': [['E0', 29, 'regular', 'accrued_interest', 100], ['E1', 28, 'regular', 'rent', 250], ['E2', 30, 'deferral', 'accrued_wages', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': 100, 'rent': 250}}], ['control 4', {'period_end': 30, 'entries': [['E0', 32, 'regular', 'accrued_interest', -100], ['E1', 29, 'deferral', 'cash', -40], ['E2', 31, 'accrual', 'accrued_wages', -100]], 'as_of': 31}, {'reversals': [], 'balances': {'cash': -40, 'accrued_wages': -100}}], ['control 5', {'period_end': 31, 'entries': [['E0', 31, 'regular', 'rent', 100], ['E1', 33, 'regular', 'rent', 250], ['E2', 33, 'deferral', 'rent', 100]], 'as_of': 30}, {'reversals': [], 'balances': {}}], ['control 6', {'period_end': 31, 'entries': [['E0', 30, 'deferral', 'accrued_wages', 100], ['E1', 30, 'regular', 'cash', -100], ['E2', 33, 'regular', 'accrued_wages', -40], ['E3', 32, 'accrual', 'accrued_interest', 100]], 'as_of': 31}, {'reversals': [], 'balances': {'accrued_wages': 100, 'cash': -100}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 27, 'regular', 'accrued_interest', 100], ['E1', 30, 'deferral', 'accrued_interest', 250], ['E2', 28, 'accrual', 'rent', -40], ['E3', 28, 'accrual', 'accrued_interest', 100], ['E4', 5, 'accrual', 'accrued_wages', 700]], 'as_of': 27}, {'reversals': [['R-E2', 29, 40], ['R-E3', 29, -100], ['R-E4', 29, -700]], 'balances': {'accrued_interest': 100, 'accrued_wages': 700}}], ['control 1', {'period_end': 28, 'entries': [['E0', 28, 'deferral', 'rent', 100], ['E1', 28, 'deferral', 'cash', 250], ['E2', 28, 'regular', 'cash', 250], ['E3', 27, 'deferral', 'cash', 100], ['E4', 28, 'regular', 'rent', 100], ['E5', 28, 'regular', 'cash', 700]], 'as_of': 28}, {'reversals': [], 'balances': {'rent': 200, 'cash': 1300}}], ['control 2', {'period_end': 28, 'entries': [['E0', 28, 'deferral', 'accrued_interest', 100], ['E1', 27, 'regular', 'rent', -100], ['E2', 27, 'regular', 'rent', -100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': 100, 'rent': -200}}], ['control 3', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'rent', -40], ['E1', 28, 'regular', 'accrued_interest', 100], ['E2', 5, 'regular', 'accrued_wages', -100], ['E3', 25, 'regular', 'accrued_interest', 250]], 'as_of': 27}, {'reversals': [], 'balances': {'accrued_wages': -100, 'accrued_interest': 250}}], ['control 4', {'period_end': 30, 'entries': [['E0', 32, 'accrual', 'cash', 100], ['E1', 30, 'deferral', 'accrued_interest', -40], ['E2', 30, 'regular', 'rent', 250], ['E3', 29, 'deferral', 'accrued_wages', -100], ['E4', 30, 'deferral', 'cash', 700], ['E5', 30, 'regular', 'accrued_wages', -100], ['E6', 30, 'deferral', 'cash', 100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_wages': -100}}], ['control 5', {'period_end': 30, 'entries': [['E0', 32, 'accrual', 'cash', -40], ['E1', 5, 'regular', 'accrued_wages', 100], ['E2', 5, 'deferral', 'rent', 100], ['E3', 30, 'regular', 'cash', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 660, 'accrued_wages': 100, 'rent': 100}}], ['control 6', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -40], ['E1', 33, 'deferral', 'cash', -100], ['E2', 31, 'deferral', 'accrued_interest', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': -40, 'accrued_interest': 700}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 5, 'accrual', 'rent', -40], ['E1', 27, 'accrual', 'cash', -100], ['E2', 5, 'accrual', 'cash', 100], ['E3', 27, 'regular', 'accrued_interest', 700]], 'as_of': 30}, {'reversals': [['R-E0', 29, 40], ['R-E1', 29, 100], ['R-E2', 29, -100]], 'balances': {'rent': 0, 'cash': 0, 'accrued_interest': 700}}], ['control 1', {'period_end': 30, 'entries': [['E0', 5, 'deferral', 'cash', -100], ['E1', 32, 'accrual', 'cash', 250], ['E2', 32, 'accrual', 'rent', -100], ['E3', 31, 'accrual', 'cash', -100], ['E4', 31, 'regular', 'accrued_wages', 100], ['E5', 5, 'deferral', 'cash', 700], ['E6', 30, 'deferral', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {'cash': 600, 'accrued_wages': 700}}], ['control 2', {'period_end': 30, 'entries': [['E0', 30, 'deferral', 'accrued_wages', -40], ['E1', 32, 'accrual', 'accrued_interest', -40], ['E2', 27, 'deferral', 'accrued_interest', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': -40, 'accrued_interest': 100}}], ['control 3', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'cash', -100], ['E1', 28, 'deferral', 'accrued_wages', 100], ['E2', 29, 'regular', 'rent', -40], ['E3', 25, 'deferral', 'rent', -100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_wages': 100, 'rent': -140}}], ['control 4', {'period_end': 30, 'entries': [['E0', 31, 'deferral', 'cash', 100], ['E1', 31, 'accrual', 'accrued_wages', 100], ['E2', 30, 'deferral', 'accrued_interest', -100], ['E3', 5, 'deferral', 'accrued_interest', 250]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 100, 'accrued_wages': 100, 'accrued_interest': 150}}], ['control 5', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_interest', 100], ['E1', 30, 'deferral', 'accrued_wages', 250], ['E2', 30, 'regular', 'cash', 250], ['E3', 27, 'regular', 'accrued_interest', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': -40}}], ['control 6', {'period_end': 28, 'entries': [['E0', 27, 'deferral', 'rent', 100], ['E1', 27, 'deferral', 'accrued_wages', 700], ['E2', 5, 'regular', 'rent', 700], ['E3', 30, 'deferral', 'accrued_wages', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'rent': 800, 'accrued_wages': 660}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 28, 'regular', 'cash', -40], ['E1', 27, 'accrual', 'accrued_interest', 100], ['E2', 30, 'regular', 'cash', -40]], 'as_of': 29}, {'reversals': [['R-E1', 29, -100]], 'balances': {'cash': -40, 'accrued_interest': 0}}], ['control 1', {'period_end': 31, 'entries': [['E0', 32, 'deferral', 'cash', 250], ['E1', 28, 'regular', 'rent', 250], ['E2', 33, 'deferral', 'cash', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 250, 'rent': 250}}], ['control 2', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -100], ['E1', 31, 'deferral', 'cash', -100], ['E2', 32, 'regular', 'cash', 700], ['E3', 32, 'accrual', 'rent', 100], ['E4', 31, 'deferral', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {}}], ['control 3', {'period_end': 31, 'entries': [['E0', 32, 'accrual', 'accrued_wages', 100], ['E1', 33, 'deferral', 'cash', 100], ['E2', 30, 'regular', 'cash', 700], ['E3', 33, 'regular', 'rent', 700]], 'as_of': 33}, {'reversals': [], 'balances': {'accrued_wages': 100, 'cash': 800, 'rent': 700}}], ['control 4', {'period_end': 30, 'entries': [['E0', 29, 'deferral', 'accrued_wages', 100], ['E1', 5, 'deferral', 'accrued_interest', -100], ['E2', 30, 'regular', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 800, 'accrued_interest': -100}}], ['control 5', {'period_end': 30, 'entries': [['E0', 31, 'regular', 'accrued_interest', -100], ['E1', 27, 'regular', 'rent', 250], ['E2', 31, 'deferral', 'accrued_wages', 250], ['E3', 31, 'regular', 'accrued_interest', 100], ['E4', 27, 'deferral', 'accrued_interest', 100], ['E5', 29, 'regular', 'accrued_interest', 100], ['E6', 32, 'accrual', 'cash', 250]], 'as_of': 32}, {'reversals': [], 'balances': {'accrued_interest': 200, 'rent': 250, 'accrued_wages': 250, 'cash': 250}}], ['control 6', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_wages', 700], ['E1', 30, 'accrual', 'cash', 250], ['E2', 28, 'regular', 'rent', 250]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 700, 'cash': 250, 'rent': 250}}]]]
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):
    end = x['period_end']
    rev = []
    for eid, day, kind, acct, amt in x['entries']:
        if kind == 'accrual' and day <= end:
            rev.append(['R-' + eid, day + 1, kind, acct, -amt])
    bal = {}
    for eid, day, kind, acct, amt in x['entries'] + rev:
        if day <= x['as_of']:
            bal[acct] = bal.get(acct, 0) + amt
    return {'reversals': [[r[0], r[1], r[4]] for r in rev], 'balances': bal}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: reversal date', {'period_end': 28, 'entries': [['E0', 5, 'accrual', 'rent', 250], ['E1', 27, 'regular', 'rent', 250], ['E2', 30, 'accrual', 'cash', -100], ['E3', 30, 'deferral', 'accrued_interest', 100]], 'as_of': 29}, {'reversals': [['R-E0', 29, -250]], 'balances': {'rent': 250}}], ['control 1', {'period_end': 31, 'entries': [['E0', 33, 'accrual', 'rent', 700], ['E1', 31, 'deferral', 'rent', -40], ['E2', 32, 'deferral', 'rent', 100]], 'as_of': 32}, {'reversals': [], 'balances': {'rent': 60}}], ['control 2', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_interest', 700], ['E1', 5, 'deferral', 'accrued_interest', -100], ['E2', 5, 'regular', 'cash', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': -100, 'cash': -40}}], ['control 3', {'period_end': 30, 'entries': [['E0', 30, 'deferral', 'accrued_interest', 100], ['E1', 32, 'accrual', 'cash', -100], ['E2', 31, 'deferral', 'accrued_interest', 700], ['E3', 31, 'regular', 'rent', 250], ['E4', 30, 'deferral', 'accrued_wages', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_interest': 100, 'accrued_wages': 100}}], ['control 4', {'period_end': 28, 'entries': [['E0', 25, 'deferral', 'cash', 100], ['E1', 27, 'regular', 'cash', 250], ['E2', 30, 'regular', 'accrued_wages', 100], ['E3', 28, 'regular', 'cash', -100], ['E4', 30, 'regular', 'accrued_interest', 100], ['E5', 27, 'regular', 'accrued_interest', 100], ['E6', 5, 'deferral', 'rent', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'cash': 250, 'accrued_wages': 100, 'accrued_interest': 200, 'rent': 100}}], ['control 5', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -100], ['E1', 31, 'regular', 'accrued_wages', -40], ['E2', 30, 'deferral', 'rent', 100], ['E3', 33, 'accrual', 'accrued_interest', -100], ['E4', 5, 'deferral', 'cash', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'rent': 100, 'cash': -40}}], ['control 6', {'period_end': 30, 'entries': [['E0', 31, 'deferral', 'accrued_interest', -40], ['E1', 30, 'deferral', 'rent', -40], ['E2', 30, 'regular', 'cash', 100], ['E3', 32, 'accrual', 'cash', 250]], 'as_of': 31}, {'reversals': [], 'balances': {'accrued_interest': -40, 'rent': -40, 'cash': 100}}]], [['regression: reversal date', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'accrued_wages', 250], ['E1', 28, 'accrual', 'cash', 100], ['E2', 5, 'accrual', 'accrued_wages', -40], ['E3', 31, 'regular', 'cash', 100], ['E4', 5, 'accrual', 'cash', -40], ['E5', 31, 'accrual', 'accrued_interest', 250], ['E6', 33, 'accrual', 'accrued_interest', -100]], 'as_of': 33}, {'reversals': [['R-E1', 32, -100], ['R-E2', 32, 40], ['R-E4', 32, 40], ['R-E5', 32, -250]], 'balances': {'accrued_wages': 250, 'cash': 100, 'accrued_interest': -100}}], ['control 1', {'period_end': 30, 'entries': [['E0', 32, 'deferral', 'accrued_wages', 100], ['E1', 29, 'regular', 'accrued_wages', 100], ['E2', 32, 'deferral', 'accrued_wages', 700], ['E3', 27, 'deferral', 'rent', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 100, 'rent': -40}}], ['control 2', {'period_end': 28, 'entries': [['E0', 5, 'regular', 'accrued_interest', -40], ['E1', 30, 'accrual', 'accrued_interest', 250], ['E2', 29, 'regular', 'cash', 700], ['E3', 30, 'accrual', 'cash', 700], ['E4', 27, 'deferral', 'rent', 250], ['E5', 29, 'accrual', 'accrued_interest', -100], ['E6', 27, 'deferral', 'rent', 700]], 'as_of': 28}, {'reversals': [], 'balances': {'accrued_interest': -40, 'rent': 950}}], ['control 3', {'period_end': 28, 'entries': [['E0', 29, 'regular', 'accrued_interest', 100], ['E1', 28, 'regular', 'rent', 250], ['E2', 30, 'deferral', 'accrued_wages', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': 100, 'rent': 250}}], ['control 4', {'period_end': 30, 'entries': [['E0', 32, 'regular', 'accrued_interest', -100], ['E1', 29, 'deferral', 'cash', -40], ['E2', 31, 'accrual', 'accrued_wages', -100]], 'as_of': 31}, {'reversals': [], 'balances': {'cash': -40, 'accrued_wages': -100}}], ['control 5', {'period_end': 31, 'entries': [['E0', 31, 'regular', 'rent', 100], ['E1', 33, 'regular', 'rent', 250], ['E2', 33, 'deferral', 'rent', 100]], 'as_of': 30}, {'reversals': [], 'balances': {}}], ['control 6', {'period_end': 31, 'entries': [['E0', 30, 'deferral', 'accrued_wages', 100], ['E1', 30, 'regular', 'cash', -100], ['E2', 33, 'regular', 'accrued_wages', -40], ['E3', 32, 'accrual', 'accrued_interest', 100]], 'as_of': 31}, {'reversals': [], 'balances': {'accrued_wages': 100, 'cash': -100}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 27, 'regular', 'accrued_interest', 100], ['E1', 30, 'deferral', 'accrued_interest', 250], ['E2', 28, 'accrual', 'rent', -40], ['E3', 28, 'accrual', 'accrued_interest', 100], ['E4', 5, 'accrual', 'accrued_wages', 700]], 'as_of': 27}, {'reversals': [['R-E2', 29, 40], ['R-E3', 29, -100], ['R-E4', 29, -700]], 'balances': {'accrued_interest': 100, 'accrued_wages': 700}}], ['control 1', {'period_end': 28, 'entries': [['E0', 28, 'deferral', 'rent', 100], ['E1', 28, 'deferral', 'cash', 250], ['E2', 28, 'regular', 'cash', 250], ['E3', 27, 'deferral', 'cash', 100], ['E4', 28, 'regular', 'rent', 100], ['E5', 28, 'regular', 'cash', 700]], 'as_of': 28}, {'reversals': [], 'balances': {'rent': 200, 'cash': 1300}}], ['control 2', {'period_end': 28, 'entries': [['E0', 28, 'deferral', 'accrued_interest', 100], ['E1', 27, 'regular', 'rent', -100], ['E2', 27, 'regular', 'rent', -100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': 100, 'rent': -200}}], ['control 3', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'rent', -40], ['E1', 28, 'regular', 'accrued_interest', 100], ['E2', 5, 'regular', 'accrued_wages', -100], ['E3', 25, 'regular', 'accrued_interest', 250]], 'as_of': 27}, {'reversals': [], 'balances': {'accrued_wages': -100, 'accrued_interest': 250}}], ['control 4', {'period_end': 30, 'entries': [['E0', 32, 'accrual', 'cash', 100], ['E1', 30, 'deferral', 'accrued_interest', -40], ['E2', 30, 'regular', 'rent', 250], ['E3', 29, 'deferral', 'accrued_wages', -100], ['E4', 30, 'deferral', 'cash', 700], ['E5', 30, 'regular', 'accrued_wages', -100], ['E6', 30, 'deferral', 'cash', 100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_wages': -100}}], ['control 5', {'period_end': 30, 'entries': [['E0', 32, 'accrual', 'cash', -40], ['E1', 5, 'regular', 'accrued_wages', 100], ['E2', 5, 'deferral', 'rent', 100], ['E3', 30, 'regular', 'cash', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 660, 'accrued_wages': 100, 'rent': 100}}], ['control 6', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -40], ['E1', 33, 'deferral', 'cash', -100], ['E2', 31, 'deferral', 'accrued_interest', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': -40, 'accrued_interest': 700}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 5, 'accrual', 'rent', -40], ['E1', 27, 'accrual', 'cash', -100], ['E2', 5, 'accrual', 'cash', 100], ['E3', 27, 'regular', 'accrued_interest', 700]], 'as_of': 30}, {'reversals': [['R-E0', 29, 40], ['R-E1', 29, 100], ['R-E2', 29, -100]], 'balances': {'rent': 0, 'cash': 0, 'accrued_interest': 700}}], ['control 1', {'period_end': 30, 'entries': [['E0', 5, 'deferral', 'cash', -100], ['E1', 32, 'accrual', 'cash', 250], ['E2', 32, 'accrual', 'rent', -100], ['E3', 31, 'accrual', 'cash', -100], ['E4', 31, 'regular', 'accrued_wages', 100], ['E5', 5, 'deferral', 'cash', 700], ['E6', 30, 'deferral', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {'cash': 600, 'accrued_wages': 700}}], ['control 2', {'period_end': 30, 'entries': [['E0', 30, 'deferral', 'accrued_wages', -40], ['E1', 32, 'accrual', 'accrued_interest', -40], ['E2', 27, 'deferral', 'accrued_interest', 100]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': -40, 'accrued_interest': 100}}], ['control 3', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'cash', -100], ['E1', 28, 'deferral', 'accrued_wages', 100], ['E2', 29, 'regular', 'rent', -40], ['E3', 25, 'deferral', 'rent', -100]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_wages': 100, 'rent': -140}}], ['control 4', {'period_end': 30, 'entries': [['E0', 31, 'deferral', 'cash', 100], ['E1', 31, 'accrual', 'accrued_wages', 100], ['E2', 30, 'deferral', 'accrued_interest', -100], ['E3', 5, 'deferral', 'accrued_interest', 250]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 100, 'accrued_wages': 100, 'accrued_interest': 150}}], ['control 5', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_interest', 100], ['E1', 30, 'deferral', 'accrued_wages', 250], ['E2', 30, 'regular', 'cash', 250], ['E3', 27, 'regular', 'accrued_interest', -40]], 'as_of': 29}, {'reversals': [], 'balances': {'accrued_interest': -40}}], ['control 6', {'period_end': 28, 'entries': [['E0', 27, 'deferral', 'rent', 100], ['E1', 27, 'deferral', 'accrued_wages', 700], ['E2', 5, 'regular', 'rent', 700], ['E3', 30, 'deferral', 'accrued_wages', -40]], 'as_of': 30}, {'reversals': [], 'balances': {'rent': 800, 'accrued_wages': 660}}]], [['regression: reversal date', {'period_end': 28, 'entries': [['E0', 28, 'regular', 'cash', -40], ['E1', 27, 'accrual', 'accrued_interest', 100], ['E2', 30, 'regular', 'cash', -40]], 'as_of': 29}, {'reversals': [['R-E1', 29, -100]], 'balances': {'cash': -40, 'accrued_interest': 0}}], ['control 1', {'period_end': 31, 'entries': [['E0', 32, 'deferral', 'cash', 250], ['E1', 28, 'regular', 'rent', 250], ['E2', 33, 'deferral', 'cash', 700]], 'as_of': 32}, {'reversals': [], 'balances': {'cash': 250, 'rent': 250}}], ['control 2', {'period_end': 31, 'entries': [['E0', 31, 'deferral', 'cash', -100], ['E1', 31, 'deferral', 'cash', -100], ['E2', 32, 'regular', 'cash', 700], ['E3', 32, 'accrual', 'rent', 100], ['E4', 31, 'deferral', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {}}], ['control 3', {'period_end': 31, 'entries': [['E0', 32, 'accrual', 'accrued_wages', 100], ['E1', 33, 'deferral', 'cash', 100], ['E2', 30, 'regular', 'cash', 700], ['E3', 33, 'regular', 'rent', 700]], 'as_of': 33}, {'reversals': [], 'balances': {'accrued_wages': 100, 'cash': 800, 'rent': 700}}], ['control 4', {'period_end': 30, 'entries': [['E0', 29, 'deferral', 'accrued_wages', 100], ['E1', 5, 'deferral', 'accrued_interest', -100], ['E2', 30, 'regular', 'accrued_wages', 700]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 800, 'accrued_interest': -100}}], ['control 5', {'period_end': 30, 'entries': [['E0', 31, 'regular', 'accrued_interest', -100], ['E1', 27, 'regular', 'rent', 250], ['E2', 31, 'deferral', 'accrued_wages', 250], ['E3', 31, 'regular', 'accrued_interest', 100], ['E4', 27, 'deferral', 'accrued_interest', 100], ['E5', 29, 'regular', 'accrued_interest', 100], ['E6', 32, 'accrual', 'cash', 250]], 'as_of': 32}, {'reversals': [], 'balances': {'accrued_interest': 200, 'rent': 250, 'accrued_wages': 250, 'cash': 250}}], ['control 6', {'period_end': 28, 'entries': [['E0', 30, 'accrual', 'accrued_wages', 700], ['E1', 30, 'accrual', 'cash', 250], ['E2', 28, 'regular', 'rent', 250]], 'as_of': 30}, {'reversals': [], 'balances': {'accrued_wages': 700, 'cash': 250, 'rent': 250}}]]]
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:
Accruals cancel themselves inside the period they were meant to accrue.

x = {'period_end': day, 'as_of': day, 'entries': [[id, day, kind, account, signed amount]]}. Every entry of kind 'accrual' dated on or before period_end is reversed by a generated entry 'R-'+id dated period_end + 1 with the negated amount; regular and deferral entries and later accruals are not reversed. Balances include every original and generated row dated on or before as_of; an account with rows in range appears even if its balance is zero. Return {'reversals': [[id, day, amount]], 'balances': {account: amount}}.

split: open-access