Discuss / Python / 第三题

第三题

Topic source

豆芽太羊

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

#--coding:utf-8-- from functools import reduce def char2float(c): dict={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'.':'.'} return dict[c] def fun1(x,y): return x10+y def fun2(x,y): return 0.1x+y def str2float(s): L=list(map(char2float,s)) i=0 while i<=len(L): if L[i]=='.': break else: i=i+1 fore=reduce(fun1,L[0:i]) back=reduce(fun2,L[len(L):i:-1]) return fore+0.1*back print('123.4567->',str2float('123.4567'),sep='')

豆芽太羊

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

while循环部分可以用i=s.find('.')来代替


  • 1

Reply