Discuss / Python / 第三题

第三题

Topic source

商郡马

#1 Created at ... [Delete] [Delete and Lock User]
def str2float(s):
    def Mult(x,y):
        return x*10+y
    def char2num(s):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
    def str2int(s):
        return reduce(Mult, map(char2num, s))
    L = s.split('.')
    r1 = str2int(L[0])
    s1 = '%d' % r1
    #处理没有小数部分的情况
    if len(L) > 1:
        r2 = str2int(L[1])
        s2 = '%d' % r2
        return s1+'.'+s2
    else:
        s2 = ''
        return s1

  • 1

Reply