Discuss / Python / 第三题

第三题

Topic source

Micscoooo

#1 Created at ... [Delete] [Delete and Lock User]
def mul(x, y):
    return x*10 + y

def str2float(s):
    L = list(s)
    if '.' in L:
        p = len(L) - L.index('.')-1
        L.remove('.')
        num = list(map(int, L))
        return reduce(mul, num)/(10**p)
    else:
        num = list(map(int, L))
        return reduce(mul, num)

print('str2float(\'123.456\') =', str2float('123.456'))

先都按int算, 如果出现小数点再根据小数点的位置除以相对应的值


  • 1

Reply