Discuss / Python / 第三题

第三题

Topic source

runlovehappy

#1 Created at ... [Delete] [Delete and Lock User]
from functools import reduce


def str2float(s):
    s2List = list(s)
    def getPositionOfDot(L):
        for i in range(len(L)):
            if s2List[i] == '.':
                return i
    position = getPositionOfDot(s2List)
    s2List[position] = '0'
    def char2num(L):
        return {'0':0, '1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9}[L]
    sIntList = list(map(char2num, s2List))
    integer = sIntList[ : position]
    decimal = sIntList[position : ]
    decimal2reverse = decimal[::-1]
    integer2int = reduce(lambda x, y: x * 10 + y, integer)
    decimal2float = reduce(lambda x, y: x * 0.1 + y, decimal2reverse)
    return integer2int + decimal2float

  • 1

Reply