Discuss / Python / 第三题答案

第三题答案

Topic source

Othershe

#1 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-
from functools import reduce
import math
def str2float(s):
    def char2num(c):
        return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[c]
    def str2int(x, y):
        return 10*x + y
    index = len(s) - s.index('.') - 1
    ss = s.replace('.', '')
    return reduce(str2int, map(char2num, ss))/math.pow(10, index)

  • 1

Reply