Discuss / Python / 第三题

第三题

Topic source

金钟铉

#1 Created at ... [Delete] [Delete and Lock User]
#看评论前自己写的
from functools import reduce
d={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'.':'.'}
def str2float(s):	
    for i in range(len(s)):
        if s[i:i+1]=='.':
            return reduce(lambda x,y:x*10+y,map(lambda x:d[x],s[:i]+s[i+1:]))/10**(len(s)-1-i)
#看评论后写的第一个
from functools import reduce
d = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'}
def str2float(s):
    s1,s2 = s.split('.')
    return reduce(lambda x,y:x*10+y,map(lambda x:d[x],s1+s2))/pow(10,len(s2))
#看评论后写的第二个
from functools import reduce
d={'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9,'.':'.'}
def str2float(s):	
    return reduce(lambda x,y:x*10+y,map(lambda x:d[x],s[:s.find('.')]+s[s.find('.')+1:]))/pow(10,(len(s)-1-s.find('.')))


  • 1

Reply