Discuss / Python / 作业

作业

Topic source

船长杰克

#1 Created at ... [Delete] [Delete and Lock User]
DIGITS = {'1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def char2num(ss):
    return DIGITS[ss]

def integer(intege):
    return reduce(lambda x, y: x * 10 + y, map(char2num, intege))

def decimal(dec):
    #也能实现但是不符合要求
    #num =0
    #for i,n in enumerate(dec):
    #    n = DIGITS[n]
    #    num+=  n /pow(10,i+1)
    #return num

    new = [ dec[len(dec)-1-i] for i,x in enumerate(dec)]
    return reduce(lambda x, y: x*0.1 + y, map(char2num, new))*0.1

return integer(s.split('.')[0]) + decimal(s.split('.')[1])

  • 1

Reply