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

N = 1
observations = []
def solve(x):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = a
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: eliminated amount', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 2', [['P', 'ic_purchases', 'S2', 100], ['P', 'ic_receivable', 'P', 400], ['S2', 'cash', None, 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_receivable', None, 400], ['S2', 'ic_receivable', None, -400], ['S1', 'ic_payable', 'P', 100]], {'consolidated': {'cash': 400, 'ic_payable': 100, 'ic_purchases': 100, 'ic_receivable': 500}, 'mismatches': [['P', 'S1', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -100]]}], ['control 3', [['S1', 'ic_payable', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 250], ['S2', 'ic_payable', 'S1', 1000], ['S2', 'ic_payable', 'P', 1000]], {'consolidated': {'ic_payable': 2750}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S1', 'S2', 'ic_receivable', -750], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 4', [['P', 'ic_purchases', 'S1', 100], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'S2', 400], ['S1', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 1000], ['P', 'ic_purchases', 'S2', 100]], {'consolidated': {'cash': 100, 'ic_loan': 1400, 'ic_purchases': 1200}, 'mismatches': [['S1', 'P', 'ic_sales', -100], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 5', [['P', 'ic_purchases', 'P', 400], ['P', 'sales', None, -1000], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_purchases': 400, 'sales': -1000}, 'mismatches': []}], ['control 6', [['S1', 'sales', None, 1000], ['S1', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_purchases': 400, 'sales': 1000}, 'mismatches': []}]], [['regression: eliminated amount', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 1', [['S1', 'ic_purchases', 'P', 400], ['P', 'ic_loan', 'S1', 250], ['P', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'S1', 250], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_payable', 'S2', 400]], {'consolidated': {'ic_loan': 650, 'ic_payable': 800, 'ic_purchases': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_sales', -400], ['S2', 'P', 'ic_receivable', -400]]}], ['control 2', [['S1', 'ic_loan', 'S1', 100], ['S1', 'ic_purchases', 'S2', 1000], ['S2', 'ic_loan', 'P', 400], ['P', 'ic_payable', 'P', 400], ['S2', 'ic_purchases', 'S2', 400]], {'consolidated': {'ic_loan': 500, 'ic_payable': 400, 'ic_purchases': 1400}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 3', [['P', 'ic_receivable', None, -400], ['P', 'ic_loan', 'P', 1000], ['P', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 1400, 'ic_receivable': -400}, 'mismatches': []}], ['control 4', [['P', 'ic_sales', 'P', 400], ['S2', 'ic_purchases', 'S1', 400], ['P', 'ic_sales', 'P', 100], ['S2', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_purchases': 400, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_sales', -400]]}], ['control 5', [['P', 'ic_purchases', 'S2', 250], ['S2', 'sales', None, 100], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'S1', 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_payable', 'S1', 400], ['P', 'ic_payable', 'S1', 400], ['P', 'ic_receivable', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 400, 'ic_payable': 800, 'ic_purchases': 250, 'ic_receivable': 200, 'sales': 100}, 'mismatches': [['S1', 'P', 'ic_receivable', -800], ['S2', 'P', 'ic_sales', -250]]}], ['control 6', [['P', 'sales', None, 400], ['S2', 'sales', None, 100], ['S2', 'ic_payable', 'P', 400]], {'consolidated': {'ic_payable': 400, 'sales': 500}, 'mismatches': [['P', 'S2', 'ic_receivable', -400]]}]], [['regression: eliminated amount', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 1', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}], ['control 2', [['S1', 'ic_payable', 'S2', 100], ['P', 'ic_loan', 'S1', 100], ['S1', 'ic_purchases', 'P', 400]], {'consolidated': {'ic_loan': 100, 'ic_payable': 100, 'ic_purchases': 400}, 'mismatches': [['P', 'S1', 'ic_sales', -400], ['S2', 'S1', 'ic_receivable', -100]]}], ['control 3', [['S1', 'cash', None, 1000], ['P', 'ic_sales', 'P', 400], ['P', 'ic_receivable', 'P', 1000]], {'consolidated': {'cash': 1000, 'ic_receivable': 1000, 'ic_sales': 400}, 'mismatches': []}], ['control 4', [['S1', 'sales', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['P', 'sales', None, 400]], {'consolidated': {'ic_purchases': 400, 'sales': 1400}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 5', [['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_purchases', 'P', 1000]], {'consolidated': {'ic_payable': 100, 'ic_purchases': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -100]]}], ['control 6', [['S1', 'cash', None, 100], ['S1', 'sales', None, 100], ['S2', 'cash', None, -1000], ['S1', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'S2', 250]], {'consolidated': {'cash': -900, 'ic_loan': 250, 'ic_payable': 400, 'sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', -400]]}]], [['regression: eliminated amount', [['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'P', 100], ['S1', 'ic_payable', 'S1', 100], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 1000], ['P', 'ic_sales', 'P', 100], ['S1', 'ic_loan', 'S1', 1000], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 1000, 'ic_payable': 100, 'ic_purchases': 1100, 'ic_receivable': 100, 'ic_sales': 200}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['P', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 3', [['S2', 'ic_loan', 'S1', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, 400], ['P', 'sales', None, 250], ['P', 'ic_loan', 'S1', 100]], {'consolidated': {'ic_loan': 1500, 'ic_payable': 100, 'ic_purchases': 400, 'sales': 650}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100]]}], ['control 4', [['S2', 'ic_payable', 'P', 1000], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_purchases', 'S1', 100], ['P', 'ic_loan', 'S2', 400], ['P', 'ic_purchases', 'S2', 100], ['S2', 'ic_purchases', 'S2', 100], ['P', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 400, 'ic_payable': 1000, 'ic_purchases': 300, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 5', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 6', [['S2', 'ic_loan', 'S2', 250], ['S2', 'ic_loan', 'S1', 250], ['P', 'ic_loan', 'P', 400], ['S2', 'ic_purchases', 'P', 400], ['P', 'ic_payable', 'S1', 100], ['P', 'sales', None, -100]], {'consolidated': {'ic_loan': 900, 'ic_payable': 100, 'ic_purchases': 400, 'sales': -100}, 'mismatches': [['P', 'S2', 'ic_sales', -400], ['S1', 'P', 'ic_receivable', -100]]}]], [['regression: eliminated amount', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 2', [['S2', 'ic_loan', 'P', 400], ['P', 'cash', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_loan', 'S1', 1000], ['S1', 'cash', None, -100], ['S1', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 900, 'ic_loan': 1400, 'ic_purchases': 800}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 3', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_sales', 'S2', 250], ['P', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 400, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 4', [['S2', 'cash', None, 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 1000]], {'consolidated': {'cash': 250, 'ic_loan': 400, 'ic_purchases': 1000}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 5', [['S2', 'ic_payable', 'S2', 100], ['S1', 'ic_sales', 'S1', 100], ['P', 'ic_purchases', 'S1', 1000], ['P', 'ic_loan', 'P', 250]], {'consolidated': {'ic_loan': 250, 'ic_payable': 100, 'ic_purchases': 1000, 'ic_sales': 100}, 'mismatches': [['S1', 'P', 'ic_sales', -1000]]}], ['control 6', [['S1', 'ic_purchases', 'S1', 400], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_purchases', 'P', 100], ['S1', 'cash', None, -400]], {'consolidated': {'cash': -400, 'ic_payable': 1000, 'ic_purchases': 500}, 'mismatches': [['P', 'S2', 'ic_sales', -100], ['S2', 'S1', 'ic_receivable', -1000]]}]]]
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):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = max(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: eliminated amount', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 2', [['P', 'ic_purchases', 'S2', 100], ['P', 'ic_receivable', 'P', 400], ['S2', 'cash', None, 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_receivable', None, 400], ['S2', 'ic_receivable', None, -400], ['S1', 'ic_payable', 'P', 100]], {'consolidated': {'cash': 400, 'ic_payable': 100, 'ic_purchases': 100, 'ic_receivable': 500}, 'mismatches': [['P', 'S1', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -100]]}], ['control 3', [['S1', 'ic_payable', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 250], ['S2', 'ic_payable', 'S1', 1000], ['S2', 'ic_payable', 'P', 1000]], {'consolidated': {'ic_payable': 2750}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S1', 'S2', 'ic_receivable', -750], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 4', [['P', 'ic_purchases', 'S1', 100], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'S2', 400], ['S1', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 1000], ['P', 'ic_purchases', 'S2', 100]], {'consolidated': {'cash': 100, 'ic_loan': 1400, 'ic_purchases': 1200}, 'mismatches': [['S1', 'P', 'ic_sales', -100], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 5', [['P', 'ic_purchases', 'P', 400], ['P', 'sales', None, -1000], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_purchases': 400, 'sales': -1000}, 'mismatches': []}], ['control 6', [['S1', 'sales', None, 1000], ['S1', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_purchases': 400, 'sales': 1000}, 'mismatches': []}]], [['regression: eliminated amount', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 1', [['S1', 'ic_purchases', 'P', 400], ['P', 'ic_loan', 'S1', 250], ['P', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'S1', 250], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_payable', 'S2', 400]], {'consolidated': {'ic_loan': 650, 'ic_payable': 800, 'ic_purchases': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_sales', -400], ['S2', 'P', 'ic_receivable', -400]]}], ['control 2', [['S1', 'ic_loan', 'S1', 100], ['S1', 'ic_purchases', 'S2', 1000], ['S2', 'ic_loan', 'P', 400], ['P', 'ic_payable', 'P', 400], ['S2', 'ic_purchases', 'S2', 400]], {'consolidated': {'ic_loan': 500, 'ic_payable': 400, 'ic_purchases': 1400}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 3', [['P', 'ic_receivable', None, -400], ['P', 'ic_loan', 'P', 1000], ['P', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 1400, 'ic_receivable': -400}, 'mismatches': []}], ['control 4', [['P', 'ic_sales', 'P', 400], ['S2', 'ic_purchases', 'S1', 400], ['P', 'ic_sales', 'P', 100], ['S2', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_purchases': 400, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_sales', -400]]}], ['control 5', [['P', 'ic_purchases', 'S2', 250], ['S2', 'sales', None, 100], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'S1', 400], ['P', 'ic_receivable', 'P', 100], ['P', 'ic_payable', 'S1', 400], ['P', 'ic_payable', 'S1', 400], ['P', 'ic_receivable', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 400, 'ic_payable': 800, 'ic_purchases': 250, 'ic_receivable': 200, 'sales': 100}, 'mismatches': [['S1', 'P', 'ic_receivable', -800], ['S2', 'P', 'ic_sales', -250]]}], ['control 6', [['P', 'sales', None, 400], ['S2', 'sales', None, 100], ['S2', 'ic_payable', 'P', 400]], {'consolidated': {'ic_payable': 400, 'sales': 500}, 'mismatches': [['P', 'S2', 'ic_receivable', -400]]}]], [['regression: eliminated amount', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 1', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}], ['control 2', [['S1', 'ic_payable', 'S2', 100], ['P', 'ic_loan', 'S1', 100], ['S1', 'ic_purchases', 'P', 400]], {'consolidated': {'ic_loan': 100, 'ic_payable': 100, 'ic_purchases': 400}, 'mismatches': [['P', 'S1', 'ic_sales', -400], ['S2', 'S1', 'ic_receivable', -100]]}], ['control 3', [['S1', 'cash', None, 1000], ['P', 'ic_sales', 'P', 400], ['P', 'ic_receivable', 'P', 1000]], {'consolidated': {'cash': 1000, 'ic_receivable': 1000, 'ic_sales': 400}, 'mismatches': []}], ['control 4', [['S1', 'sales', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['P', 'sales', None, 400]], {'consolidated': {'ic_purchases': 400, 'sales': 1400}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 5', [['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_purchases', 'P', 1000]], {'consolidated': {'ic_payable': 100, 'ic_purchases': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -100]]}], ['control 6', [['S1', 'cash', None, 100], ['S1', 'sales', None, 100], ['S2', 'cash', None, -1000], ['S1', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'S2', 250]], {'consolidated': {'cash': -900, 'ic_loan': 250, 'ic_payable': 400, 'sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', -400]]}]], [['regression: eliminated amount', [['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'P', 100], ['S1', 'ic_payable', 'S1', 100], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 1000], ['P', 'ic_sales', 'P', 100], ['S1', 'ic_loan', 'S1', 1000], ['S1', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_loan': 1000, 'ic_payable': 100, 'ic_purchases': 1100, 'ic_receivable': 100, 'ic_sales': 200}, 'mismatches': [['P', 'S1', 'ic_sales', -100], ['P', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 3', [['S2', 'ic_loan', 'S1', 1000], ['S2', 'ic_payable', 'S1', 100], ['S2', 'ic_loan', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, 400], ['P', 'sales', None, 250], ['P', 'ic_loan', 'S1', 100]], {'consolidated': {'ic_loan': 1500, 'ic_payable': 100, 'ic_purchases': 400, 'sales': 650}, 'mismatches': [['S1', 'S2', 'ic_receivable', -100]]}], ['control 4', [['S2', 'ic_payable', 'P', 1000], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_purchases', 'S1', 100], ['P', 'ic_loan', 'S2', 400], ['P', 'ic_purchases', 'S2', 100], ['S2', 'ic_purchases', 'S2', 100], ['P', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 400, 'ic_payable': 1000, 'ic_purchases': 300, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 5', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 6', [['S2', 'ic_loan', 'S2', 250], ['S2', 'ic_loan', 'S1', 250], ['P', 'ic_loan', 'P', 400], ['S2', 'ic_purchases', 'P', 400], ['P', 'ic_payable', 'S1', 100], ['P', 'sales', None, -100]], {'consolidated': {'ic_loan': 900, 'ic_payable': 100, 'ic_purchases': 400, 'sales': -100}, 'mismatches': [['P', 'S2', 'ic_sales', -400], ['S1', 'P', 'ic_receivable', -100]]}]], [['regression: eliminated amount', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 2', [['S2', 'ic_loan', 'P', 400], ['P', 'cash', None, 1000], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_loan', 'S1', 1000], ['S1', 'cash', None, -100], ['S1', 'ic_purchases', 'S1', 400]], {'consolidated': {'cash': 900, 'ic_loan': 1400, 'ic_purchases': 800}, 'mismatches': [['S1', 'P', 'ic_sales', -400]]}], ['control 3', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_sales', 'S2', 250], ['P', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 400, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['control 4', [['S2', 'cash', None, 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 1000]], {'consolidated': {'cash': 250, 'ic_loan': 400, 'ic_purchases': 1000}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 5', [['S2', 'ic_payable', 'S2', 100], ['S1', 'ic_sales', 'S1', 100], ['P', 'ic_purchases', 'S1', 1000], ['P', 'ic_loan', 'P', 250]], {'consolidated': {'ic_loan': 250, 'ic_payable': 100, 'ic_purchases': 1000, 'ic_sales': 100}, 'mismatches': [['S1', 'P', 'ic_sales', -1000]]}], ['control 6', [['S1', 'ic_purchases', 'S1', 400], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_purchases', 'P', 100], ['S1', 'cash', None, -400]], {'consolidated': {'cash': -400, 'ic_payable': 1000, 'ic_purchases': 500}, 'mismatches': [['P', 'S2', 'ic_sales', -100], ['S2', 'S1', 'ic_receivable', -1000]]}]]]
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:
Consolidation shows negative intercompany payables.

x = [[entity, account, counterparty or None, amount]]. Rows with no counterparty, the entity itself as counterparty, or an account outside the pairs (ic_receivable/ic_payable, ic_sales/ic_purchases) are consolidated as-is. For each (entity, counterparty, pair) the left balance of entity toward counterparty is matched against the right balance of counterparty toward entity; min(left, right) is eliminated from both, residuals are consolidated and a mismatch [entity, counterparty, left account, left - right] is reported when they differ. Return {'consolidated': nonzero account totals, 'mismatches': in sorted key order}.

split: open-access


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

N = 1
observations = []
def solve(x):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((ent, cp, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: orphan right-side balance', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 1', [['P', 'ic_loan', 'S1', 250], ['S1', 'ic_payable', 'S1', 100], ['S2', 'sales', None, 1000], ['S1', 'ic_receivable', 'P', 250], ['P', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1050, 'ic_payable': 100, 'ic_receivable': 250, 'sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', 250]]}], ['control 2', [['S2', 'cash', None, 400], ['S2', 'ic_receivable', 'S1', 250], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_receivable': 250}, 'mismatches': [['S2', 'S1', 'ic_receivable', 250]]}], ['control 3', [['S2', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', 'S2', 100], ['S1', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 100, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', 100]]}], ['control 4', [['S2', 'ic_payable', 'S2', 100], ['S1', 'ic_sales', 'P', 400], ['P', 'ic_sales', 'P', 400], ['P', 'sales', None, 400], ['S2', 'ic_receivable', 'S2', 400], ['S2', 'ic_sales', 'P', 250], ['S1', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 500, 'ic_receivable': 400, 'ic_sales': 1050, 'sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S2', 'P', 'ic_sales', 250]]}], ['control 5', [['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 250]], {'consolidated': {'ic_payable': 150, 'ic_receivable': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S2', 'S1', 'ic_receivable', -150]]}], ['control 6', [['P', 'ic_purchases', 'P', 400], ['P', 'sales', None, -1000], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_purchases': 400, 'sales': -1000}, 'mismatches': []}]], [['regression: orphan right-side balance', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 2', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_purchases', 'S1', 400], ['P', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 250], ['S2', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_purchases': 650, 'ic_sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', -250]]}], ['control 6', [['P', 'ic_sales', 'P', 1000], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_purchases', 'P', 100], ['P', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'S2', 250], ['S2', 'ic_sales', 'P', 250], ['S2', 'cash', None, -400]], {'consolidated': {'cash': -400, 'ic_loan': 250, 'ic_payable': 400, 'ic_purchases': 500, 'ic_sales': 1250}, 'mismatches': [['S2', 'P', 'ic_sales', 250]]}]], [['regression: orphan right-side balance', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 1', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['control 2', [['S2', 'sales', None, -250], ['S2', 'ic_receivable', 'S1', 400], ['S2', 'cash', None, -400], ['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 400], ['S1', 'cash', None, 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 800, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'P', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 400]]}], ['control 3', [['P', 'ic_receivable', None, -250], ['S1', 'sales', None, -250], ['S2', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_receivable': -250, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['S2', 'S1', 'ic_sales', 400]]}], ['control 4', [['P', 'ic_receivable', 'S1', 400], ['P', 'ic_purchases', 'P', 250], ['S1', 'ic_purchases', 'S1', 1000], ['S2', 'ic_receivable', 'P', 100], ['S2', 'ic_receivable', None, -400]], {'consolidated': {'ic_purchases': 1250, 'ic_receivable': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S2', 'P', 'ic_receivable', 100]]}], ['control 5', [['S2', 'ic_receivable', 'P', 100], ['S1', 'ic_purchases', 'S2', 250], ['S2', 'ic_purchases', 'S1', 250]], {'consolidated': {'ic_purchases': 500, 'ic_receivable': 100}, 'mismatches': [['S1', 'S2', 'ic_sales', -250], ['S2', 'P', 'ic_receivable', 100], ['S2', 'S1', 'ic_sales', -250]]}], ['control 6', [['S1', 'ic_sales', 'S2', 400], ['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'S1', 1000], ['P', 'ic_receivable', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['P', 'S2', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 400]]}]], [['regression: orphan right-side balance', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 3', [['S2', 'ic_purchases', 'S2', 100], ['S1', 'ic_loan', 'P', 1000], ['P', 'ic_receivable', 'P', 100]], {'consolidated': {'ic_loan': 1000, 'ic_purchases': 100, 'ic_receivable': 100}, 'mismatches': []}], ['control 4', [['P', 'ic_sales', 'S2', 100], ['P', 'ic_loan', 'S2', 100], ['S1', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 400, 'ic_sales': 100}, 'mismatches': [['P', 'S2', 'ic_sales', 100]]}], ['control 5', [['S1', 'ic_payable', 'P', 250], ['P', 'ic_loan', 'P', 100], ['P', 'ic_payable', 'S1', 1000], ['S1', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 100, 'ic_payable': 1250}, 'mismatches': [['P', 'S1', 'ic_receivable', -250], ['S1', 'P', 'ic_receivable', -1000]]}], ['control 6', [['S1', 'ic_receivable', 'P', 400], ['P', 'ic_loan', 'S1', 1000], ['P', 'ic_receivable', None, 1000], ['S2', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1400, 'ic_receivable': 1400}, 'mismatches': [['S1', 'P', 'ic_receivable', 400]]}]], [['regression: orphan right-side balance', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['S1', 'ic_sales', 'S1', 1000], ['S1', 'ic_receivable', 'S2', 100], ['P', 'ic_sales', 'S2', 400], ['P', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_receivable': 100, 'ic_sales': 1650}, 'mismatches': [['P', 'S1', 'ic_sales', 250], ['P', 'S2', 'ic_sales', 400], ['S1', 'S2', 'ic_receivable', 100]]}], ['control 3', [['P', 'ic_loan', 'P', 100], ['P', 'ic_loan', 'S2', 1000], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'P', 100], ['S2', 'ic_receivable', 'S1', 400], ['S1', 'ic_payable', 'S1', 400], ['P', 'sales', None, -1000]], {'consolidated': {'cash': -400, 'ic_loan': 1200, 'ic_payable': 400, 'ic_receivable': 400, 'sales': -1000}, 'mismatches': [['S2', 'S1', 'ic_receivable', 400]]}], ['control 4', [['P', 'sales', None, -100], ['P', 'ic_receivable', 'P', 1000], ['P', 'ic_sales', 'S1', 400], ['S2', 'ic_receivable', 'P', 250], ['P', 'cash', None, 250], ['P', 'ic_purchases', 'P', 400]], {'consolidated': {'cash': 250, 'ic_purchases': 400, 'ic_receivable': 1250, 'ic_sales': 400, 'sales': -100}, 'mismatches': [['P', 'S1', 'ic_sales', 400], ['S2', 'P', 'ic_receivable', 250]]}], ['control 5', [['P', 'ic_sales', 'S1', 1000], ['S2', 'ic_loan', 'P', 1000], ['S1', 'ic_payable', 'S1', 400], ['S1', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'S1', 400], ['S2', 'sales', None, 400]], {'consolidated': {'ic_loan': 1800, 'ic_payable': 400, 'ic_sales': 1000, 'sales': 400}, 'mismatches': [['P', 'S1', 'ic_sales', 1000]]}], ['control 6', [['S2', 'ic_loan', 'P', 100], ['S2', 'ic_receivable', 'S1', 100], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_sales', 'S2', 400], ['S1', 'sales', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 100, 'ic_sales': 800, 'sales': 400}, 'mismatches': [['S1', 'S2', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 100]]}]]]
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):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                pass
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: orphan right-side balance', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 1', [['P', 'ic_loan', 'S1', 250], ['S1', 'ic_payable', 'S1', 100], ['S2', 'sales', None, 1000], ['S1', 'ic_receivable', 'P', 250], ['P', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1050, 'ic_payable': 100, 'ic_receivable': 250, 'sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', 250]]}], ['control 2', [['S2', 'cash', None, 400], ['S2', 'ic_receivable', 'S1', 250], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_receivable': 250}, 'mismatches': [['S2', 'S1', 'ic_receivable', 250]]}], ['control 3', [['S2', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', 'S2', 100], ['S1', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 100, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', 100]]}], ['control 4', [['S2', 'ic_payable', 'S2', 100], ['S1', 'ic_sales', 'P', 400], ['P', 'ic_sales', 'P', 400], ['P', 'sales', None, 400], ['S2', 'ic_receivable', 'S2', 400], ['S2', 'ic_sales', 'P', 250], ['S1', 'ic_payable', 'S1', 400]], {'consolidated': {'ic_payable': 500, 'ic_receivable': 400, 'ic_sales': 1050, 'sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S2', 'P', 'ic_sales', 250]]}], ['control 5', [['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 250]], {'consolidated': {'ic_payable': 150, 'ic_receivable': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S2', 'S1', 'ic_receivable', -150]]}], ['control 6', [['P', 'ic_purchases', 'P', 400], ['P', 'sales', None, -1000], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_purchases': 400, 'sales': -1000}, 'mismatches': []}]], [['regression: orphan right-side balance', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 2', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_purchases', 'S1', 400], ['P', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 250], ['S2', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_purchases': 650, 'ic_sales': 400}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', -250]]}], ['control 6', [['P', 'ic_sales', 'P', 1000], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_purchases', 'P', 100], ['P', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'S2', 250], ['S2', 'ic_sales', 'P', 250], ['S2', 'cash', None, -400]], {'consolidated': {'cash': -400, 'ic_loan': 250, 'ic_payable': 400, 'ic_purchases': 500, 'ic_sales': 1250}, 'mismatches': [['S2', 'P', 'ic_sales', 250]]}]], [['regression: orphan right-side balance', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 1', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['control 2', [['S2', 'sales', None, -250], ['S2', 'ic_receivable', 'S1', 400], ['S2', 'cash', None, -400], ['P', 'ic_receivable', 'S1', 400], ['S2', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 400], ['S1', 'cash', None, 400]], {'consolidated': {'ic_payable': 400, 'ic_receivable': 800, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'P', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 400]]}], ['control 3', [['P', 'ic_receivable', None, -250], ['S1', 'sales', None, -250], ['S2', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_receivable': -250, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['S2', 'S1', 'ic_sales', 400]]}], ['control 4', [['P', 'ic_receivable', 'S1', 400], ['P', 'ic_purchases', 'P', 250], ['S1', 'ic_purchases', 'S1', 1000], ['S2', 'ic_receivable', 'P', 100], ['S2', 'ic_receivable', None, -400]], {'consolidated': {'ic_purchases': 1250, 'ic_receivable': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S2', 'P', 'ic_receivable', 100]]}], ['control 5', [['S2', 'ic_receivable', 'P', 100], ['S1', 'ic_purchases', 'S2', 250], ['S2', 'ic_purchases', 'S1', 250]], {'consolidated': {'ic_purchases': 500, 'ic_receivable': 100}, 'mismatches': [['S1', 'S2', 'ic_sales', -250], ['S2', 'P', 'ic_receivable', 100], ['S2', 'S1', 'ic_sales', -250]]}], ['control 6', [['S1', 'ic_sales', 'S2', 400], ['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'S1', 1000], ['P', 'ic_receivable', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['P', 'S2', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 400]]}]], [['regression: orphan right-side balance', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['S2', 'ic_receivable', None, -400], ['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 100], ['S1', 'ic_payable', 'S2', 1000], ['S2', 'ic_sales', 'S2', 250], ['S2', 'ic_payable', 'S2', 100], ['S2', 'cash', None, 400], ['S2', 'ic_sales', 'S2', 250]], {'consolidated': {'cash': 400, 'ic_payable': 1500, 'ic_receivable': -300, 'ic_sales': 500}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 3', [['S2', 'ic_purchases', 'S2', 100], ['S1', 'ic_loan', 'P', 1000], ['P', 'ic_receivable', 'P', 100]], {'consolidated': {'ic_loan': 1000, 'ic_purchases': 100, 'ic_receivable': 100}, 'mismatches': []}], ['control 4', [['P', 'ic_sales', 'S2', 100], ['P', 'ic_loan', 'S2', 100], ['S1', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 400, 'ic_sales': 100}, 'mismatches': [['P', 'S2', 'ic_sales', 100]]}], ['control 5', [['S1', 'ic_payable', 'P', 250], ['P', 'ic_loan', 'P', 100], ['P', 'ic_payable', 'S1', 1000], ['S1', 'cash', None, -100]], {'consolidated': {'cash': -100, 'ic_loan': 100, 'ic_payable': 1250}, 'mismatches': [['P', 'S1', 'ic_receivable', -250], ['S1', 'P', 'ic_receivable', -1000]]}], ['control 6', [['S1', 'ic_receivable', 'P', 400], ['P', 'ic_loan', 'S1', 1000], ['P', 'ic_receivable', None, 1000], ['S2', 'ic_loan', 'P', 400]], {'consolidated': {'ic_loan': 1400, 'ic_receivable': 1400}, 'mismatches': [['S1', 'P', 'ic_receivable', 400]]}]], [['regression: orphan right-side balance', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['S1', 'ic_sales', 'S1', 1000], ['S1', 'ic_receivable', 'S2', 100], ['P', 'ic_sales', 'S2', 400], ['P', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_receivable': 100, 'ic_sales': 1650}, 'mismatches': [['P', 'S1', 'ic_sales', 250], ['P', 'S2', 'ic_sales', 400], ['S1', 'S2', 'ic_receivable', 100]]}], ['control 3', [['P', 'ic_loan', 'P', 100], ['P', 'ic_loan', 'S2', 1000], ['S1', 'cash', None, -400], ['S2', 'ic_loan', 'P', 100], ['S2', 'ic_receivable', 'S1', 400], ['S1', 'ic_payable', 'S1', 400], ['P', 'sales', None, -1000]], {'consolidated': {'cash': -400, 'ic_loan': 1200, 'ic_payable': 400, 'ic_receivable': 400, 'sales': -1000}, 'mismatches': [['S2', 'S1', 'ic_receivable', 400]]}], ['control 4', [['P', 'sales', None, -100], ['P', 'ic_receivable', 'P', 1000], ['P', 'ic_sales', 'S1', 400], ['S2', 'ic_receivable', 'P', 250], ['P', 'cash', None, 250], ['P', 'ic_purchases', 'P', 400]], {'consolidated': {'cash': 250, 'ic_purchases': 400, 'ic_receivable': 1250, 'ic_sales': 400, 'sales': -100}, 'mismatches': [['P', 'S1', 'ic_sales', 400], ['S2', 'P', 'ic_receivable', 250]]}], ['control 5', [['P', 'ic_sales', 'S1', 1000], ['S2', 'ic_loan', 'P', 1000], ['S1', 'ic_payable', 'S1', 400], ['S1', 'ic_loan', 'P', 400], ['S1', 'ic_loan', 'S1', 400], ['S2', 'sales', None, 400]], {'consolidated': {'ic_loan': 1800, 'ic_payable': 400, 'ic_sales': 1000, 'sales': 400}, 'mismatches': [['P', 'S1', 'ic_sales', 1000]]}], ['control 6', [['S2', 'ic_loan', 'P', 100], ['S2', 'ic_receivable', 'S1', 100], ['P', 'ic_sales', 'P', 400], ['S1', 'ic_sales', 'S2', 400], ['S1', 'sales', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 100, 'ic_sales': 800, 'sales': 400}, 'mismatches': [['S1', 'S2', 'ic_sales', 400], ['S2', 'S1', 'ic_receivable', 100]]}]]]
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 payable with no matching receivable is eliminated against the wrong pair or reported backwards.

x = [[entity, account, counterparty or None, amount]]. Rows with no counterparty, the entity itself as counterparty, or an account outside the pairs (ic_receivable/ic_payable, ic_sales/ic_purchases) are consolidated as-is. For each (entity, counterparty, pair) the left balance of entity toward counterparty is matched against the right balance of counterparty toward entity; min(left, right) is eliminated from both, residuals are consolidated and a mismatch [entity, counterparty, left account, left - right] is reported when they differ. Return {'consolidated': nonzero account totals, 'mismatches': in sorted key order}.

split: open-access


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

N = 1
observations = []
def solve(x):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or acct not in PAIRED:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: self counterparty', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 2', [['S2', 'ic_receivable', 'P', 1000], ['P', 'ic_payable', 'S1', 400], ['S2', 'ic_purchases', 'S1', 100]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 100, 'ic_receivable': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S1', 'S2', 'ic_sales', -100], ['S2', 'P', 'ic_receivable', 1000]]}], ['control 3', [['S2', 'cash', None, 400], ['S2', 'ic_receivable', 'S1', 250], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_receivable': 250}, 'mismatches': [['S2', 'S1', 'ic_receivable', 250]]}], ['control 4', [['S1', 'ic_payable', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 250], ['S2', 'ic_payable', 'S1', 1000], ['S2', 'ic_payable', 'P', 1000]], {'consolidated': {'ic_payable': 2750}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S1', 'S2', 'ic_receivable', -750], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 5', [['P', 'ic_purchases', 'S1', 100], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'S2', 400], ['S1', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 1000], ['P', 'ic_purchases', 'S2', 100]], {'consolidated': {'cash': 100, 'ic_loan': 1400, 'ic_purchases': 1200}, 'mismatches': [['S1', 'P', 'ic_sales', -100], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 6', [['P', 'ic_payable', 'S1', 250], ['S1', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'P', 250], ['P', 'ic_payable', 'S2', 400], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_payable': 900, 'ic_purchases': 100, 'ic_sales': 250}, 'mismatches': [['P', 'S2', 'ic_receivable', -250], ['P', 'S2', 'ic_sales', -100], ['S1', 'P', 'ic_receivable', -250], ['S1', 'P', 'ic_sales', 250], ['S2', 'P', 'ic_receivable', -400]]}]], [['regression: self counterparty', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 2', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S1', 'ic_receivable', 'P', 100], ['P', 'ic_payable', 'S2', 100], ['P', 'ic_receivable', 'S1', 1000], ['P', 'ic_sales', 'S1', 100], ['P', 'ic_sales', 'S1', 1000], ['S2', 'ic_payable', 'P', 400], ['P', 'ic_purchases', 'S2', 1000]], {'consolidated': {'ic_payable': 500, 'ic_purchases': 1000, 'ic_receivable': 1100, 'ic_sales': 1100}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['P', 'S1', 'ic_sales', 1100], ['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 100], ['S2', 'P', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -1000]]}], ['control 6', [['P', 'cash', None, 400], ['S2', 'sales', None, -250], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_receivable', 'S2', 100], ['P', 'sales', None, 250], ['P', 'ic_receivable', 'S2', 1000]], {'consolidated': {'cash': 400, 'ic_payable': 400, 'ic_receivable': 1100}, 'mismatches': [['P', 'S2', 'ic_receivable', 1000], ['S1', 'S2', 'ic_receivable', 100], ['S2', 'S1', 'ic_receivable', -400]]}]], [['regression: self counterparty', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['regression: self counterparty, partial-repair probe', [['P', 'ic_loan', 'P', 1000], ['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'P', 1000], ['S1', 'ic_receivable', 'P', 1000], ['P', 'cash', None, 400], ['S2', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S1', 100]], {'consolidated': {'cash': 400, 'ic_loan': 2100, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 1000], ['S1', 'S2', 'ic_sales', -400]]}], ['control 1', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 2', [['S1', 'ic_payable', 'P', 1000], ['S1', 'ic_loan', 'S1', 1000], ['P', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['P', 'S1', 'ic_sales', 400]]}], ['control 3', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}], ['control 4', [['S1', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', 'S2', 400], ['S1', 'ic_payable', 'P', 400], ['P', 'ic_loan', 'P', 400], ['S1', 'cash', None, 100], ['S1', 'ic_receivable', None, 100], ['S1', 'ic_purchases', 'P', 400]], {'consolidated': {'cash': 100, 'ic_loan': 400, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 500, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -400], ['P', 'S1', 'ic_sales', -400], ['P', 'S2', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 400]]}], ['control 5', [['P', 'ic_receivable', None, -250], ['S1', 'sales', None, -250], ['S2', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_receivable': -250, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['S2', 'S1', 'ic_sales', 400]]}]], [['regression: self counterparty', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['S2', 'ic_loan', 'S2', 250], ['S2', 'ic_loan', 'S1', 250], ['P', 'ic_loan', 'P', 400], ['S2', 'ic_purchases', 'P', 400], ['P', 'ic_payable', 'S1', 100], ['P', 'sales', None, -100]], {'consolidated': {'ic_loan': 900, 'ic_payable': 100, 'ic_purchases': 400, 'sales': -100}, 'mismatches': [['P', 'S2', 'ic_sales', -400], ['S1', 'P', 'ic_receivable', -100]]}], ['control 3', [['S2', 'ic_loan', 'S2', 100], ['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_payable', 'P', 100], ['P', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', None, 250], ['S1', 'cash', None, -400], ['P', 'ic_purchases', 'S1', 1000], ['P', 'ic_payable', 'S2', 400]], {'consolidated': {'cash': -400, 'ic_loan': 100, 'ic_payable': 500, 'ic_purchases': 1250, 'ic_receivable': 250, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -100], ['P', 'S2', 'ic_sales', 400], ['S1', 'P', 'ic_sales', -1000], ['S1', 'S2', 'ic_sales', -250], ['S2', 'P', 'ic_receivable', -400]]}], ['control 4', [['S1', 'ic_payable', 'P', 1000], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000], ['S1', 'ic_loan', 'P', 250]], {'consolidated': {'ic_loan': 250, 'ic_payable': 1000, 'ic_purchases': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 5', [['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 400], ['S2', 'ic_loan', 'P', 1000], ['S2', 'ic_loan', 'S1', 400]], {'consolidated': {'ic_loan': 1400, 'ic_payable': 400, 'ic_receivable': 400}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400]]}], ['control 6', [['P', 'ic_sales', 'S2', 100], ['P', 'ic_loan', 'S2', 100], ['S1', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 400, 'ic_sales': 100}, 'mismatches': [['P', 'S2', 'ic_sales', 100]]}]], [['regression: self counterparty', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['regression: self counterparty, partial-repair probe', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['P', 'ic_purchases', 'S2', 250], ['S1', 'ic_sales', 'P', 1000], ['S2', 'ic_payable', 'S1', 250], ['S2', 'ic_loan', 'S1', 1000]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 250, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_sales', 1000], ['S1', 'S2', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', -250]]}], ['control 3', [['S2', 'cash', None, 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 1000]], {'consolidated': {'cash': 250, 'ic_loan': 400, 'ic_purchases': 1000}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 4', [['P', 'sales', None, 250], ['S2', 'ic_receivable', 'P', 100], ['S2', 'ic_payable', 'P', 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_sales', 'S2', 250], ['S2', 'ic_receivable', None, 400], ['S2', 'ic_receivable', 'P', 400], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_payable': 250, 'ic_receivable': 900, 'ic_sales': 250, 'sales': 250}, 'mismatches': [['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_sales', 250], ['S2', 'P', 'ic_receivable', 500]]}], ['control 5', [['S2', 'ic_receivable', 'P', 400], ['P', 'ic_receivable', 'S2', 400], ['S2', 'ic_receivable', None, -1000]], {'consolidated': {'ic_receivable': -200}, 'mismatches': [['P', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_receivable', 400]]}]]]
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):
    PAIRS = (('ic_receivable', 'ic_payable'), ('ic_sales', 'ic_purchases'))
    PAIRED = {a for p in PAIRS for a in p}
    cons = {}
    ic = {}
    for ent, acct, cp, amt in x:
        if cp is None or cp == ent:
            cons[acct] = cons.get(acct, 0) + amt
        else:
            ic[(ent, acct, cp)] = ic.get((ent, acct, cp), 0) + amt
    keys = set()
    for (ent, acct, cp) in ic:
        for left, right in PAIRS:
            if acct == left:
                keys.add((ent, cp, left, right))
            elif acct == right:
                keys.add((cp, ent, left, right))
    mism = []
    for ent, cp, left, right in sorted(keys):
        a = ic.get((ent, left, cp), 0)
        b = ic.get((cp, right, ent), 0)
        m = min(a, b)
        if a != b:
            mism.append([ent, cp, left, a - b])
        if a - m:
            cons[left] = cons.get(left, 0) + a - m
        if b - m:
            cons[right] = cons.get(right, 0) + b - m
    return {'consolidated': {k: v for k, v in sorted(cons.items()) if v}, 'mismatches': mism}
def check(label, actual, expected):
    observations.append({"check": label, "actual": actual, "expected": expected, "passed": actual == expected})
fixtures = [[['regression: self counterparty', [['P', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S1', 400], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_sales', 'P', 400], ['P', 'ic_purchases', 'S1', 400], ['S2', 'ic_sales', 'S1', 250]], {'consolidated': {'ic_loan': 900, 'ic_purchases': 400, 'ic_sales': 650}, 'mismatches': [['S1', 'P', 'ic_sales', -400], ['S2', 'S1', 'ic_sales', 250]]}], ['control 1', [['S1', 'ic_purchases', 'S2', 1000], ['S1', 'ic_loan', 'P', 250], ['S2', 'sales', None, -100]], {'consolidated': {'ic_loan': 250, 'ic_purchases': 1000, 'sales': -100}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 2', [['S2', 'ic_receivable', 'P', 1000], ['P', 'ic_payable', 'S1', 400], ['S2', 'ic_purchases', 'S1', 100]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 100, 'ic_receivable': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S1', 'S2', 'ic_sales', -100], ['S2', 'P', 'ic_receivable', 1000]]}], ['control 3', [['S2', 'cash', None, 400], ['S2', 'ic_receivable', 'S1', 250], ['S1', 'ic_loan', 'P', 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_receivable': 250}, 'mismatches': [['S2', 'S1', 'ic_receivable', 250]]}], ['control 4', [['S1', 'ic_payable', 'S2', 1000], ['S1', 'ic_receivable', 'S2', 250], ['S2', 'ic_payable', 'S1', 1000], ['S2', 'ic_payable', 'P', 1000]], {'consolidated': {'ic_payable': 2750}, 'mismatches': [['P', 'S2', 'ic_receivable', -1000], ['S1', 'S2', 'ic_receivable', -750], ['S2', 'S1', 'ic_receivable', -1000]]}], ['control 5', [['P', 'ic_purchases', 'S1', 100], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'S2', 400], ['S1', 'cash', None, 100], ['S2', 'ic_purchases', 'S1', 1000], ['P', 'ic_purchases', 'S2', 100]], {'consolidated': {'cash': 100, 'ic_loan': 1400, 'ic_purchases': 1200}, 'mismatches': [['S1', 'P', 'ic_sales', -100], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'P', 'ic_sales', -100]]}], ['control 6', [['P', 'ic_payable', 'S1', 250], ['S1', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'P', 250], ['P', 'ic_payable', 'S2', 400], ['S2', 'cash', None, -400], ['S2', 'ic_purchases', 'P', 100]], {'consolidated': {'cash': -400, 'ic_payable': 900, 'ic_purchases': 100, 'ic_sales': 250}, 'mismatches': [['P', 'S2', 'ic_receivable', -250], ['P', 'S2', 'ic_sales', -100], ['S1', 'P', 'ic_receivable', -250], ['S1', 'P', 'ic_sales', 250], ['S2', 'P', 'ic_receivable', -400]]}]], [['regression: self counterparty', [['S1', 'ic_loan', 'S2', 400], ['P', 'ic_receivable', 'P', 400], ['S2', 'sales', None, -250], ['S2', 'ic_sales', 'P', 100], ['P', 'ic_receivable', 'S1', 1000]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 1400, 'ic_sales': 100, 'sales': -250}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['S2', 'P', 'ic_sales', 100]]}], ['control 1', [['S1', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000]], {'consolidated': {'ic_purchases': 1000, 'ic_sales': 800}, 'mismatches': [['S1', 'P', 'ic_sales', 400], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 2', [['S1', 'cash', None, 400], ['P', 'ic_receivable', 'S1', 100], ['S1', 'ic_sales', 'P', 100]], {'consolidated': {'cash': 400, 'ic_receivable': 100, 'ic_sales': 100}, 'mismatches': [['P', 'S1', 'ic_receivable', 100], ['S1', 'P', 'ic_sales', 100]]}], ['control 3', [['S1', 'ic_sales', 'S2', 250], ['P', 'ic_receivable', 'S1', 400], ['S1', 'ic_loan', 'S2', 400]], {'consolidated': {'ic_loan': 400, 'ic_receivable': 400, 'ic_sales': 250}, 'mismatches': [['P', 'S1', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 250]]}], ['control 4', [['S1', 'ic_receivable', None, 100], ['P', 'ic_loan', 'P', 1000], ['S2', 'ic_receivable', 'S1', 100], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_sales', 'P', 250]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 300, 'ic_receivable': 100, 'ic_sales': 250}, 'mismatches': [['S1', 'P', 'ic_sales', 250], ['S2', 'S1', 'ic_receivable', -300]]}], ['control 5', [['S1', 'ic_receivable', 'P', 100], ['P', 'ic_payable', 'S2', 100], ['P', 'ic_receivable', 'S1', 1000], ['P', 'ic_sales', 'S1', 100], ['P', 'ic_sales', 'S1', 1000], ['S2', 'ic_payable', 'P', 400], ['P', 'ic_purchases', 'S2', 1000]], {'consolidated': {'ic_payable': 500, 'ic_purchases': 1000, 'ic_receivable': 1100, 'ic_sales': 1100}, 'mismatches': [['P', 'S1', 'ic_receivable', 1000], ['P', 'S1', 'ic_sales', 1100], ['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 100], ['S2', 'P', 'ic_receivable', -100], ['S2', 'P', 'ic_sales', -1000]]}], ['control 6', [['P', 'cash', None, 400], ['S2', 'sales', None, -250], ['S1', 'ic_payable', 'S2', 400], ['S1', 'ic_receivable', 'S2', 100], ['P', 'sales', None, 250], ['P', 'ic_receivable', 'S2', 1000]], {'consolidated': {'cash': 400, 'ic_payable': 400, 'ic_receivable': 1100}, 'mismatches': [['P', 'S2', 'ic_receivable', 1000], ['S1', 'S2', 'ic_receivable', 100], ['S2', 'S1', 'ic_receivable', -400]]}]], [['regression: self counterparty', [['P', 'ic_receivable', None, -1000], ['P', 'ic_payable', 'P', 100], ['S1', 'ic_sales', 'S1', 400], ['S1', 'ic_payable', 'S2', 250], ['P', 'ic_sales', 'P', 100], ['S2', 'ic_receivable', 'S1', 400]], {'consolidated': {'ic_payable': 100, 'ic_receivable': -850, 'ic_sales': 500}, 'mismatches': [['S2', 'S1', 'ic_receivable', 150]]}], ['regression: self counterparty, partial-repair probe', [['P', 'ic_loan', 'P', 1000], ['S2', 'ic_sales', 'S2', 1000], ['S2', 'ic_payable', 'P', 400], ['S1', 'ic_loan', 'P', 1000], ['S1', 'ic_receivable', 'P', 1000], ['P', 'cash', None, 400], ['S2', 'ic_purchases', 'S1', 400], ['S1', 'ic_loan', 'S1', 100]], {'consolidated': {'cash': 400, 'ic_loan': 2100, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 1000, 'ic_sales': 1000}, 'mismatches': [['P', 'S2', 'ic_receivable', -400], ['S1', 'P', 'ic_receivable', 1000], ['S1', 'S2', 'ic_sales', -400]]}], ['control 1', [['S2', 'ic_payable', 'P', 250], ['S2', 'ic_loan', 'S2', 1000], ['P', 'ic_loan', 'P', 250], ['S1', 'ic_receivable', 'S2', 400], ['P', 'ic_receivable', 'S1', 250], ['S2', 'ic_sales', 'P', 1000]], {'consolidated': {'ic_loan': 1250, 'ic_payable': 250, 'ic_receivable': 650, 'ic_sales': 1000}, 'mismatches': [['P', 'S1', 'ic_receivable', 250], ['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 1000]]}], ['control 2', [['S1', 'ic_payable', 'P', 1000], ['S1', 'ic_loan', 'S1', 1000], ['P', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['P', 'S1', 'ic_sales', 400]]}], ['control 3', [['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_loan', 'S1', 100], ['P', 'ic_loan', 'S2', 250]], {'consolidated': {'ic_loan': 350, 'ic_purchases': 250}, 'mismatches': [['S1', 'S2', 'ic_sales', -250]]}], ['control 4', [['S1', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', 'S2', 400], ['S1', 'ic_payable', 'P', 400], ['P', 'ic_loan', 'P', 400], ['S1', 'cash', None, 100], ['S1', 'ic_receivable', None, 100], ['S1', 'ic_purchases', 'P', 400]], {'consolidated': {'cash': 100, 'ic_loan': 400, 'ic_payable': 400, 'ic_purchases': 400, 'ic_receivable': 500, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -400], ['P', 'S1', 'ic_sales', -400], ['P', 'S2', 'ic_receivable', 400], ['S1', 'S2', 'ic_sales', 400]]}], ['control 5', [['P', 'ic_receivable', None, -250], ['S1', 'sales', None, -250], ['S2', 'ic_sales', 'S1', 400]], {'consolidated': {'ic_receivable': -250, 'ic_sales': 400, 'sales': -250}, 'mismatches': [['S2', 'S1', 'ic_sales', 400]]}]], [['regression: self counterparty', [['P', 'ic_payable', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['S1', 'sales', None, -250], ['S1', 'ic_loan', 'S2', 100], ['P', 'ic_payable', 'S2', 400], ['P', 'ic_purchases', 'P', 100]], {'consolidated': {'ic_loan': 100, 'ic_payable': 800, 'ic_purchases': 500, 'sales': -250}, 'mismatches': [['S2', 'P', 'ic_receivable', -800]]}], ['control 1', [['S1', 'cash', None, 400], ['P', 'sales', None, -400], ['S1', 'ic_receivable', None, 1000]], {'consolidated': {'cash': 400, 'ic_receivable': 1000, 'sales': -400}, 'mismatches': []}], ['control 2', [['S2', 'ic_loan', 'S2', 250], ['S2', 'ic_loan', 'S1', 250], ['P', 'ic_loan', 'P', 400], ['S2', 'ic_purchases', 'P', 400], ['P', 'ic_payable', 'S1', 100], ['P', 'sales', None, -100]], {'consolidated': {'ic_loan': 900, 'ic_payable': 100, 'ic_purchases': 400, 'sales': -100}, 'mismatches': [['P', 'S2', 'ic_sales', -400], ['S1', 'P', 'ic_receivable', -100]]}], ['control 3', [['S2', 'ic_loan', 'S2', 100], ['S2', 'ic_purchases', 'S1', 250], ['S2', 'ic_payable', 'P', 100], ['P', 'ic_sales', 'S2', 400], ['P', 'ic_receivable', None, 250], ['S1', 'cash', None, -400], ['P', 'ic_purchases', 'S1', 1000], ['P', 'ic_payable', 'S2', 400]], {'consolidated': {'cash': -400, 'ic_loan': 100, 'ic_payable': 500, 'ic_purchases': 1250, 'ic_receivable': 250, 'ic_sales': 400}, 'mismatches': [['P', 'S2', 'ic_receivable', -100], ['P', 'S2', 'ic_sales', 400], ['S1', 'P', 'ic_sales', -1000], ['S1', 'S2', 'ic_sales', -250], ['S2', 'P', 'ic_receivable', -400]]}], ['control 4', [['S1', 'ic_payable', 'P', 1000], ['S2', 'ic_sales', 'S1', 400], ['S2', 'ic_purchases', 'S1', 1000], ['S1', 'ic_loan', 'P', 250]], {'consolidated': {'ic_loan': 250, 'ic_payable': 1000, 'ic_purchases': 1000, 'ic_sales': 400}, 'mismatches': [['P', 'S1', 'ic_receivable', -1000], ['S1', 'S2', 'ic_sales', -1000], ['S2', 'S1', 'ic_sales', 400]]}], ['control 5', [['S2', 'ic_payable', 'S1', 400], ['S2', 'ic_receivable', None, 400], ['S2', 'ic_loan', 'P', 1000], ['S2', 'ic_loan', 'S1', 400]], {'consolidated': {'ic_loan': 1400, 'ic_payable': 400, 'ic_receivable': 400}, 'mismatches': [['S1', 'S2', 'ic_receivable', -400]]}], ['control 6', [['P', 'ic_sales', 'S2', 100], ['P', 'ic_loan', 'S2', 100], ['S1', 'ic_receivable', None, 400]], {'consolidated': {'ic_loan': 100, 'ic_receivable': 400, 'ic_sales': 100}, 'mismatches': [['P', 'S2', 'ic_sales', 100]]}]], [['regression: self counterparty', [['S1', 'ic_purchases', 'S2', 400], ['S2', 'ic_purchases', 'S2', 400], ['P', 'ic_payable', 'S1', 400], ['S1', 'ic_sales', 'S1', 1000]], {'consolidated': {'ic_payable': 400, 'ic_purchases': 800, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_receivable', -400], ['S2', 'S1', 'ic_sales', -400]]}], ['regression: self counterparty, partial-repair probe', [['S2', 'ic_sales', 'P', 400], ['S2', 'ic_sales', 'P', 250], ['S2', 'ic_payable', 'S1', 400], ['S1', 'ic_purchases', 'P', 1000], ['P', 'ic_loan', 'S1', 100], ['S2', 'ic_receivable', 'P', 400], ['S2', 'cash', None, -1000]], {'consolidated': {'cash': -1000, 'ic_loan': 100, 'ic_payable': 400, 'ic_purchases': 1000, 'ic_receivable': 400, 'ic_sales': 650}, 'mismatches': [['P', 'S1', 'ic_sales', -1000], ['S1', 'S2', 'ic_receivable', -400], ['S2', 'P', 'ic_receivable', 400], ['S2', 'P', 'ic_sales', 650]]}], ['control 1', [['P', 'ic_purchases', 'S1', 100], ['S1', 'ic_sales', 'P', 400], ['S2', 'ic_loan', 'S2', 1000], ['P', 'cash', None, 100]], {'consolidated': {'cash': 100, 'ic_loan': 1000, 'ic_sales': 300}, 'mismatches': [['S1', 'P', 'ic_sales', 300]]}], ['control 2', [['P', 'ic_purchases', 'S2', 250], ['S1', 'ic_sales', 'P', 1000], ['S2', 'ic_payable', 'S1', 250], ['S2', 'ic_loan', 'S1', 1000]], {'consolidated': {'ic_loan': 1000, 'ic_payable': 250, 'ic_purchases': 250, 'ic_sales': 1000}, 'mismatches': [['S1', 'P', 'ic_sales', 1000], ['S1', 'S2', 'ic_receivable', -250], ['S2', 'P', 'ic_sales', -250]]}], ['control 3', [['S2', 'cash', None, 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_purchases', 'S2', 1000]], {'consolidated': {'cash': 250, 'ic_loan': 400, 'ic_purchases': 1000}, 'mismatches': [['S2', 'S1', 'ic_sales', -1000]]}], ['control 4', [['P', 'sales', None, 250], ['S2', 'ic_receivable', 'P', 100], ['S2', 'ic_payable', 'P', 250], ['S1', 'ic_loan', 'S2', 400], ['S1', 'ic_sales', 'S2', 250], ['S2', 'ic_receivable', None, 400], ['S2', 'ic_receivable', 'P', 400], ['P', 'cash', None, 400]], {'consolidated': {'cash': 400, 'ic_loan': 400, 'ic_payable': 250, 'ic_receivable': 900, 'ic_sales': 250, 'sales': 250}, 'mismatches': [['P', 'S2', 'ic_receivable', -250], ['S1', 'S2', 'ic_sales', 250], ['S2', 'P', 'ic_receivable', 500]]}], ['control 5', [['S2', 'ic_receivable', 'P', 400], ['P', 'ic_receivable', 'S2', 400], ['S2', 'ic_receivable', None, -1000]], {'consolidated': {'ic_receivable': -200}, 'mismatches': [['P', 'S2', 'ic_receivable', 400], ['S2', 'P', 'ic_receivable', 400]]}]]]
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:
Balances that name their own entity as counterparty are eliminated against themselves.

x = [[entity, account, counterparty or None, amount]]. Rows with no counterparty, the entity itself as counterparty, or an account outside the pairs (ic_receivable/ic_payable, ic_sales/ic_purchases) are consolidated as-is. For each (entity, counterparty, pair) the left balance of entity toward counterparty is matched against the right balance of counterparty toward entity; min(left, right) is eliminated from both, residuals are consolidated and a mismatch [entity, counterparty, left account, left - right] is reported when they differ. Return {'consolidated': nonzero account totals, 'mismatches': in sorted key order}.

split: open-access