Discuss / Python / 作业三想破脑壳,参考各位大神终于通过了

作业三想破脑壳,参考各位大神终于通过了

Topic source

SherryMaoMAO

#1 Created at ... [Delete] [Delete and Lock User]
# -*- ciding: utf-8 -*-
from functools import reduce
def str2float(s):
	s = s.split(".", 1)
	def fn(x, y):
		return x * 10 + y
	return reduce(fn, map(int, s[0])) + reduce(fn, map(int, s[1]))/(10 ** len(s[1]))
print('str2float(\'123.456\') =', str2float('123.456'))
if abs(str2float('123.456') - 123.456) < 0.00001:
	print('测试成功')
else:
	print('测试失败')

  • 1

Reply