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.

"""Python 3 reproducible arithmetic checks. Synthetic assumptions; no network or records."""
from fractions import Fraction as F
import json

def accrual():
 cash=20000-6000+5000+3000-2500
 receivable=9000-5000;prepaid=6000-1000;liabilities=3000+600
 income=9000-1000-2500-600;equity=20000+income
 assets=cash+receivable+prepaid
 ocf=5000+3000-6000-2500
 bridge=income-receivable-prepaid+3000+600
 assert (cash,assets,liabilities,equity,income,ocf)==(19500,28500,3600,24900,4900,-500)
 assert assets==liabilities+equity and bridge==ocf
 # Settlement clears balances; it does not repeat recognition of the old event.
 cash_after=cash+4000-600;ar_after=receivable-4000;utilities_after=600-600
 assert cash_after==22900 and ar_after==0 and utilities_after==0
 return dict(cash=cash,assets=assets,liabilities=liabilities,equity=equity,income=income,operatingCashFlow=ocf)

def inventory():
 available=100*10+100*14;units=200;sold=150;revenue=sold*20
 fifo_cost=100*10+50*14;fifo_end=50*14
 average=F(available,units);avg_cost=sold*average;avg_end=50*average
 assert fifo_cost+fifo_end==available==avg_cost+avg_end
 assert (fifo_cost,fifo_end,revenue-fifo_cost)==(1700,700,1300)
 assert (avg_cost,avg_end,revenue-avg_cost)==(1800,600,1200)
 # Moving average: sell 60 before purchasing the second layer, then sell 90.
 first_cost=60*10;new_average=F(40*10+100*14,140)
 moving_cost=first_cost+90*new_average;moving_end=50*new_average
 assert moving_cost==F(12300,7) and moving_end==F(4500,7)
 assert moving_cost+moving_end==available and new_average!=average
 fifo_shortage=5*14;avg_shortage=5*average
 assert (fifo_shortage,avg_shortage,fifo_end-fifo_shortage,avg_end-avg_shortage)==(70,60,630,540)
 assert revenue-1400==1600
 return dict(fifoCost=fifo_cost,fifoEnding=fifo_end,periodicAverageCost=str(avg_cost),movingAverageCost=str(moving_cost),movingAverageEnding=str(moving_end),fifoCountLoss=fifo_shortage,averageCountLoss=str(avg_shortage))

def cvp():
 p,v,fixed=80,50,9000;cm=p-v
 be=F(fixed,cm);target=F(fixed+3000,cm)
 assert (be,target,350*cm-fixed)==(300,400,1500)
 def profit(q):
  assert 0<=q<=500
  return cm*q-(9000 if q<=400 else 12000)
 assert [profit(q) for q in [400,401,450,500]]==[3000,30,1500,3000]
 assert F(9000,80-55)==360 and 400*(80-55)-9000==1000
 # A fixed mix is a bundle, not the unweighted mean of contribution margins.
 bundle_a=2*30+60;bundle_b=30+2*60
 assert (F(fixed,bundle_a),F(fixed,bundle_b))==(75,60)
 assert (75*3,60*3)==(225,180)
 return dict(breakEvenUnits=str(be),targetUnits=str(target),profitsAt400401450500=[profit(q) for q in [400,401,450,500]],mixAUnits=225,mixBUnits=180)

def relevant_cost():
 contribution=200*(75-65);setup=500;displaced=100*40
 constrained=contribution-setup-displaced;idle=contribution-setup
 overtime=idle-100*8;rework=overtime-900;partial=100*(75-65)-setup
 assert (constrained,idle,overtime,rework,partial)==(-2500,1500,700,-200,500)
 best=max(((30*a+24*b,a,b) for a in range(31) for b in range(61) if 3*a+b<=120))
 assert best==(2040,20,60)
 assert 30*30+24*30==1620
 return dict(constrainedOrder=constrained,idleCapacityOrder=idle,overtimeOrder=overtime,afterRework=rework,partialOrder=partial,bestMix=dict(contribution=best[0],a=best[1],b=best[2]))

def elasticity():
 ep=F(80-100,1)/F(80+100,2)/(F(12-10,1)/F(12+10,2))
 before=10*100;after=12*80
 profit_before=before-6*100-200;profit_after=after-6*80-200
 assert ep==F(-11,9) and (before,after,profit_before,profit_after)==(1000,960,200,280)
 def profit(q):return (20-F(q,10))*q-6*q-200
 assert profit(70)==290 and profit(69)==profit(71)==F(2899,10)
 assert max((profit(q),q) for q in range(101))==(290,70)
 assert max((profit(q),q) for q in range(61))==(280,60)
 return dict(midpointElasticity=str(ep),revenueBefore=before,revenueAfter=after,profitBefore=profit_before,profitAfter=profit_after,unconstrainedQuantity=70,capacityQuantity=60)

def tax_wedge():
 def solve(t):
  ps=F(140-2*t,5);pb=ps+t;q=-20+3*ps
  assert q==120-2*pb
  cs=F(1,2)*(60-pb)*q;producer=F(1,2)*(ps-F(20,3))*q;receipts=t*q
  assert pb*q==ps*q+receipts
  return dict(ps=ps,pb=pb,q=q,cs=cs,producer=producer,receipts=receipts,total=cs+producer+receipts)
 zero=solve(0);ten=solve(10);twenty=solve(20)
 assert (zero['ps'],zero['q'])==(28,64)
 assert (ten['ps'],ten['pb'],ten['q'],ten['receipts'])==(24,34,52,520)
 assert (ten['cs'],ten['producer'],ten['total'])==(676,F(1352,3),F(4940,3))
 assert zero['total']-ten['total']==60==F(1,2)*10*(64-52)
 assert (twenty['ps'],twenty['pb'],twenty['q'],twenty['receipts'])==(20,40,40,800)
 assert zero['total']-twenty['total']==240
 assert (ten['pb']-zero['pb'],zero['ps']-ten['ps'])==(6,4)
 return {name:{k:str(v) for k,v in values.items()} for name,values in [('untaxed',zero),('tenUnitWedge',ten),('twentyUnitWedge',twenty)]}

def main():
 result={name:fn() for name,fn in [('accrual',accrual),('inventory',inventory),('cvp',cvp),('relevantCost',relevant_cost),('elasticity',elasticity),('taxWedge',tax_wedge)]}
 print(json.dumps({'passed':True,'syntheticCases':len(result),'results':result},indent=2))
 return result
if __name__=='__main__':main()

Related chapters