Lawfare Group → Knowledge Institute → Lawfare Academy

Reproduce the worked examples

Copy the complete code below into a UTF-8 file named verify.py and run python verify.py with Python 3. It uses only the standard library and performs exact or rational synthetic calculations. It does not access a network or learner records. Assertions fail if the expected synthetic results change.

These checks verify the stated arithmetic and query behavior, not independent academic acceptance or production migration safety.

from fractions import Fraction as F
from math import isclose
import json
checks=[]
def check(name,condition):
 assert condition,name
 checks.append(name)
# New complete ledger uses thousands of currency units; debit balances positive.
opening=dict(Cash=40,AR=30,Allowance=-2,Inventory=20,Equipment=60,AccumulatedDepreciation=-12,AP=-18,WagesPayable=-2,Debt=-40,Capital=-50,RetainedEarnings=-26)
ledger=opening.copy();journals=[]
def post(name,entries,kind=None):
 check('balanced journal: '+name,sum(entries.values())==0)
 for account,amount in entries.items():ledger[account]=ledger.get(account,0)+amount
 journals.append(dict(name=name,entries=entries,cashFlowCategory=kind))
check('opening trial balance',sum(opening.values())==0)
post('earned credit sales',dict(AR=120,Revenue=-120))
post('collect receivables',dict(Cash=110,AR=-110),'operating')
post('inventory bought on account',dict(Inventory=70,AP=-70))
post('pay suppliers',dict(AP=64,Cash=-64),'operating')
post('inventory cost consumed',dict(COGS=65,Inventory=-65))
post('customer advance',dict(Cash=12,CustomerAdvances=-12),'operating')
post('perform part of advance service',dict(CustomerAdvances=5,Revenue=-5))
post('wages incurred and paid',dict(WagesExpense=30,Cash=-28,WagesPayable=-2),'operating')
post('other operating payment',dict(OtherExpense=10,Cash=-10),'operating')
post('interest payment',dict(InterestExpense=4,Cash=-4),'operating')
post('equipment purchase at period end',dict(Equipment=20,Cash=-20),'investing')
post('new borrowing',dict(Cash=15,Debt=-15),'financing')
post('principal repayment',dict(Debt=6,Cash=-6),'financing')
post('owner distribution',dict(Dividends=8,Cash=-8),'financing')
post('period depreciation',dict(DepreciationExpense=12,AccumulatedDepreciation=-12))
post('allowance adjustment',dict(BadDebtExpense=2,Allowance=-2))
post('warranty provision',dict(WarrantyExpense=3,WarrantyPayable=-3))
post('warranty payment',dict(WarrantyPayable=1,Cash=-1),'operating')
expenseAccounts=['COGS','WagesExpense','OtherExpense','InterestExpense','DepreciationExpense','BadDebtExpense','WarrantyExpense']
revenue=-ledger['Revenue'];expenses=sum(ledger[a] for a in expenseAccounts);profit=revenue-expenses
check('income statement',revenue==125 and expenses==126 and profit==-1)
cashflows={kind:sum(j['entries'].get('Cash',0) for j in journals if j['cashFlowCategory']==kind) for kind in ['operating','investing','financing']}
check('direct cash flow categories',cashflows==dict(operating=15,investing=-20,financing=1))
check('gross receivable rollforward',30+120-110==40 and ledger['AR']==40)
check('inventory movement',20+70-65==25 and ledger['Inventory']==25)
check('supplier payable movement',18+70-64==24 and ledger['AP']==-24)
check('wages liability movement',2+30-28==4 and ledger['WagesPayable']==-4)
check('advance rollforward',12-5==7 and ledger['CustomerAdvances']==-7)
check('warranty rollforward',0+3-1==2 and ledger['WarrantyPayable']==-2)
check('allowance rollforward',2+2==4 and ledger['Allowance']==-4)
indirect=profit+12+2-(40-30)-(25-20)+(24-18)+(4-2)+7+2
netARindirect=profit+12-((40-4)-(30-2))-(25-20)+(24-18)+(4-2)+7+2
check('gross-account indirect bridge',indirect==15)
check('net-account alternative no double count',netARindirect==15)
check('cash reconciliation',opening['Cash']+sum(cashflows.values())==ledger['Cash']==36)
preclose=ledger.copy()
post('close revenue',dict(Revenue=revenue,RetainedEarnings=-revenue))
post('close expenses',{**{a:-ledger[a] for a in expenseAccounts},'RetainedEarnings':expenses})
post('close distributions',dict(Dividends=-8,RetainedEarnings=8))
assets=ledger['Cash']+ledger['AR']+ledger['Allowance']+ledger['Inventory']+ledger['Equipment']+ledger['AccumulatedDepreciation']
liabilities=-sum(ledger[a] for a in ['AP','WagesPayable','Debt','CustomerAdvances','WarrantyPayable']);equity=-ledger['Capital']-ledger['RetainedEarnings']
check('closing statements and equation',assets==153 and liabilities==86 and equity==67 and assets==liabilities+equity)
check('equity rollforward',76+profit-8==67 and -ledger['RetainedEarnings']==17)
check('adjustment scenario',profit-3-4==-8 and assets-3-4==146 and equity-3-4==60)
check('valuation adjustment no current cash',ledger['Cash']==36)
check('stressed gross-account cash bridge',-8+12+5+4-10-5+6+2+7+2==15)
check('stressed net-account cash bridge',-8+12-(33-28)-(21-20)+6+2+7+2==15)
check('collection delay preserves assets not liquidity',26+46+25+56==153 and 15-10==5)
check('unperformed advance changes profit/liability not cash',125-5-126==-6 and 86+5+67-5==153)
check('no distribution changes cash and equity not profit',36+8==44 and 67+8==75 and 153+8==86+75)
currentLiabilities=24+4+7+2+12;baseCA=36+36+25;stressCA=36+33+21
check('current ratio stated contract definition',currentLiabilities==49 and F(baseCA,49)>=F(19,10) and F(stressCA,49)<F(19,10))
check('changed contract excludes advances',F(97,42)>F(19,10) and F(90,42)>F(19,10))
check('unperformed advance current ratio',F(97,54)<F(19,10))
check('gross receivable days',isclose(365*((30+40)/2)/120,106.45833333333333))
check('inventory turnover',F(65,1)/F(20+25,2)==F(26,9))
check('old-asset revised-life comparison',36/3==12 and 36/6==6 and 20/5==4)
check('next-period depreciation total',36/3+20/5==16 and 36/6+20/5==10)
check('par bond changed yield',F(4,1)/F(27,25)+F(54,1)/F(27,25)**2==50)
check('defined cash after equipment and earnings coverage',15-20==-5 and (profit+4)/4==.75)
bond=F(4,1)/F(11,10)+F(54,1)/F(11,10)**2;interest1=bond/10;car1=bond+interest1-4;interest2=car1/10;car2=car1+interest2-4
check('effective interest original bond',isclose(float(bond),48.264462809917354) and car2==50)
check('bond coupon differs expense',interest1>4 and interest2>4 and car1==F(540,11))
# Reconstruct preserved companion cases in ordinary currency units.
check('retained receipt classification',35000+20000+10000+15000==80000 and 80000-15000==65000)
check('retained allowance re-estimation',10000-500==9500 and 9500-8000==1500)
check('retained corrected and recurring profit',70000-6000==64000 and 64000-15000==49000)
check('retained operating cash bridge',64000+12000-15000-10000+4000+6000==61000)
check('retained working-capital cash use',12000+8000-5000==15000)
check('retained net working assets',100000-8000==92000 and 40000-6000==34000 and 5000+6000==11000)
check('retained equipment debt example',24000/48==500 and 24000-500==23500 and 18000-1000==17000)
check('retained financing bridge',25000+12000-18000-2000-5000==12000 and 30000+12000-5000==37000 and 18000-3000==15000)
check('retained changed financing characterization',30000+12000==42000 and 3000+2000+5000==10000)
check('retained normalized salary/rent',90000-(60000-20000)-(18000-10000)==42000)
check('retained passive-buyer normalization',90000-(25000-10000)-50000==25000)
check('retained owner opportunity-cost change',90000-(25000-10000)-40000==35000)
check('retained sustainability bridge',70000-15000-10000-40000==5000)
check('retained cash after draw',120000-20000-8000+6000-18000-70000-15000==-5000)
check('retained delayed collection downside',120000-20000-8000+6000-18000-70000-15000-10000==-15000)
check('retained inventory cash release change',-5000+8000==3000)
check('retained cumulative corrected/recurring',50000-8000==42000 and 42000-12000==30000)
check('retained cumulative operating cash',42000+10000-12000-20000-5000+7000+8000==30000)
if __name__=='__main__':print(json.dumps(dict(passed=True,checks=checks,units='New complete ledger in thousands; retained case fixtures in ordinary currency units',opening=opening,journals=journals,precloseTrial=preclose,closingTrial=ledger,calculated=dict(revenue=revenue,expenses=expenses,profit=profit,assets=assets,liabilities=liabilities,equity=equity,cash=36,cashflows=cashflows,baseCurrentRatio=baseCA/currentLiabilities,stressedCurrentRatio=stressCA/currentLiabilities,bondIssueValue=float(bond),bondFirstInterest=float(interest1),bondFirstCarrying=float(car1),bondSecondInterest=float(interest2))),indent=2))

Related chapters