Discuss / Python / 第三题

第三题

Topic source

安徽省石台县大演中心学校

#1 Created at ... [Delete] [Delete and Lock User]
def str2float(s):
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 tointeger(x, y):
return x * 10 + y
#小数部分
def tofloat(x, y):
return x/10 + y
if s is not None:
return reduce(tointeger, map(char2num, s[0:s.find('.')])) + reduce(tofloat, map(char2num, s[s.find('.')+1:][::-1]))/10

冰如火_199231

#2 Created at ... [Delete] [Delete and Lock User]
# -*- coding: utf-8 -*-

from functools import reduce

def str2float(s):

#确定小数点位置
def fm(s):
for i in range(len(s)):
if s[i]=='.':
return i
else:
i=+i

#字符串转为整型
def f(x,y):
return x*10+y
def char3num(s):
return {'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
#去除小数点“.”
M=fm(s)
L=list(s)
del L[M]
#将L转化字符串s
s=('').join(L)
#计算小数点位数
m= len(s)-M
print(m)
#将s转化为整型数123456
l=reduce(f,map(char3num,s))
print(l)
#转化为小数123.456
n=0
while n<m:
n=n+1
l=l / 10
print(l)
return l
print('str2float(\'123.456\') =', str2float('123.456'))

• 1