练习
Topic sourcedef str2float(s):
def f(x, y):
return x * 10 + y
def char2number(t):
return DIGITS[t]
if isinstance(s, str):
# 是否含有'.'
if s.rfind('.') == -1:
return reduce(f, map(char2number, s))
# 使用'.'分割字符串
sl = s.split('.')
integer_part = reduce(f, map(char2number, sl[0]))
decimal_part = reduce(f, map(char2number, sl[-1])) * pow(10, -len(sl[-1]))
return integer_part + decimal_part
raise TypeError('bad operand type')
- 1
skychi