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 MBA682 cases. Standard library only; no records or network."""
from fractions import Fraction as F
from math import ceil,isclose
import json,itertools
checks=[]
def eq(name,value,target):
 assert isclose(float(value),float(target),rel_tol=1e-10,abs_tol=1e-8),(name,value,target)
 checks.append(dict(case=name,result=float(value),expected=float(target)))
def menu(ph,pb,basic_high=70,version_cost=1000,outside=(0,0)):
 total=-5000-version_cost;choices=[]
 for count,pv,bv,o in [(100,120,basic_high,outside[0]),(200,70,50,outside[1])]:
  offers=[(pv-ph,'premium',ph-25),(bv-pb,'basic',pb-21),(o,'outside',0)]
  best=max(offers)
  assert sum(x[0]==best[0] for x in offers)==1,'Ambiguous tie needs a rule'
  total+=count*best[2];choices.append(best[1])
 return total,choices
v,ch=menu(80,40);eq('self-selected menu profit',v,3300);assert ch==['premium','basic']
eq('premium-only price80',100*55-5000,500)
eq('premium-only price40',300*15-5000,-500)
eq('high group defects to basic',menu(80,40,basic_high=90)[0],-300)
eq('costly version',menu(80,40,version_cost=4500)[0],-200)
assert menu(80,40,outside=(70,50))[1]==['outside','outside']
eq('premium49 contribution',100*(49-25),2400)
eq('continuation avoidable cost',2400-2600,-200)
flow=lambda b,s:min(b,5*s)
assert [flow(100,20),flow(100,40),flow(200,20),flow(200,40)]==[100,100,100,200]
eq('costly platform expansion',100*8-1200,-400)
eq('cheap platform expansion',100*8-500,300)
eq('accepted costly expansion',60*8-1200,-720)
eq('accepted cheap expansion',60*8-500,-20)
eq('ranking A',(20-2)*30-200,340)
eq('ranking B gross',(30-8)*30-400,260)
eq('ranking B retained',30*30-400,500)
eq('B advertising tie',22*30-340,320)
eq('B extra support tie',22*25-340,210)
eq('B extra support actual',22*25-400,150)
def da(apps,positions):
 next_i={a:0 for a in apps};held={};free=list(apps);trace=[]
 while free:
  a=free.pop(0)
  if next_i[a]>=len(apps[a]):continue
  p=apps[a][next_i[a]];next_i[a]+=1;old=held.get(p)
  if a not in positions[p]:free.append(a);continue
  if old is None or positions[p].index(a)<positions[p].index(old):
   held[p]=a
   if old is not None:free.append(old)
   trace.append([a,p,'held',old])
  else:free.append(a);trace.append([a,p,'rejected',old])
 return {a:p for p,a in held.items()},trace
def blockers(assignment,apps,positions):
 assert len(set(assignment.values()))==len(assignment)
 inv={p:a for a,p in assignment.items()}
 for a,p in assignment.items():assert p in apps[a] and a in positions[p]
 result=[]
 for a,rank in apps.items():
  current=assignment.get(a);better=rank[:rank.index(current)] if current is not None else rank
  for p in better:
   if a not in positions[p]:continue
   holder=inv.get(p)
   if holder is None or positions[p].index(a)<positions[p].index(holder):result.append([a,p])
 return result
apps={'A':['X','Y','Z'],'B':['X','Z','Y'],'C':['Y','X','Z']}
positions={'X':['B','A','C'],'Y':['A','C','B'],'Z':['C','B','A']}
a,trace=da(apps,positions);assert a=={'B':'X','A':'Y','C':'Z'} and blockers(a,apps,positions)==[]
assert ['B','X'] in blockers({'A':'X','B':'Z','C':'Y'},apps,positions)
changed={**positions,'X':['A','B','C']};a2,_=da(apps,changed)
assert a2=={'A':'X','C':'Y','B':'Z'} and not blockers(a2,apps,changed)
incomplete={**apps,'C':['Y','X']};a3,_=da(incomplete,positions)
assert a3=={'B':'X','A':'Y'} and not blockers(a3,incomplete,positions)
# Independent enumeration confirms every returned allocation is among stable acceptable allocations.
for ap,po,result in [(apps,positions,a),(apps,changed,a2),(incomplete,positions,a3)]:
 stable=[]
 for choices in itertools.product([None,'X','Y','Z'],repeat=3):
  candidate={x:y for x,y in zip(ap,choices) if y is not None}
  if len(set(candidate.values()))!=len(candidate):continue
  if any(p not in ap[x] or x not in po[p] for x,p in candidate.items()):continue
  if not blockers(candidate,ap,po):stable.append(candidate)
 assert result in stable
checks.append(dict(case='deferred acceptance and independently enumerated blocking pairs',passed=True,trace=trace))
eq('migration transition',20*100+10*400,6000)
discount=F(10,11)+F(100,121)
eq('migration NPV',5000*discount-6000,2677.685950413223)
eq('migration added charge',5000*discount-9000,-322.314049586777)
eq('migration benefit4000',4000*discount-6000,942.1487603305785)
eq('migration benefit4000 added charge',4000*discount-9000,-2057.8512396694216)
eq('migration benefits delayed',F(500000,121)+F(5000000,1331)-6000,1888.805409466567)
eq('assistant saved hours',300*(12-4-3-1)/60,20)
eq('assistant pre-severe',20*30-200-100-50,250)
eq('assistant lower risk',250-F(5,1000)*20000,150)
eq('assistant higher risk',250-F(2,100)*20000,-150)
eq('assistant threshold',F(250,20000),.0125)
eq('assistant longer review',300*(12-4-7-1)/60*30-350,-350)
# Isolated teaching control: authority checked before mutation, duplicate key after approval.
# This in-memory illustration is not a production security assurance or permission change.
ledger={}
def execute(key,approved):
 if not approved:return 'denied'
 if key in ledger:return 'duplicate'
 ledger[key]='synthetic';return 'accepted'
assert execute('R003',False)=='denied' and ledger=={}
assert execute('R003',True)=='accepted' and execute('R003',True)=='duplicate' and len(ledger)==1
checks.append(dict(case='isolated authority and duplicate illustration',passed=True))
# Reconstruct retained original native and companion examples without altering them.
eq('retained guide unit margin',60-8-22,30);eq('retained guide400 contribution',400*30,12000)
eq('retained guide break-even',ceil(18000/30),600);eq('retained higher acquisition',ceil(18000/17),1059)
eq('retained platform first period',50*30-2000-500,-1000)
eq('retained platform second period',50*30-500,1000)
eq('retained ranking A',40*20,800);eq('retained ranking B gross',35*20,700);eq('retained ranking B net',45*20,900)
eq('retained bid allocation',100+90,190);eq('retained urgency allocation',100+70,170)
eq('retained urgency total',9+8,17)
eq('retained draft benefit',10*40-150-3*40-80,50)
eq('retained draft higher correction',10*40-150-3*40-150,-20)
eq('retained entry margin',50-10-25,15)
eq('retained entry deficit',1000*15-40000,-25000)
eq('retained entry recovery',ceil(40000/15),2667);eq('retained lower CAC recovery',ceil(40000/30),1334)
eq('retained congested platform',10000-8000-4000,-2000)
eq('retained improved matching',10000-4000-4000-1000,1000)
ap2={'A':['X','Y'],'B':['X','Y']};po2={'X':['B','A'],'Y':['A','B']}
r,_=da(ap2,po2);assert r=={'B':'X','A':'Y'} and not blockers(r,ap2,po2)
r,_=da(ap2,{**po2,'X':['A','B']});assert r=={'A':'X','B':'Y'}
print(json.dumps(dict(passed=True,checks=checks,limits='Original synthetic arithmetic/mechanism verification; input validity, independent subject review, learner testing and real authority are not established.')))

Related chapters