"""Deterministic ledger tasks. Each task_id -> (ledger csv, question, expected answer).""" import random KINDS = ["balance", "category_total", "duplicate", "largest_expense", "net_flow", "unmatched"] # The server accepts any "-" with n >= 1 (unbounded task pool); the # submission lists 50 of them (the arena maximum). TASK_IDS = [f"{k}-{i}" for k in KINDS for i in range(1, 9)] + ["balance-9", "category_total-9"] ACCOUNTS = ["rent", "salary", "food", "travel", "tools", "tax"] def build(task_id: str): kind, idx = task_id.rsplit("-", 1) if kind not in KINDS: raise KeyError(task_id) rng = random.Random(f"ledger-{task_id}") n = 8 + 2 * (int(idx) % 12) rows = [] for i in range(1, n + 1): acct = rng.choice(ACCOUNTS) amt = round(rng.uniform(5, 900), 2) if acct == "salary": amt = round(rng.uniform(1000, 3000), 2) else: amt = -amt rows.append([f"T{i:02d}", acct, amt]) if kind == "balance": opening = round(rng.uniform(500, 5000), 2) q = f"Opening balance is {opening:.2f}. What is the closing balance after all entries? Answer with a number, 2 decimals." ans = f"{opening + sum(r[2] for r in rows):.2f}" elif kind == "category_total": acct = rng.choice([r[1] for r in rows]) q = f"What is the total (signed sum) of all entries in account '{acct}'? Answer with a number, 2 decimals." ans = f"{sum(r[2] for r in rows if r[1] == acct):.2f}" elif kind == "duplicate": j = rng.randrange(2, n) dup = list(rows[rng.randrange(0, j)]) dup[0] = f"T{j+1:02d}x" rows.insert(j, dup) q = "Exactly one entry duplicates an earlier entry (same account and amount) with a later id. Which entry id is the duplicate?" ans = dup[0] elif kind == "largest_expense": q = "Which entry id is the largest expense (most negative amount)?" ans = min(rows, key=lambda r: r[2])[0] elif kind == "net_flow": q = "What is the sum of all positive entries minus the absolute sum of all negative entries (i.e. net flow)? Answer with a number, 2 decimals." ans = f"{sum(r[2] for r in rows):.2f}" else: # unmatched k = rng.randrange(0, n) rows[k][1] = "unknown" q = "Exactly one entry has account 'unknown'. Which entry id is it?" ans = rows[k][0] csv = "id,account,amount\n" + "\n".join(f"{r[0]},{r[1]},{r[2]:.2f}" for r in rows) return csv, q, ans def score(expected: str, given: str) -> float: g = given.strip().strip("'\"").replace(",", "") try: return 1.0 if abs(float(g) - float(expected)) < 0.005 else 0.0 except ValueError: return 1.0 if g.lower() == expected.lower() else 0.0