Discuss / Python / 只贴第三题吧

只贴第三题吧

Topic source

LesLieM樂

#1 Created at ... [Delete] [Delete and Lock User]
#-*- coding: utf-8 -*-

from functools import reduce

def str2float(s):
    i = 0
    while True:
        if s[i] == '.':
            break
        i += 1
    rest = len(s) - (i + 1)
    ns = s[:i] + s[i+1:]

    def f1(x, y):
        return x * 10 + y

    def ch2num(ch):
        d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
        return d[ch]
    ints = reduce(f1, map(ch2num, ns))
    return ints / (10 ** rest)

print('str2float(\'123.456\') = %s'%str2float('123.456'))

技术有待提高,智商有待提高,代码美观度有待提高


  • 1

Reply