Discuss / Python / 第二题

第二题

Topic source

丿疯子乄

#1 Created at ... [Delete] [Delete and Lock User]
# 
# 把字符串转换成浮点数
# 

def str2float(S):
    def ch2int(ch):
        return {'0' : 0, '1' : 1, '2' : 2, '3' : 3, '4' : 4, '5' : 5, '6' : 6, '7' : 7, '8' : 8, '9' : 9}[ch]
    def str2int(s):
        return reduce(lambda x, y : x * 10 + y, map(ch2int, s))
    ns = S.split('.')
    return str2int(ns[0]) + 0.1 ** len(ns[1]) * str2int(ns[1])

print(str2float('7432.00467'))

丿疯子乄

#2 Created at ... [Delete] [Delete and Lock User]

错了,第三题 ……


  • 1

Reply