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.
"""Independent Harbor fictional data/model laboratory; not an Excel engine."""
from fractions import Fraction as F
import json,math,copy
checks={}
def check(n,a,b):
assert a==b,(n,a,b)
checks[n]=str(a)
def close(n,a,b):
assert math.isclose(float(a),float(b),rel_tol=1e-9,abs_tol=1e-8),(n,a,b)
checks[n]=float(a)
raw=[dict(id='R001',region='North',product='A',units=60,price=20,cost=12),dict(id='R002',region='South',product='A',units=40,price=20,cost=12),dict(id='R003',region='North',product='B',units=400,price=10,cost=9),dict(id='R004',region='South',product='B',units=500,price=10,cost=9)]
def summary(rows):
assert len({r['id'] for r in rows})==len(rows),'Duplicate key'
assert all(r['cost'] is not None for r in rows),'Missing cost'
revenue=sum(r['units']*r['price'] for r in rows);cost=sum(r['units']*r['cost'] for r in rows)
return revenue,cost,revenue-cost
check('record count',len(raw),4);check('unique key count',len({r['id'] for r in raw}),4)
check('total units',sum(r['units'] for r in raw),1000)
check('original total',summary(raw),(11000,9300,1700))
a=summary([r for r in raw if r['product']=='A']);b=summary([r for r in raw if r['product']=='B'])
check('product A total',a,(2000,1200,800));check('product B total',b,(9000,8100,900))
check('A margin',F(a[2],a[0]),F(2,5));check('B margin',F(b[2],b[0]),F(1,10))
check('business revenue-weighted margin',F(1700,11000),F(17,110))
check('different equal-product average',(F(2,5)+F(1,10))/2,F(1,4))
check('A markup',F(a[2],a[1]),F(2,3))
n=summary([r for r in raw if r['region']=='North']);s=summary([r for r in raw if r['region']=='South'])
check('North total',n,(5200,4320,880));check('South total',s,(5800,4980,820))
check('North margin',F(n[2],n[0]),F(11,65))
check('group revenue reconciliation',n[0]+s[0],11000);check('group gross reconciliation',n[2]+s[2],1700)
changed=raw+[dict(id='R005',region='North',product='A',units=20,price=20,cost=12)]
check('added period all-row total',summary(changed),(11400,9540,1860))
check('original period remains scoped',summary(changed[:4]),(11000,9300,1700))
check('all-period margin',F(1860,11400),F(31,190))
cn=summary([r for r in changed if r['region']=='North']);check('changed North total',cn,(5600,4560,1040))
check('changed North margin',F(1040,5600),F(13,70))
bad=copy.deepcopy(raw);bad[2]['cost']=None
try:summary(bad)
except AssertionError as e:check('missing cost exposed',str(e),'Missing cost')
else:raise AssertionError('Missing cost silently accepted')
try:summary(raw+[raw[0]])
except AssertionError as e:check('duplicate key exposed',str(e),'Duplicate key')
else:raise AssertionError('Duplicate silently accepted')
check('text identifier preservation','007','007');assert '007'!=str(7)
check('price20 with anchored5pct',20*(1-F(5,100)),19)
check('price10 with same anchored rate',10*(1-F(5,100)),F(19,2))
check('mixed grid outputs',[[q*p for p in [5,6]] for q in [10,20]],[[50,60],[100,120]])
def margin(g,r):return None if r==0 else F(g,r)
check('zero denominator remains undefined',margin(0,0),None)
jan=3000+2000-4500;feb=jan+6000-3500
check('January ending cash',jan,500);check('February ending cash',feb,3000)
check('two-period roll-forward',3000+8000-8000,3000)
check('January shortfall to policy floor',1000-jan,500)
def payment(p,r,n):return F(p,n) if r==0 else p*r/(1-(1+r)**(-n))
pv=12000;rate=F(1,100);periods=12;pay=payment(pv,rate,periods)
close('end-month payment magnitude',pay,1066.1854641401)
check('zero-interest payment',payment(pv,0,12),1000)
check('first-period interest',pv*rate,120)
balance=F(pv);interest_total=F(0);principal_total=F(0)
for _ in range(periods):
interest=balance*rate;principal=pay-interest;balance-=principal;interest_total+=interest;principal_total+=principal
check('exact end balance',balance,0);check('principal reconciliation',principal_total,pv)
check('payment-interest reconciliation',pay*periods-pv,interest_total)
close('present value of end payments',sum(pay/(1+rate)**t for t in range(1,13)),pv)
due=pay/(1+rate)
close('present value of beginning payments',sum(due/(1+rate)**t for t in range(12)),pv)
profit=lambda p,v,q,f:(p-v)*q-f
check('base scenario result',profit(20,12,1000,5000),3000)
targetprice=12+F(5000+5000,1000);check('price target',targetprice,22)
check('target price substitution',profit(targetprice,12,1000,5000),5000)
qtarget=F(5000+5000,20-12);check('quantity target',qtarget,1250)
assert qtarget>1100
check('capacity-limited base result',profit(20,12,1100,5000),3800)
check('base quantity for3000 target',F(3000+5000,20-12),1000)
check('changed-cost quantity for3000 target',F(3000+5000,20-15),1600)
check('changed-cost capacity-limited result',profit(20,15,1100,5000),500)
print(json.dumps(dict(passed=True,checks=checks,limits='Fictional independent data and model checks only. This is not an Excel formula engine, a UI/interoperability test, an actual cash forecast or a learner competency test. Real inputs, legal rates, financial suitability and software-version behavior are not established. Independent subject review and observed learner testing remain pending; no learner records are read or written.'),indent=2))
Related chapters
- Build a table whose records and meanings can be checked
- Write formulas that retain their meaning when copied
- Summarize without changing the question or denominator
- Model cash timing and payments with explicit period and sign conventions
- Solve a model without mistaking its answer for a feasible decision
- Communicate a result without hiding its denominator or uncertainty
- Build an auditable workbook and explain its decision limits