UVa 11291 Smeech Solution

def isfloat(n):
    try:
        float(n)
        return True
    except:
        return False
def res(s):
    if isfloat(s):
        return s
    else:
        s=s[1:len(s)-1]
        p,a,b=s.split()
        p=float(p)
        a=float(a)
        b=float(b)
        ans=p*(a+b)+(1-p)*(a-b)
        return str(ans)
def Main():
    while True:
        s=input()
        if s=='()':
            return 0
        ed=s.find(')')
        while ed!=-1:
            j=ed
            while s[j]!='(':
                j-=1
            if ed<len(s)-1:
                s=s[:j]+res(s[j:ed+1])+s[ed+1:]
            else:
                s=s[:j]+res(s[j:ed+1])
            ed=s.find(')')
        s=float(s)
        print("%1.2f"%s)
if __name__=="__main__":
    Main()

Comments