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.
"""Original synthetic planning cases; Python 3 standard library. No network or learner records."""
from fractions import Fraction as F
import math,json
results={}
def profit(q):
assert 0<=q<=200
return 80*q-(8000 if q<=120 else 10000)
assert [profit(q) for q in [100,120,121,125,175,200]]==[0,1600,-320,0,4000,6000]
rate=F(21200-13600,160-80);intercept=13600-rate*80
assert rate==95 and intercept==6000 and intercept+rate*120==17400
results['cost_regimes']={'break_even':[100,125],'target_4000':175,'step_profit':[1600,-320],'high_low':[95,6000]}
mixes=[(48*a+40*b,a,b) for a in range(301) for b in range(501) if 2*a+b<=1000]
best=max(mixes);assert best==(32000,250,500)
assert 300*48+400*40==30400
assert 100*(82-60-2)-200==1800 and 1800-50*48==-600
assert 60+2+F(200+2400,100)==88 and F(80,1-F(1,4))==F(320,3)
results['pricing_and_capacity']={'best_mix_A_B':[250,500],'contribution':32000,'profit_after_fixed':14000,'idle_order_gain':1800,'constrained_order_gain':-600,'constrained_break_even_price':88}
rates=[F(6000,30),F(4000,40)];assigned=[20*rates[0]+10*rates[1],5*rates[0]+20*rates[1]]
assert assigned==[5000,3000]
unused=(30-25)*rates[0]+(40-30)*rates[1];assert unused==2000
assert (4000-assigned[0])+(4000-assigned[1])-unused==-2000
actual_usage=[20*F(6000,25)+10*F(4000,30),5*F(6000,25)+20*F(4000,30)]
assert sum(actual_usage)==10000 and -4000+3000==-1000
variable_profit=80*(60-30)-2000-500;absorption_profit=80*60-80*(30+F(2000,100))-500
assert variable_profit==-100 and absorption_profit==300 and absorption_profit-variable_profit==20*20
results['resource_cost_and_absorption']={'assigned_support':list(map(int,assigned)),'unused_capacity':int(unused),'company_profit':-2000,'drop_A_delta':-1000,'variable_and_absorption_profit':[-100,300]}
def budget(current_collection=F(3,5)):
sales=[100,120,140];future=[120,140,160];cash=F(2000);debt=F(0);inv=F(20);ar=F(4000);ap=F(3000);ppe=F(10000);equity=F(14200);rows=[]
for i,(units,next_units) in enumerate(zip(sales,future)):
revenue=units*100;endinv=F(1,5)*next_units;purchased=units+endinv-inv;purchases=purchased*60
collections=ar+current_collection*revenue;payments=ap+F(1,2)*purchases;capex=5000 if i==0 else 0
pre=cash+collections-payments-3000-capex;borrow=max(F(0),1000-pre);repay=min(debt,max(F(0),pre-1000));cash=pre+borrow-repay;debt+=borrow-repay
ar=(1-current_collection)*revenue;ap=F(1,2)*purchases;inv=endinv;ppe+=capex-500;income=revenue-units*60-3500;equity+=income
assets=cash+ar+inv*60+ppe;liabilities_equity=ap+debt+equity;assert assets==liabilities_equity
rows.append(dict(units=units,purchase_units=int(purchased),purchases=int(purchases),collections=int(collections),payments=int(payments),profit=int(income),pre_financing_cash=int(pre),borrow=int(borrow),repay=int(repay),cash=int(cash),debt=int(debt),receivables=int(ar),payables=int(ap),inventory=int(inv*60),net_equipment=int(ppe),equity=int(equity),assets=int(assets)))
return rows
base=budget();assert [r['purchase_units'] for r in base]==[104,124,144]
assert [r['collections'] for r in base]==[10000,11200,13200]
assert [r['payments'] for r in base]==[6120,6840,8040]
assert [r['profit'] for r in base]==[500,1300,2100]
assert [r['borrow'] for r in base]==[3120,0,0] and [r['repay'] for r in base]==[0,1360,1760]
assert base[-1]['assets']==22420 and base[-1]['equity']==18100 and base[-1]['cash']==1400 and base[-1]['debt']==0
assert 3900+1500-1600-720+1320-5000==-600
stress=budget(F(3,10));assert [r['collections'] for r in stress]==[7000,10600,12600]
assert stress[0]['borrow']==6120 and [r['repay'] for r in stress]==[0,760,1560]
assert stress[-1]['assets']==26220 and stress[-1]['debt']==3800 and stress[-1]['cash']==1000 and stress[-1]['receivables']==9800
assert max(r['debt'] for r in base)<=4000<max(r['debt'] for r in stress)
results['linked_budget']={'base':base,'slow_collection_stress':stress,'facility_ceiling':4000,'stress_initial_gap':2120}
static=1000*(20-5)-4000;flex=1100*(20-5)-4000;actual=1100*(F(39,2)-F(27,5))-4300
assert [static,flex,actual]==[11000,12500,11210] and static+1500-550-440-300==actual
material_price=220*(F(24,5)-5);material_quantity=(220-200)*5
assert material_price==-44 and material_quantity==100 and material_price+material_quantity==220*F(24,5)-1000==56
assert 160*(21-20)+(160-150)*20==160*21-150*20==360
assert 700-160*4+(160-150)*4==700-150*4==100
assert 1100-1000+(1000-150*5)==1100-150*5==350
results['variance_bridges']={'static_flexible_actual':[11000,12500,11210],'materials_price_quantity_total':[-44,100,56],'labor_rate_efficiency_total':[160,200,360],'variable_overhead':[60,40,100],'fixed_overhead':[100,250,350]}
prob=[F(1,4),F(1,2),F(1,4)];volumes=[80,120,180]
expected_q=sum(p*q for p,q in zip(prob,volumes));expected_profit=sum(p*profit(q) for p,q in zip(prob,volumes))
assert expected_q==125 and expected_profit==1500 and profit(expected_q)==0
npv=-10000+F(4000,1)/F(11,10)+F(4000,1)/F(11,10)**2+F(5000,1)/F(11,10)**3
assert round(float(npv),2)==698.72
assert F(2000,10000)==F(1,5) and F(3000,20000)==F(3,20)
assert 2000-F(8,100)*10000==1200 and 1000-F(8,100)*10000==200
results['uncertainty_and_capital']={'expected_volume':125,'expected_profit':1500,'profit_at_expected_volume':0,'loss_probability':0.25,'npv':float(npv),'combined_roi':0.15,'combined_residual_income':1400,'year_end_payback_year':3}
def january(units,price,cash_expense):
revenue=units*price;purchases=(units+24-20)*60;income=revenue-units*60-cash_expense-500
receipts=4000+F(3,5)*revenue;payments=3000+F(1,2)*purchases;pre=2000+receipts-payments-cash_expense-5000;borrow=max(F(0),1000-pre)
cash=pre+borrow;ar=F(2,5)*revenue;ap=F(1,2)*purchases;assets=cash+ar+1440+14500;equity=14200+income
assert assets==ap+borrow+equity
return dict(revenue=int(revenue),purchases=int(purchases),profit=int(income),receipts=int(receipts),payments=int(payments),pre_financing_cash=int(pre),borrow=int(borrow),cash=int(cash),receivables=int(ar),payables=int(ap),assets=int(assets),equity=int(equity))
actual_jan=january(110,98,3100);proposal=january(130,95,3000)
assert actual_jan['profit']==580 and actual_jan['borrow']==3052 and actual_jan['assets']==21252 and actual_jan['equity']==14780
assert 500+400-220-100==580 and 580+500-312-240+420-5000+3052==-1000
assert proposal['profit']==1050 and proposal['borrow']==2610 and proposal['receipts']==11410 and proposal['payments']==7020
assert math.ceil(F(4000,35))==115 and january(110,95,3000)['profit']==350
results['cumulative_dossier']={'actual_january':actual_jan,'conditional_discount_proposal':proposal,'discount_volume_threshold':115,'facility_3000_actual_gap':52}
print(json.dumps({'passed':True,'cases':results},indent=2))
Related chapters
- Build a cost model that survives a capacity change
- Price the opportunity and the scarce resource
- Trace customer support while keeping unused capacity visible
- Reconcile the operating plan to cash and the balance sheet
- Explain the variance before assigning responsibility
- Plan for the distribution, financing limits and long-run consequences
- Budgeting clinic: defend the plan, explain the result and fund the next decision