Discuss / Python / 第三题自己写的比较笨的方法

第三题自己写的比较笨的方法

Topic source

秋雨mac

#1 Created at ... [Delete] [Delete and Lock User]
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from functools import reduce
def str2float(s):
    if not isinstance(s, str):
        raise TypeError('bad operand type. Here str is expected.')
    for i in range(len(s)):
        if s[i] == '.':
            break
    L1 = [c for i1, c in enumerate(list(s)) if i1 < i]
    L2 = [c for i2, c in enumerate(list(s)) if i2 > i]
    L2.reverse()
    L2.append(0)
    res = reduce(lambda x,y: x*10+y, map(int, L1))
    res = res + reduce(lambda x,y: x/10+y, map(int, L2))
    return res

  • 1

Reply