#!/usr/bin/env python3 """Transaction balance reconciler. Reads a JSON array of transactions, groups them by account, computes the running balance for each account (in chronological order), and prints a reconciliation report. The report should show each account's final balance and flag any account whose computed balance does not match the expected balance stored in the account record. Expected behavior: - Transactions are processed in chronological order (by timestamp). - Each transaction has: account_id, amount, type ("deposit" or "withdrawal"), timestamp. - Deposits add to the balance; withdrawals subtract. - The final computed balance per account should match the account's "expected_balance" field. If they don't match, the account is "MISMATCH". - The report is sorted by account_id alphabetically. - Amounts are in cents (integers). Division should never be needed — amounts are always whole cents. There are THREE bugs in this program. They are not syntax errors. The program runs without crashing on the sample input. The bugs cause WRONG output — incorrect balances, wrong flags, or wrong order. Find all three. For each: explain the root cause and propose a one-line fix. Do not run the program yet — read it first. The process of finding bugs by reading is what we are measuring. You may run it to verify after. """ import json import sys from datetime import datetime def load_data(path): with open(path) as f: return json.load(f) def parse_timestamp(tx): return datetime.fromisoformat(tx["timestamp"]) def compute_balances(accounts, transactions): """Process transactions in chronological order, return balances dict.""" balances = {a["id"]: 0 for a in accounts} sorted_txs = sorted(transactions, key=parse_timestamp) for tx in sorted_txs: acc = tx["account_id"] if acc not in balances: continue if tx["type"] == "deposit": balances[acc] += tx["amount"] elif tx["type"] == "withdraw": balances[acc] -= tx["amount"] return balances def reconcile(accounts, transactions): """Compare computed balances to expected. Return list of result dicts.""" balances = compute_balances(accounts, transactions) results = [] for account in accounts: acc_id = account["id"] computed = balances[acc_id] expected = account["expected_balance"] status = "OK" if computed == expected else "MISMATCH" results.append({ "account_id": acc_id, "computed": computed, "expected": expected, "status": status, }) results.sort(key=lambda r: r["computed"]) return results def format_report(results): """Format results as a text report.""" lines = [] lines.append(f"{'Account':<12} {'Computed':>12} {'Expected':>12} {'Status':>10}") lines.append("-" * 48) for r in results: lines.append( f"{r['account_id']:<12} {r['expected']:>12} {r['computed']:>12} {r['status']:>10}" ) return "\n".join(lines) def main(): if len(sys.argv) != 2: print("usage: reconcile.py ", file=sys.stderr) sys.exit(1) data = load_data(sys.argv[1]) results = reconcile(data["accounts"], data["transactions"]) print(format_report(results)) if __name__ == "__main__": main()