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

N = 1
observations = []
def solve(x):
    bank = x['bank']
    book = x['book']
    entries = []
    for item in x['items']:
        kind = item[0]
        if kind == 'deposit_in_transit':
            bank += item[1]
        elif kind == 'outstanding_check':
            bank -= item[1]
        elif kind == 'bank_error':
            bank += item[1]
        else:
            if kind == 'bank_fee' or kind == 'nsf':
                adj = item[1]
            elif kind == 'interest':
                adj = item[1]
            elif kind == 'collection':
                adj = item[1] - item[2]
            elif kind == 'check_error':
                adj = item[1] - item[2]
            else:
                continue
            book += adj
            entries.append([kind, adj])
    return {'adjusted_bank': bank, 'adjusted_book': book, 'reconciled': bank == book, 'entries': entries}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: service charge direction', {'bank': 10000, 'book': 10000, 'items': [['bank_fee', 90], ['interest', 15], ['bank_error', 1200], ['nsf', 1200], ['memo', 250], ['memo', 1200]]}, {'adjusted_bank': 11200, 'adjusted_book': 8725, 'reconciled': False, 'entries': [['bank_fee', -90], ['interest', 15], ['nsf', -1200]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['interest', 1200], ['bank_error', 400], ['interest', 400], ['bank_error', 250], ['interest', 400]]}, {'adjusted_bank': 8650, 'adjusted_book': 11000, 'reconciled': False, 'entries': [['interest', 1200], ['interest', 400], ['interest', 400]]}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['deposit_in_transit', 250], ['collection', 12000, 0], ['check_error', 1200, 1020]]}, {'adjusted_bank': 12750, 'adjusted_book': 21180, 'reconciled': False, 'entries': [['collection', 12000], ['check_error', 180]]}], ['control 3', {'bank': 10000, 'book': 9000, 'items': [['outstanding_check', 15], ['interest', 1200], ['interest', 1200], ['collection', 12000, 0], ['collection', 4000, 15], ['outstanding_check', 90]]}, {'adjusted_bank': 9895, 'adjusted_book': 27385, 'reconciled': False, 'entries': [['interest', 1200], ['interest', 1200], ['collection', 12000], ['collection', 3985]]}], ['control 4', {'bank': 8000, 'book': 9000, 'items': [['memo', 15], ['memo', 400], ['bank_error', 400], ['check_error', 540, 2010], ['memo', 400]]}, {'adjusted_bank': 8400, 'adjusted_book': 7530, 'reconciled': False, 'entries': [['check_error', -1470]]}], ['control 5', {'bank': 10000, 'book': 10000, 'items': [['interest', 250], ['outstanding_check', 250], ['memo', 400], ['memo', 90], ['deposit_in_transit', 250], ['outstanding_check', 15]]}, {'adjusted_bank': 9985, 'adjusted_book': 10250, 'reconciled': False, 'entries': [['interest', 250]]}], ['control 6', {'bank': 12500, 'book': 9000, 'items': [['memo', 15], ['deposit_in_transit', 1200], ['memo', 400]]}, {'adjusted_bank': 13700, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}]], [['regression: service charge direction', {'bank': 12500, 'book': 9000, 'items': [['nsf', 400], ['bank_error', -90], ['bank_fee', 90]]}, {'adjusted_bank': 12410, 'adjusted_book': 8510, 'reconciled': False, 'entries': [['nsf', -400], ['bank_fee', -90]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 400], ['outstanding_check', 400], ['memo', 15], ['outstanding_check', 250], ['memo', 1200], ['deposit_in_transit', 250]]}, {'adjusted_bank': 8000, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['bank_error', 90], ['deposit_in_transit', 400], ['bank_error', -400], ['outstanding_check', 400]]}, {'adjusted_bank': 12190, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 3', {'bank': 10000, 'book': 11800, 'items': [['bank_error', 1200], ['collection', 4000, 25], ['memo', 90], ['collection', 12000, 15], ['collection', 150, 25], ['collection', 150, 25], ['collection', 12000, 0]]}, {'adjusted_bank': 11200, 'adjusted_book': 40010, 'reconciled': False, 'entries': [['collection', 3975], ['collection', 11985], ['collection', 125], ['collection', 125], ['collection', 12000]]}], ['control 4', {'bank': 8000, 'book': 10000, 'items': [['bank_error', -400], ['outstanding_check', 1200], ['bank_error', 15], ['deposit_in_transit', 250], ['check_error', 450, 459], ['outstanding_check', 15]]}, {'adjusted_bank': 6650, 'adjusted_book': 9991, 'reconciled': False, 'entries': [['check_error', -9]]}], ['control 5', {'bank': 10000, 'book': 9000, 'items': [['deposit_in_transit', 90], ['outstanding_check', 1200], ['collection', 900, 0]]}, {'adjusted_bank': 8890, 'adjusted_book': 9900, 'reconciled': False, 'entries': [['collection', 900]]}], ['control 6', {'bank': 8000, 'book': 9000, 'items': [['check_error', 540, 450], ['outstanding_check', 90], ['interest', 250], ['interest', 400]]}, {'adjusted_bank': 7910, 'adjusted_book': 9740, 'reconciled': False, 'entries': [['check_error', 90], ['interest', 250], ['interest', 400]]}]], [['regression: service charge direction', {'bank': 10000, 'book': 11800, 'items': [['collection', 4000, 15], ['outstanding_check', 1200], ['bank_error', 400], ['bank_fee', 1200], ['bank_error', 400]]}, {'adjusted_bank': 9600, 'adjusted_book': 14585, 'reconciled': False, 'entries': [['collection', 3985], ['bank_fee', -1200]]}], ['control 1', {'bank': 10000, 'book': 11800, 'items': [['deposit_in_transit', 1200], ['check_error', 450, 459], ['check_error', 540, 450], ['bank_error', 15]]}, {'adjusted_bank': 11215, 'adjusted_book': 11881, 'reconciled': False, 'entries': [['check_error', -9], ['check_error', 90]]}], ['control 2', {'bank': 8000, 'book': 11800, 'items': [['check_error', 450, 450], ['outstanding_check', 1200], ['bank_error', -400], ['check_error', 2100, 2010]]}, {'adjusted_bank': 6400, 'adjusted_book': 11890, 'reconciled': False, 'entries': [['check_error', 0], ['check_error', 90]]}], ['control 3', {'bank': 12500, 'book': 11800, 'items': [['bank_error', -90], ['deposit_in_transit', 400], ['collection', 12000, 15], ['deposit_in_transit', 90], ['bank_error', 250], ['check_error', 540, 540]]}, {'adjusted_bank': 13150, 'adjusted_book': 23785, 'reconciled': False, 'entries': [['collection', 11985], ['check_error', 0]]}], ['control 4', {'bank': 12500, 'book': 10000, 'items': [['deposit_in_transit', 250], ['interest', 15], ['bank_error', 90], ['interest', 90], ['collection', 150, 0], ['outstanding_check', 400]]}, {'adjusted_bank': 12440, 'adjusted_book': 10255, 'reconciled': False, 'entries': [['interest', 15], ['interest', 90], ['collection', 150]]}], ['control 5', {'bank': 10000, 'book': 11800, 'items': [['bank_error', 90], ['outstanding_check', 1200], ['collection', 900, 0], ['deposit_in_transit', 1200], ['deposit_in_transit', 15], ['interest', 90], ['check_error', 450, 540]]}, {'adjusted_bank': 10105, 'adjusted_book': 12700, 'reconciled': False, 'entries': [['collection', 900], ['interest', 90], ['check_error', -90]]}], ['control 6', {'bank': 12500, 'book': 10000, 'items': [['bank_error', -15], ['memo', 250], ['collection', 12000, 25], ['bank_error', 15], ['outstanding_check', 1200]]}, {'adjusted_bank': 11300, 'adjusted_book': 21975, 'reconciled': False, 'entries': [['collection', 11975]]}]], [['regression: service charge direction', {'bank': 12500, 'book': 10000, 'items': [['deposit_in_transit', 15], ['bank_error', -15], ['nsf', 90], ['collection', 2500, 25], ['outstanding_check', 90], ['collection', 900, 0]]}, {'adjusted_bank': 12410, 'adjusted_book': 13285, 'reconciled': False, 'entries': [['nsf', -90], ['collection', 2475], ['collection', 900]]}], ['regression: service charge direction, partial-repair probe', {'bank': 12500, 'book': 9000, 'items': [['bank_error', -1200], ['memo', 15], ['bank_fee', 15], ['outstanding_check', 400], ['outstanding_check', 400]]}, {'adjusted_bank': 10500, 'adjusted_book': 8985, 'reconciled': False, 'entries': [['bank_fee', -15]]}], ['control 1', {'bank': 12500, 'book': 11800, 'items': [['collection', 4000, 0], ['bank_error', 400], ['check_error', 450, 459]]}, {'adjusted_bank': 12900, 'adjusted_book': 15791, 'reconciled': False, 'entries': [['collection', 4000], ['check_error', -9]]}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['deposit_in_transit', 250], ['bank_error', -90], ['bank_error', 90]]}, {'adjusted_bank': 12750, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 3', {'bank': 8000, 'book': 10000, 'items': [['deposit_in_transit', 400], ['deposit_in_transit', 1200], ['memo', 90], ['interest', 250], ['check_error', 540, 540], ['check_error', 2100, 2109], ['interest', 250]]}, {'adjusted_bank': 9600, 'adjusted_book': 10491, 'reconciled': False, 'entries': [['interest', 250], ['check_error', 0], ['check_error', -9], ['interest', 250]]}], ['control 4', {'bank': 8000, 'book': 10000, 'items': [['deposit_in_transit', 90], ['bank_error', 15], ['collection', 4000, 0], ['interest', 250]]}, {'adjusted_bank': 8105, 'adjusted_book': 14250, 'reconciled': False, 'entries': [['collection', 4000], ['interest', 250]]}], ['control 5', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 250], ['bank_error', -90], ['deposit_in_transit', 400], ['collection', 12000, 0]]}, {'adjusted_bank': 8560, 'adjusted_book': 21000, 'reconciled': False, 'entries': [['collection', 12000]]}]], [['regression: service charge direction', {'bank': 12500, 'book': 10000, 'items': [['outstanding_check', 250], ['collection', 4000, 25], ['check_error', 2100, 450], ['outstanding_check', 250], ['bank_fee', 90], ['interest', 1200], ['outstanding_check', 15]]}, {'adjusted_bank': 11985, 'adjusted_book': 16735, 'reconciled': False, 'entries': [['collection', 3975], ['check_error', 1650], ['bank_fee', -90], ['interest', 1200]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['check_error', 450, 459], ['outstanding_check', 400], ['interest', 1200]]}, {'adjusted_bank': 7600, 'adjusted_book': 10191, 'reconciled': False, 'entries': [['check_error', -9], ['interest', 1200]]}], ['control 2', {'bank': 12500, 'book': 11800, 'items': [['check_error', 1200, 540], ['bank_error', -250], ['check_error', 2100, 450], ['bank_error', -15]]}, {'adjusted_bank': 12235, 'adjusted_book': 14110, 'reconciled': False, 'entries': [['check_error', 660], ['check_error', 1650]]}], ['control 3', {'bank': 12500, 'book': 10000, 'items': [['bank_error', -15], ['bank_error', 250], ['memo', 400], ['bank_error', 15], ['memo', 1200]]}, {'adjusted_bank': 12750, 'adjusted_book': 10000, 'reconciled': False, 'entries': []}], ['control 4', {'bank': 12500, 'book': 11800, 'items': [['check_error', 540, 540], ['bank_error', -90], ['outstanding_check', 1200]]}, {'adjusted_bank': 11210, 'adjusted_book': 11800, 'reconciled': False, 'entries': [['check_error', 0]]}], ['control 5', {'bank': 10000, 'book': 9000, 'items': [['check_error', 450, 2010], ['outstanding_check', 90], ['memo', 1200]]}, {'adjusted_bank': 9910, 'adjusted_book': 7440, 'reconciled': False, 'entries': [['check_error', -1560]]}], ['control 6', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 1200], ['collection', 150, 15], ['bank_error', 90], ['interest', 1200]]}, {'adjusted_bank': 9290, 'adjusted_book': 10335, 'reconciled': False, 'entries': [['collection', 135], ['interest', 1200]]}]]]
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):
    bank = x['bank']
    book = x['book']
    entries = []
    for item in x['items']:
        kind = item[0]
        if kind == 'deposit_in_transit':
            bank += item[1]
        elif kind == 'outstanding_check':
            bank -= item[1]
        elif kind == 'bank_error':
            bank += item[1]
        else:
            if kind == 'bank_fee' or kind == 'nsf':
                adj = -item[1] if kind == 'nsf' else item[1]
            elif kind == 'interest':
                adj = item[1]
            elif kind == 'collection':
                adj = item[1] - item[2]
            elif kind == 'check_error':
                adj = item[1] - item[2]
            else:
                continue
            book += adj
            entries.append([kind, adj])
    return {'adjusted_bank': bank, 'adjusted_book': book, 'reconciled': bank == book, 'entries': entries}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: service charge direction', {'bank': 10000, 'book': 10000, 'items': [['bank_fee', 90], ['interest', 15], ['bank_error', 1200], ['nsf', 1200], ['memo', 250], ['memo', 1200]]}, {'adjusted_bank': 11200, 'adjusted_book': 8725, 'reconciled': False, 'entries': [['bank_fee', -90], ['interest', 15], ['nsf', -1200]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['interest', 1200], ['bank_error', 400], ['interest', 400], ['bank_error', 250], ['interest', 400]]}, {'adjusted_bank': 8650, 'adjusted_book': 11000, 'reconciled': False, 'entries': [['interest', 1200], ['interest', 400], ['interest', 400]]}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['deposit_in_transit', 250], ['collection', 12000, 0], ['check_error', 1200, 1020]]}, {'adjusted_bank': 12750, 'adjusted_book': 21180, 'reconciled': False, 'entries': [['collection', 12000], ['check_error', 180]]}], ['control 3', {'bank': 10000, 'book': 9000, 'items': [['outstanding_check', 15], ['interest', 1200], ['interest', 1200], ['collection', 12000, 0], ['collection', 4000, 15], ['outstanding_check', 90]]}, {'adjusted_bank': 9895, 'adjusted_book': 27385, 'reconciled': False, 'entries': [['interest', 1200], ['interest', 1200], ['collection', 12000], ['collection', 3985]]}], ['control 4', {'bank': 8000, 'book': 9000, 'items': [['memo', 15], ['memo', 400], ['bank_error', 400], ['check_error', 540, 2010], ['memo', 400]]}, {'adjusted_bank': 8400, 'adjusted_book': 7530, 'reconciled': False, 'entries': [['check_error', -1470]]}], ['control 5', {'bank': 10000, 'book': 10000, 'items': [['interest', 250], ['outstanding_check', 250], ['memo', 400], ['memo', 90], ['deposit_in_transit', 250], ['outstanding_check', 15]]}, {'adjusted_bank': 9985, 'adjusted_book': 10250, 'reconciled': False, 'entries': [['interest', 250]]}], ['control 6', {'bank': 12500, 'book': 9000, 'items': [['memo', 15], ['deposit_in_transit', 1200], ['memo', 400]]}, {'adjusted_bank': 13700, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}]], [['regression: service charge direction', {'bank': 12500, 'book': 9000, 'items': [['nsf', 400], ['bank_error', -90], ['bank_fee', 90]]}, {'adjusted_bank': 12410, 'adjusted_book': 8510, 'reconciled': False, 'entries': [['nsf', -400], ['bank_fee', -90]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 400], ['outstanding_check', 400], ['memo', 15], ['outstanding_check', 250], ['memo', 1200], ['deposit_in_transit', 250]]}, {'adjusted_bank': 8000, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['bank_error', 90], ['deposit_in_transit', 400], ['bank_error', -400], ['outstanding_check', 400]]}, {'adjusted_bank': 12190, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 3', {'bank': 10000, 'book': 11800, 'items': [['bank_error', 1200], ['collection', 4000, 25], ['memo', 90], ['collection', 12000, 15], ['collection', 150, 25], ['collection', 150, 25], ['collection', 12000, 0]]}, {'adjusted_bank': 11200, 'adjusted_book': 40010, 'reconciled': False, 'entries': [['collection', 3975], ['collection', 11985], ['collection', 125], ['collection', 125], ['collection', 12000]]}], ['control 4', {'bank': 8000, 'book': 10000, 'items': [['bank_error', -400], ['outstanding_check', 1200], ['bank_error', 15], ['deposit_in_transit', 250], ['check_error', 450, 459], ['outstanding_check', 15]]}, {'adjusted_bank': 6650, 'adjusted_book': 9991, 'reconciled': False, 'entries': [['check_error', -9]]}], ['control 5', {'bank': 10000, 'book': 9000, 'items': [['deposit_in_transit', 90], ['outstanding_check', 1200], ['collection', 900, 0]]}, {'adjusted_bank': 8890, 'adjusted_book': 9900, 'reconciled': False, 'entries': [['collection', 900]]}], ['control 6', {'bank': 8000, 'book': 9000, 'items': [['check_error', 540, 450], ['outstanding_check', 90], ['interest', 250], ['interest', 400]]}, {'adjusted_bank': 7910, 'adjusted_book': 9740, 'reconciled': False, 'entries': [['check_error', 90], ['interest', 250], ['interest', 400]]}]], [['regression: service charge direction', {'bank': 10000, 'book': 11800, 'items': [['collection', 4000, 15], ['outstanding_check', 1200], ['bank_error', 400], ['bank_fee', 1200], ['bank_error', 400]]}, {'adjusted_bank': 9600, 'adjusted_book': 14585, 'reconciled': False, 'entries': [['collection', 3985], ['bank_fee', -1200]]}], ['control 1', {'bank': 10000, 'book': 11800, 'items': [['deposit_in_transit', 1200], ['check_error', 450, 459], ['check_error', 540, 450], ['bank_error', 15]]}, {'adjusted_bank': 11215, 'adjusted_book': 11881, 'reconciled': False, 'entries': [['check_error', -9], ['check_error', 90]]}], ['control 2', {'bank': 8000, 'book': 11800, 'items': [['check_error', 450, 450], ['outstanding_check', 1200], ['bank_error', -400], ['check_error', 2100, 2010]]}, {'adjusted_bank': 6400, 'adjusted_book': 11890, 'reconciled': False, 'entries': [['check_error', 0], ['check_error', 90]]}], ['control 3', {'bank': 12500, 'book': 11800, 'items': [['bank_error', -90], ['deposit_in_transit', 400], ['collection', 12000, 15], ['deposit_in_transit', 90], ['bank_error', 250], ['check_error', 540, 540]]}, {'adjusted_bank': 13150, 'adjusted_book': 23785, 'reconciled': False, 'entries': [['collection', 11985], ['check_error', 0]]}], ['control 4', {'bank': 12500, 'book': 10000, 'items': [['deposit_in_transit', 250], ['interest', 15], ['bank_error', 90], ['interest', 90], ['collection', 150, 0], ['outstanding_check', 400]]}, {'adjusted_bank': 12440, 'adjusted_book': 10255, 'reconciled': False, 'entries': [['interest', 15], ['interest', 90], ['collection', 150]]}], ['control 5', {'bank': 10000, 'book': 11800, 'items': [['bank_error', 90], ['outstanding_check', 1200], ['collection', 900, 0], ['deposit_in_transit', 1200], ['deposit_in_transit', 15], ['interest', 90], ['check_error', 450, 540]]}, {'adjusted_bank': 10105, 'adjusted_book': 12700, 'reconciled': False, 'entries': [['collection', 900], ['interest', 90], ['check_error', -90]]}], ['control 6', {'bank': 12500, 'book': 10000, 'items': [['bank_error', -15], ['memo', 250], ['collection', 12000, 25], ['bank_error', 15], ['outstanding_check', 1200]]}, {'adjusted_bank': 11300, 'adjusted_book': 21975, 'reconciled': False, 'entries': [['collection', 11975]]}]], [['regression: service charge direction', {'bank': 12500, 'book': 10000, 'items': [['deposit_in_transit', 15], ['bank_error', -15], ['nsf', 90], ['collection', 2500, 25], ['outstanding_check', 90], ['collection', 900, 0]]}, {'adjusted_bank': 12410, 'adjusted_book': 13285, 'reconciled': False, 'entries': [['nsf', -90], ['collection', 2475], ['collection', 900]]}], ['regression: service charge direction, partial-repair probe', {'bank': 12500, 'book': 9000, 'items': [['bank_error', -1200], ['memo', 15], ['bank_fee', 15], ['outstanding_check', 400], ['outstanding_check', 400]]}, {'adjusted_bank': 10500, 'adjusted_book': 8985, 'reconciled': False, 'entries': [['bank_fee', -15]]}], ['control 1', {'bank': 12500, 'book': 11800, 'items': [['collection', 4000, 0], ['bank_error', 400], ['check_error', 450, 459]]}, {'adjusted_bank': 12900, 'adjusted_book': 15791, 'reconciled': False, 'entries': [['collection', 4000], ['check_error', -9]]}], ['control 2', {'bank': 12500, 'book': 9000, 'items': [['deposit_in_transit', 250], ['bank_error', -90], ['bank_error', 90]]}, {'adjusted_bank': 12750, 'adjusted_book': 9000, 'reconciled': False, 'entries': []}], ['control 3', {'bank': 8000, 'book': 10000, 'items': [['deposit_in_transit', 400], ['deposit_in_transit', 1200], ['memo', 90], ['interest', 250], ['check_error', 540, 540], ['check_error', 2100, 2109], ['interest', 250]]}, {'adjusted_bank': 9600, 'adjusted_book': 10491, 'reconciled': False, 'entries': [['interest', 250], ['check_error', 0], ['check_error', -9], ['interest', 250]]}], ['control 4', {'bank': 8000, 'book': 10000, 'items': [['deposit_in_transit', 90], ['bank_error', 15], ['collection', 4000, 0], ['interest', 250]]}, {'adjusted_bank': 8105, 'adjusted_book': 14250, 'reconciled': False, 'entries': [['collection', 4000], ['interest', 250]]}], ['control 5', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 250], ['bank_error', -90], ['deposit_in_transit', 400], ['collection', 12000, 0]]}, {'adjusted_bank': 8560, 'adjusted_book': 21000, 'reconciled': False, 'entries': [['collection', 12000]]}]], [['regression: service charge direction', {'bank': 12500, 'book': 10000, 'items': [['outstanding_check', 250], ['collection', 4000, 25], ['check_error', 2100, 450], ['outstanding_check', 250], ['bank_fee', 90], ['interest', 1200], ['outstanding_check', 15]]}, {'adjusted_bank': 11985, 'adjusted_book': 16735, 'reconciled': False, 'entries': [['collection', 3975], ['check_error', 1650], ['bank_fee', -90], ['interest', 1200]]}], ['control 1', {'bank': 8000, 'book': 9000, 'items': [['check_error', 450, 459], ['outstanding_check', 400], ['interest', 1200]]}, {'adjusted_bank': 7600, 'adjusted_book': 10191, 'reconciled': False, 'entries': [['check_error', -9], ['interest', 1200]]}], ['control 2', {'bank': 12500, 'book': 11800, 'items': [['check_error', 1200, 540], ['bank_error', -250], ['check_error', 2100, 450], ['bank_error', -15]]}, {'adjusted_bank': 12235, 'adjusted_book': 14110, 'reconciled': False, 'entries': [['check_error', 660], ['check_error', 1650]]}], ['control 3', {'bank': 12500, 'book': 10000, 'items': [['bank_error', -15], ['bank_error', 250], ['memo', 400], ['bank_error', 15], ['memo', 1200]]}, {'adjusted_bank': 12750, 'adjusted_book': 10000, 'reconciled': False, 'entries': []}], ['control 4', {'bank': 12500, 'book': 11800, 'items': [['check_error', 540, 540], ['bank_error', -90], ['outstanding_check', 1200]]}, {'adjusted_bank': 11210, 'adjusted_book': 11800, 'reconciled': False, 'entries': [['check_error', 0]]}], ['control 5', {'bank': 10000, 'book': 9000, 'items': [['check_error', 450, 2010], ['outstanding_check', 90], ['memo', 1200]]}, {'adjusted_bank': 9910, 'adjusted_book': 7440, 'reconciled': False, 'entries': [['check_error', -1560]]}], ['control 6', {'bank': 8000, 'book': 9000, 'items': [['deposit_in_transit', 1200], ['collection', 150, 15], ['bank_error', 90], ['interest', 1200]]}, {'adjusted_bank': 9290, 'adjusted_book': 10335, 'reconciled': False, 'entries': [['collection', 135], ['interest', 1200]]}]]]
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:
Monthly bank service charges raise the adjusted book balance.

x = {'bank': statement balance, 'book': ledger cash balance, 'items': [[kind, ...]]}. Bank side: deposit_in_transit adds, outstanding_check subtracts, bank_error adds its signed correction. Book side (each produces an adjusting entry [kind, signed amount] in input order): bank_fee and nsf subtract, interest adds, collection [kind, face, fee] adds face - fee, check_error [kind, recorded, actual] adds recorded - actual. Other kinds are ignored. Return {'adjusted_bank', 'adjusted_book', 'reconciled', 'entries'}.

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 - carried
        gain += 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: liability gain sign', {'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': [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 2', {'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 3', {'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 4', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 4], ['a1', 625]], 'unrealized_gain': 125}], ['control 5', {'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}], ['control 6', {'bid': [20, 10], 'ask': [21, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'asset', True, 250, 253], ['a2', 'asset', True, 5, 8], ['a3', 'asset', True, 7, 14]]}, {'balances': [['a0', 10], ['a1', 500], ['a2', 10], ['a3', 14]], 'unrealized_gain': 254}]], [['regression: liability gain sign', {'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': [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': [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 3', {'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 4', {'bid': [16, 8], 'ask': [18, 8], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 999, 1002]]}, {'balances': [['a0', 6], ['a1', 1998]], 'unrealized_gain': 996}], ['control 5', {'bid': [10, 8], 'ask': [11, 8], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', False, 999, 999], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 999, 1002]]}, {'balances': [['a0', 1], ['a1', 999], ['a2', 313], ['a3', 1002]], 'unrealized_gain': -436}], ['control 6', {'bid': [8, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 3, 3], ['a1', 'asset', True, 3, 6], ['a2', 'asset', False, 1000, 1000], ['a3', 'liability', False, 5, 500], ['a4', 'asset', True, 101, 500]]}, {'balances': [['a0', 3], ['a1', 6], ['a2', 1000], ['a3', 500], ['a4', 202]], 'unrealized_gain': -298}]], [['regression: liability gain sign', {'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': [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}], ['control 6', {'bid': [18, 10], 'ask': [20, 10], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'asset', True, 999, 1998], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 2], ['a1', 1798], ['a2', 450]], 'unrealized_gain': -250}]], [['regression: liability gain sign', {'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: liability gain sign, partial-repair probe', {'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 1', {'bid': [6, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 3, 500], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 3, 3], ['a4', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 749], ['a3', 3], ['a4', 749]], 'unrealized_gain': -1502}], ['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}]], [['regression: liability gain sign', {'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}], ['regression: liability gain sign, partial-repair probe', {'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 1', {'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 2', {'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 3', {'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 4', {'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 5', {'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)

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 - carried
        gain += delta if kind == 'asset' else -abs(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: liability gain sign', {'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': [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 2', {'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 3', {'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 4', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 4], ['a1', 625]], 'unrealized_gain': 125}], ['control 5', {'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}], ['control 6', {'bid': [20, 10], 'ask': [21, 10], 'accounts': [['a0', 'asset', True, 5, 5], ['a1', 'asset', True, 250, 253], ['a2', 'asset', True, 5, 8], ['a3', 'asset', True, 7, 14]]}, {'balances': [['a0', 10], ['a1', 500], ['a2', 10], ['a3', 14]], 'unrealized_gain': 254}]], [['regression: liability gain sign', {'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': [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': [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 3', {'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 4', {'bid': [16, 8], 'ask': [18, 8], 'accounts': [['a0', 'liability', False, 3, 6], ['a1', 'asset', True, 999, 1002]]}, {'balances': [['a0', 6], ['a1', 1998]], 'unrealized_gain': 996}], ['control 5', {'bid': [10, 8], 'ask': [11, 8], 'accounts': [['a0', 'asset', True, 1, 500], ['a1', 'asset', False, 999, 999], ['a2', 'asset', True, 250, 250], ['a3', 'liability', False, 999, 1002]]}, {'balances': [['a0', 1], ['a1', 999], ['a2', 313], ['a3', 1002]], 'unrealized_gain': -436}], ['control 6', {'bid': [8, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 3, 3], ['a1', 'asset', True, 3, 6], ['a2', 'asset', False, 1000, 1000], ['a3', 'liability', False, 5, 500], ['a4', 'asset', True, 101, 500]]}, {'balances': [['a0', 3], ['a1', 6], ['a2', 1000], ['a3', 500], ['a4', 202]], 'unrealized_gain': -298}]], [['regression: liability gain sign', {'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': [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}], ['control 6', {'bid': [18, 10], 'ask': [20, 10], 'accounts': [['a0', 'liability', True, 1, 2], ['a1', 'asset', True, 999, 1998], ['a2', 'asset', True, 250, 500]]}, {'balances': [['a0', 2], ['a1', 1798], ['a2', 450]], 'unrealized_gain': -250}]], [['regression: liability gain sign', {'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: liability gain sign, partial-repair probe', {'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 1', {'bid': [6, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 3, 500], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 3, 3], ['a4', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 749], ['a3', 3], ['a4', 749]], 'unrealized_gain': -1502}], ['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}]], [['regression: liability gain sign', {'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}], ['regression: liability gain sign, partial-repair probe', {'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 1', {'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 2', {'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 3', {'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 4', {'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 5', {'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:
A weakening home currency reports a gain on foreign-currency payables.

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 and kind != 'asset':
            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: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 3, 500], ['a1', 'asset', True, 3, 3]]}, {'balances': [['a0', 8], ['a1', 3]], 'unrealized_gain': 492}], ['control 2', {'bid': [11, 8], 'ask': [13, 8], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'asset', True, 101, 500], ['a2', 'liability', True, 250, 253], ['a3', 'liability', True, 1000, 2000], ['a4', 'asset', True, 7, 14]]}, {'balances': [['a0', 344], ['a1', 139], ['a2', 406], ['a3', 1625], ['a4', 10]], 'unrealized_gain': -299}], ['control 3', {'bid': [3, 4], 'ask': [5, 4], 'accounts': [['a0', 'asset', True, 1000, 2000], ['a1', 'liability', True, 999, 500], ['a2', 'liability', True, 3, 500], ['a3', 'liability', True, 1000, 1003], ['a4', 'asset', True, 1, 2]]}, {'balances': [['a0', 750], ['a1', 1249], ['a2', 4], ['a3', 1250], ['a4', 1]], 'unrealized_gain': -1751}], ['control 4', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 4], ['a1', 625]], 'unrealized_gain': 125}], ['control 5', {'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: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [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 2', {'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 3', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'liability', True, 101, 202], ['a2', 'liability', False, 101, 104], ['a3', 'asset', True, 1000, 500], ['a4', 'asset', True, 5, 500]]}, {'balances': [['a0', 188], ['a1', 101], ['a2', 104], ['a3', 750], ['a4', 4]], 'unrealized_gain': -457}], ['control 4', {'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 5', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', True, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 1750]], 'unrealized_gain': 248}]], [['regression: non-monetary exclusion', {'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 1', {'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 2', {'bid': [7, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', True, 1000, 500], ['a1', 'liability', True, 1, 2], ['a2', 'asset', True, 250, 253], ['a3', 'liability', True, 101, 500], ['a4', 'asset', True, 5, 8]]}, {'balances': [['a0', 1750], ['a1', 2], ['a2', 438], ['a3', 227], ['a4', 9]], 'unrealized_gain': 1709}], ['control 3', {'bid': [135, 100], 'ask': [136, 100], 'accounts': [['a0', 'liability', True, 999, 1002], ['a1', 'liability', False, 1, 500], ['a2', 'liability', True, 5, 5]]}, {'balances': [['a0', 1359], ['a1', 500], ['a2', 7]], 'unrealized_gain': -359}], ['control 4', {'bid': [4, 2], 'ask': [6, 2], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 7, 10], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 3000], ['a1', 14], ['a2', 202]], 'unrealized_gain': -895}], ['control 5', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 101, 500], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 253], ['a1', 2]], 'unrealized_gain': 247}], ['control 6', {'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}]], [['regression: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 1000, 1003], ['a2', 'liability', False, 7, 14]]}, {'balances': [['a0', 2500], ['a1', 1750], ['a2', 14]], 'unrealized_gain': 247}], ['control 2', {'bid': [6, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 3, 500], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 3, 3], ['a4', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 749], ['a3', 3], ['a4', 749]], 'unrealized_gain': -1502}], ['control 3', {'bid': [6, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 7, 500], ['a1', 'liability', True, 3, 6]]}, {'balances': [['a0', 500], ['a1', 7]], 'unrealized_gain': -1}], ['control 4', {'bid': [97, 100], 'ask': [98, 100], 'accounts': [['a0', 'liability', False, 3, 3], ['a1', 'liability', True, 999, 999], ['a2', 'liability', False, 5, 10], ['a3', 'liability', True, 101, 104]]}, {'balances': [['a0', 3], ['a1', 979], ['a2', 10], ['a3', 99]], 'unrealized_gain': 25}], ['control 5', {'bid': [15, 10], 'ask': [16, 10], 'accounts': [['a0', 'asset', True, 3, 6], ['a1', 'asset', True, 7, 500], ['a2', 'liability', True, 999, 500], ['a3', 'liability', True, 3, 6], ['a4', 'liability', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 11], ['a2', 1598], ['a3', 5], ['a4', 2]], 'unrealized_gain': -1587}]], [['regression: non-monetary exclusion', {'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, 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 2', {'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 3', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', True, 7, 7], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 999], ['a1', 7], ['a2', 76]], 'unrealized_gain': -524}], ['control 4', {'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 5', {'bid': [167, 100], 'ask': [169, 100], 'accounts': [['a0', 'asset', True, 250, 250], ['a1', 'asset', True, 101, 104], ['a2', 'liability', True, 999, 1998]]}, {'balances': [['a0', 418], ['a1', 169], ['a2', 1688]], 'unrealized_gain': 543}], ['control 6', {'bid': [6, 10], 'ask': [7, 10], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', True, 3, 6]]}, {'balances': [['a0', 175], ['a1', 2]], 'unrealized_gain': 321}]]]
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 and kind == 'asset':
            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: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 3, 500], ['a1', 'asset', True, 3, 3]]}, {'balances': [['a0', 8], ['a1', 3]], 'unrealized_gain': 492}], ['control 2', {'bid': [11, 8], 'ask': [13, 8], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'asset', True, 101, 500], ['a2', 'liability', True, 250, 253], ['a3', 'liability', True, 1000, 2000], ['a4', 'asset', True, 7, 14]]}, {'balances': [['a0', 344], ['a1', 139], ['a2', 406], ['a3', 1625], ['a4', 10]], 'unrealized_gain': -299}], ['control 3', {'bid': [3, 4], 'ask': [5, 4], 'accounts': [['a0', 'asset', True, 1000, 2000], ['a1', 'liability', True, 999, 500], ['a2', 'liability', True, 3, 500], ['a3', 'liability', True, 1000, 1003], ['a4', 'asset', True, 1, 2]]}, {'balances': [['a0', 750], ['a1', 1249], ['a2', 4], ['a3', 1250], ['a4', 1]], 'unrealized_gain': -1751}], ['control 4', {'bid': [5, 2], 'ask': [8, 2], 'accounts': [['a0', 'liability', True, 1, 4], ['a1', 'asset', True, 250, 500]]}, {'balances': [['a0', 4], ['a1', 625]], 'unrealized_gain': 125}], ['control 5', {'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: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [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 2', {'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 3', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'liability', True, 101, 202], ['a2', 'liability', False, 101, 104], ['a3', 'asset', True, 1000, 500], ['a4', 'asset', True, 5, 500]]}, {'balances': [['a0', 188], ['a1', 101], ['a2', 104], ['a3', 750], ['a4', 4]], 'unrealized_gain': -457}], ['control 4', {'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 5', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', True, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 1750]], 'unrealized_gain': 248}]], [['regression: non-monetary exclusion', {'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 1', {'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 2', {'bid': [7, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', True, 1000, 500], ['a1', 'liability', True, 1, 2], ['a2', 'asset', True, 250, 253], ['a3', 'liability', True, 101, 500], ['a4', 'asset', True, 5, 8]]}, {'balances': [['a0', 1750], ['a1', 2], ['a2', 438], ['a3', 227], ['a4', 9]], 'unrealized_gain': 1709}], ['control 3', {'bid': [135, 100], 'ask': [136, 100], 'accounts': [['a0', 'liability', True, 999, 1002], ['a1', 'liability', False, 1, 500], ['a2', 'liability', True, 5, 5]]}, {'balances': [['a0', 1359], ['a1', 500], ['a2', 7]], 'unrealized_gain': -359}], ['control 4', {'bid': [4, 2], 'ask': [6, 2], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 7, 10], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 3000], ['a1', 14], ['a2', 202]], 'unrealized_gain': -895}], ['control 5', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 101, 500], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 253], ['a1', 2]], 'unrealized_gain': 247}], ['control 6', {'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}]], [['regression: non-monetary exclusion', {'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}], ['regression: non-monetary exclusion, partial-repair probe', {'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': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 1000, 1003], ['a2', 'liability', False, 7, 14]]}, {'balances': [['a0', 2500], ['a1', 1750], ['a2', 14]], 'unrealized_gain': 247}], ['control 2', {'bid': [6, 8], 'ask': [8, 8], 'accounts': [['a0', 'liability', False, 3, 500], ['a1', 'liability', True, 101, 101], ['a2', 'asset', True, 999, 1998], ['a3', 'liability', False, 3, 3], ['a4', 'asset', True, 999, 1002]]}, {'balances': [['a0', 500], ['a1', 101], ['a2', 749], ['a3', 3], ['a4', 749]], 'unrealized_gain': -1502}], ['control 3', {'bid': [6, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 7, 500], ['a1', 'liability', True, 3, 6]]}, {'balances': [['a0', 500], ['a1', 7]], 'unrealized_gain': -1}], ['control 4', {'bid': [97, 100], 'ask': [98, 100], 'accounts': [['a0', 'liability', False, 3, 3], ['a1', 'liability', True, 999, 999], ['a2', 'liability', False, 5, 10], ['a3', 'liability', True, 101, 104]]}, {'balances': [['a0', 3], ['a1', 979], ['a2', 10], ['a3', 99]], 'unrealized_gain': 25}], ['control 5', {'bid': [15, 10], 'ask': [16, 10], 'accounts': [['a0', 'asset', True, 3, 6], ['a1', 'asset', True, 7, 500], ['a2', 'liability', True, 999, 500], ['a3', 'liability', True, 3, 6], ['a4', 'liability', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 11], ['a2', 1598], ['a3', 5], ['a4', 2]], 'unrealized_gain': -1587}]], [['regression: non-monetary exclusion', {'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, 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 2', {'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 3', {'bid': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'liability', True, 999, 500], ['a1', 'liability', True, 7, 7], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 999], ['a1', 7], ['a2', 76]], 'unrealized_gain': -524}], ['control 4', {'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 5', {'bid': [167, 100], 'ask': [169, 100], 'accounts': [['a0', 'asset', True, 250, 250], ['a1', 'asset', True, 101, 104], ['a2', 'liability', True, 999, 1998]]}, {'balances': [['a0', 418], ['a1', 169], ['a2', 1688]], 'unrealized_gain': 543}], ['control 6', {'bid': [6, 10], 'ask': [7, 10], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', True, 3, 6]]}, {'balances': [['a0', 175], ['a1', 2]], 'unrealized_gain': 321}]]]
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:
Inventory and prepaid balances are remeasured at the closing 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 r > 0 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: half-up tie rounding', {'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}], ['regression: half-up tie rounding, partial-repair probe', {'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': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 3, 500], ['a1', 'asset', True, 3, 3]]}, {'balances': [['a0', 8], ['a1', 3]], 'unrealized_gain': 492}], ['control 2', {'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 3', {'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 4', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 1000, 1003], ['a1', 'liability', True, 1000, 1003], ['a2', 'asset', False, 1, 2], ['a3', 'liability', False, 999, 500], ['a4', 'liability', True, 7, 10]]}, {'balances': [['a0', 1003], ['a1', 2500], ['a2', 2], ['a3', 500], ['a4', 18]], 'unrealized_gain': -1505}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'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: half-up tie rounding, partial-repair probe', {'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 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': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', True, 999, 999], ['a2', 'liability', True, 1000, 500], ['a3', 'asset', False, 5, 500], ['a4', 'liability', False, 5, 10]]}, {'balances': [['a0', 625], ['a1', 1998], ['a2', 2500], ['a3', 500], ['a4', 10]], 'unrealized_gain': -1126}], ['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': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'liability', True, 101, 202], ['a2', 'liability', False, 101, 104], ['a3', 'asset', True, 1000, 500], ['a4', 'asset', True, 5, 500]]}, {'balances': [['a0', 188], ['a1', 101], ['a2', 104], ['a3', 750], ['a4', 4]], 'unrealized_gain': -457}], ['control 5', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', True, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 1750]], 'unrealized_gain': 248}]], [['regression: half-up tie rounding', {'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: half-up tie rounding, partial-repair probe', {'bid': [7, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', True, 1000, 500], ['a1', 'liability', True, 1, 2], ['a2', 'asset', True, 250, 253], ['a3', 'liability', True, 101, 500], ['a4', 'asset', True, 5, 8]]}, {'balances': [['a0', 1750], ['a1', 2], ['a2', 438], ['a3', 227], ['a4', 9]], 'unrealized_gain': 1709}], ['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': [135, 100], 'ask': [136, 100], 'accounts': [['a0', 'liability', True, 999, 1002], ['a1', 'liability', False, 1, 500], ['a2', 'liability', True, 5, 5]]}, {'balances': [['a0', 1359], ['a1', 500], ['a2', 7]], 'unrealized_gain': -359}], ['control 3', {'bid': [4, 2], 'ask': [6, 2], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 7, 10], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 3000], ['a1', 14], ['a2', 202]], 'unrealized_gain': -895}], ['control 4', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 101, 500], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 253], ['a1', 2]], 'unrealized_gain': 247}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'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}], ['regression: half-up tie rounding, partial-repair probe', {'bid': [15, 10], 'ask': [16, 10], 'accounts': [['a0', 'asset', True, 3, 6], ['a1', 'asset', True, 7, 500], ['a2', 'liability', True, 999, 500], ['a3', 'liability', True, 3, 6], ['a4', 'liability', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 11], ['a2', 1598], ['a3', 5], ['a4', 2]], 'unrealized_gain': -1587}], ['control 1', {'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 2', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 1000, 1003], ['a2', 'liability', False, 7, 14]]}, {'balances': [['a0', 2500], ['a1', 1750], ['a2', 14]], 'unrealized_gain': 247}], ['control 3', {'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 4', {'bid': [6, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 7, 500], ['a1', 'liability', True, 3, 6]]}, {'balances': [['a0', 500], ['a1', 7]], 'unrealized_gain': -1}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'bid': [167, 100], 'ask': [169, 100], 'accounts': [['a0', 'asset', True, 250, 250], ['a1', 'asset', True, 101, 104], ['a2', 'liability', True, 999, 1998]]}, {'balances': [['a0', 418], ['a1', 169], ['a2', 1688]], 'unrealized_gain': 543}], ['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': [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 4', {'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 5', {'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 6', {'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}]]]
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['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: half-up tie rounding', {'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}], ['regression: half-up tie rounding, partial-repair probe', {'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': [2, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 3, 500], ['a1', 'asset', True, 3, 3]]}, {'balances': [['a0', 8], ['a1', 3]], 'unrealized_gain': 492}], ['control 2', {'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 3', {'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 4', {'bid': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'asset', False, 1000, 1003], ['a1', 'liability', True, 1000, 1003], ['a2', 'asset', False, 1, 2], ['a3', 'liability', False, 999, 500], ['a4', 'liability', True, 7, 10]]}, {'balances': [['a0', 1003], ['a1', 2500], ['a2', 2], ['a3', 500], ['a4', 18]], 'unrealized_gain': -1505}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'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: half-up tie rounding, partial-repair probe', {'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 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': [4, 2], 'ask': [5, 2], 'accounts': [['a0', 'liability', True, 250, 500], ['a1', 'asset', True, 999, 999], ['a2', 'liability', True, 1000, 500], ['a3', 'asset', False, 5, 500], ['a4', 'liability', False, 5, 10]]}, {'balances': [['a0', 625], ['a1', 1998], ['a2', 2500], ['a3', 500], ['a4', 10]], 'unrealized_gain': -1126}], ['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': [3, 4], 'ask': [4, 4], 'accounts': [['a0', 'asset', True, 250, 500], ['a1', 'liability', True, 101, 202], ['a2', 'liability', False, 101, 104], ['a3', 'asset', True, 1000, 500], ['a4', 'asset', True, 5, 500]]}, {'balances': [['a0', 188], ['a1', 101], ['a2', 104], ['a3', 750], ['a4', 4]], 'unrealized_gain': -457}], ['control 5', {'bid': [6, 4], 'ask': [7, 4], 'accounts': [['a0', 'asset', True, 1, 4], ['a1', 'liability', True, 1000, 2000]]}, {'balances': [['a0', 2], ['a1', 1750]], 'unrealized_gain': 248}]], [['regression: half-up tie rounding', {'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: half-up tie rounding, partial-repair probe', {'bid': [7, 4], 'ask': [9, 4], 'accounts': [['a0', 'asset', True, 1000, 500], ['a1', 'liability', True, 1, 2], ['a2', 'asset', True, 250, 253], ['a3', 'liability', True, 101, 500], ['a4', 'asset', True, 5, 8]]}, {'balances': [['a0', 1750], ['a1', 2], ['a2', 438], ['a3', 227], ['a4', 9]], 'unrealized_gain': 1709}], ['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': [135, 100], 'ask': [136, 100], 'accounts': [['a0', 'liability', True, 999, 1002], ['a1', 'liability', False, 1, 500], ['a2', 'liability', True, 5, 5]]}, {'balances': [['a0', 1359], ['a1', 500], ['a2', 7]], 'unrealized_gain': -359}], ['control 3', {'bid': [4, 2], 'ask': [6, 2], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 7, 10], ['a2', 'asset', True, 101, 101]]}, {'balances': [['a0', 3000], ['a1', 14], ['a2', 202]], 'unrealized_gain': -895}], ['control 4', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 101, 500], ['a1', 'asset', True, 1, 2]]}, {'balances': [['a0', 253], ['a1', 2]], 'unrealized_gain': 247}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'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}], ['regression: half-up tie rounding, partial-repair probe', {'bid': [15, 10], 'ask': [16, 10], 'accounts': [['a0', 'asset', True, 3, 6], ['a1', 'asset', True, 7, 500], ['a2', 'liability', True, 999, 500], ['a3', 'liability', True, 3, 6], ['a4', 'liability', True, 1, 2]]}, {'balances': [['a0', 5], ['a1', 11], ['a2', 1598], ['a3', 5], ['a4', 2]], 'unrealized_gain': -1587}], ['control 1', {'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 2', {'bid': [7, 4], 'ask': [10, 4], 'accounts': [['a0', 'liability', True, 1000, 2000], ['a1', 'asset', True, 1000, 1003], ['a2', 'liability', False, 7, 14]]}, {'balances': [['a0', 2500], ['a1', 1750], ['a2', 14]], 'unrealized_gain': 247}], ['control 3', {'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 4', {'bid': [6, 4], 'ask': [9, 4], 'accounts': [['a0', 'liability', False, 7, 500], ['a1', 'liability', True, 3, 6]]}, {'balances': [['a0', 500], ['a1', 7]], 'unrealized_gain': -1}], ['control 5', {'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}]], [['regression: half-up tie rounding', {'bid': [167, 100], 'ask': [169, 100], 'accounts': [['a0', 'asset', True, 250, 250], ['a1', 'asset', True, 101, 104], ['a2', 'liability', True, 999, 1998]]}, {'balances': [['a0', 418], ['a1', 169], ['a2', 1688]], 'unrealized_gain': 543}], ['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': [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 4', {'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 5', {'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 6', {'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}]]]
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:
Converted balances are a cent high on most accounts.

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