Discuss / Python / 练习题第三题

练习题第三题

Topic source

姬云烈

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

from functools import reduce

def char2num(s): return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'.':'.'}[s]

def findpoint(s): n = 0 for i in s: n += 1 if i == '.': break return n

def findlen(s): n = findpoint(s) lo = len(s) return lo - n

def char2float(s): a = list(map(char2num,s)) n = findpoint(s) len1 = findlen(s) c = reduce(lambda x,y : 10x + y,a[:(n-1)]) d = round(reduce(lambda x, y: 0.1 x + y, a[::-1][:len1])/10,len1) return c + d


  • 1

Reply