Discuss / Python / 作业-第三题

作业-第三题

Topic source

DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def str2float(s):

    A='.'

    if A in s:

        c=s.find(A)

        r=str.split(s,'.')

        r1=r[0]

        r2=r[1]

        def str2num(m):

            return DIGITS[m]

        r3=reduce(lambda x, y: x*10+y,map(str2num,r1))

        r4=reduce(lambda x, y: x*10+y,map(str2num,r2))

        r5=r4/(10**len(r2))

        r6=r3+r5

        return r6

    else:

        def str2num(m):

            return DIGITS[m]

        r=reduce(lambda x, y: x * 10 + y,map(str2num,s))

        return print(r)


  • 1

Reply