"""Invented bounded teaching scenarios; no market, AI or inventory claims."""
from fractions import Fraction as F
def nonnegative(*values):
 for x in values:
  if isinstance(x,bool) or F(str(x))<0:raise ValueError('Require nonnegative numbers')
def operating_result(volume,price,variable,fixed):
 nonnegative(volume,price,variable,fixed)
 return F(str(volume))*(F(str(price))-F(str(variable)))-F(str(fixed))
def marketplace(count,value,commission,support,fixed):
 nonnegative(count,value,commission,support,fixed)
 if F(str(commission))>1:raise ValueError('Commission outside 0..1')
 gross=F(str(count))*F(str(value));revenue=gross*F(str(commission))
 return gross,revenue,revenue-F(str(count))*F(str(support))-F(str(fixed))
def ai_review(tp,fp,fn,tn,capacity,fp_cost,fn_cost):
 if any(type(x) is not int or x<0 for x in [tp,fp,fn,tn,capacity]):raise ValueError('Invalid counts')
 nonnegative(fp_cost,fn_cost);flags=tp+fp;total=tp+fp+fn+tn
 return dict(precision=F(tp,flags) if flags else None,recall=F(tp,tp+fn) if tp+fn else None,accuracy=F(tp+tn,total) if total else None,flags=flags,capacityExceeded=flags>capacity,loss=fp*F(str(fp_cost))+fn*F(str(fn_cost)))
def electricity(kwh,jobs,factor):
 nonnegative(kwh,factor)
 if type(jobs) is not int or jobs<=0:raise ValueError('Require positive completed jobs')
 return F(str(kwh))/jobs,F(str(kwh))*F(str(factor))
def match(requests,sellers):
 """Greedy input-order fixture; capacity counts one slot per request, not optimal."""
 if len({x['id'] for x in requests})!=len(requests) or len({x['id'] for x in sellers})!=len(sellers):raise ValueError('Duplicate identifier')
 remaining={s['id']:s['capacity'] for s in sellers}
 if any(type(v) is not int or v<0 for v in remaining.values()):raise ValueError('Invalid capacity')
 output=[]
 for request in requests:
  seller=next((s for s in sellers if request['category'] in s['categories'] and remaining[s['id']]>0),None)
  if seller:remaining[seller['id']]-=1
  output.append((request['id'],seller['id'] if seller else None))
 return output
if __name__=='__main__':
 print('Operating A/B:',operating_result(100,1000,600,20000),operating_result(80,1200,700,25000))
 print('Marketplace gross/revenue/result:',marketplace(100,500,'.1',20,2000))
 print('AI review:',ai_review(3,2,1,4,4,10,50))
 print('Electricity intensity/estimate:',electricity(1000,100,'.5'),electricity(900,120,'.5'),electricity(1500,200,'.5'))
 print('Matching:',match([dict(id='R1',category='print'),dict(id='R2',category='print'),dict(id='R3',category='design')],[dict(id='S1',categories=['print'],capacity=1)]))
