交作业
Topic source"""
:param s: 带入含有小数点的数字字符串
:return: 返回相同值的浮点数
"""
DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
if '.' in s:
# 获取小数点在字符串的下标
dot_pos = s.index('.')
# 最后获取的整数需要整除10的n次方
float_offset = len(s) - dot_pos - 1
# 去除字符串中的小数点
s = s.replace('.', '')
def char2num(s):
"""
:param s: 带入含有小数点的数字字符串
:return: 将字符串中的每一个元素转换成数字
"""
return DIGITS[s]
def fun(x, y):
return x * 10 + y
return reduce(fun, map(char2num, s))/(10 ** float_offset)
else:
print('Please call str2int()')
- 1
用户7686226831