python3 <<'EOF' from fractions import Fraction as F r=F('0.78') u=F('0.05') b=2-r xm=2/b q=F('-173/6000') prev='I' h=0 def cl(x,a,c): return a if xc else x) def RF(x): z=(1-x)/b return z if z>0 else F(0) def U(x): z=RF(x) return x-x*z-b/2*x*x def PH(x): z=RF(x) z=(1-z)/b return z if z>0 else F(0) C=[F(1)/(3-r)] cm=F(1)/(3-r) Uv=[U(c) for c in C] def CL(x): ux=U(x) ph=PH(x) for i,c in enumerate(C): if ux>=Uv[i] and (ph-x)*(c-x)>=0: return 1 mn=min(Uv) if ux>=mn and C[0]<=x<=C[-1]: return 2 if ux>=mn-F('0.03'): return 3 return 4 def AD(T,x): cc=CL(x) if T=='S': z=cc==1 if h==1: z=z and U(x)>U(cm) return z if T=='I': z=cc==1 or cc==2 if h==2: z=z and (PH(x)-x)*(cm-x)<=0 return z z=cc==1 or cc==2 or cc==3 if h==1: return False if h==2: z=z and U(x)>=U(cm) return z def SC(x,e): z=RF(x) cs=(x-(-z))**2/2 ps=U(x)-(-(z-z*x-b/2*z*z)) out=[cs-(-e),ps-(-e),cs-(-ps)-(-e)] return out[h] ps=[['0.806328','S','-0.008'],['0.100738','I','0.015'],['1.540594','S','-0.015'],['1.481586','Q','-0.015']] st=[] for i,pr in enumerate(ps,1): a=F(pr[0]) T=pr[1] e=F(pr[2]) adj=F(0) if prev=='S' and T=='Q': adj=u/4 if prev=='Q' and T=='S': adj=-u/3 x=cl(a-(-q)-(-adj),F(0),xm) st.append({'n':i,'a':a,'T':T,'e':e,'x':x,'c':CL(x),'ad':AD(T,x),'s':SC(x,e)}) rank=sorted(st,key=lambda z:(-z['s'],z['n'])) for z in st: print('S1',z['n'],z['T'],'x',z['x'],float(z['x']),'c',z['c'],'ad',z['ad'],'s',z['s'],float(z['s'])) print('RANK',[z['n'] for z in rank]) ads=[z for z in rank if z['ad']] print('ADS',[z['n'] for z in ads]) def S2(z): off={'S':u,'I':-u/3,'Q':-u/2}[z['T']] x2=cl(z['x']-(-off),F(0),xm) sc=SC(x2,z['e']) if z['T']==prev: sc=sc-(-F('0.01')) return {'n':z['n'],'T':z['T'],'x2':x2,'c2':CL(x2),'ad':AD(z['T'],x2),'s':sc} for z in ads[:2]: w=S2(z) print('S2',w['n'],w['T'],'x2',w['x2'],float(w['x2']),'c2',w['c2'],'ad',w['ad'],'s',w['s'],float(w['s'])) EOF