Discuss / Python / 自己用了一些笨方法写出来的

自己用了一些笨方法写出来的

Topic source

孟宪chi

#1 Created at ... [Delete] [Delete and Lock User]

    dist = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9 }

    def strtoint(s):

        def fn(x, y):

            return x * 10 +y

        def char2num(S):

            return dist[S]

        return reduce(fn, map(char2num, s))

    def findd(s):

        n = 0

        for i in s:

            if i == '.':

                return n

            else:

                n = n+1

        return n

    n= findd(s) #找到小数点的位置  

    print(n)

    s = s.replace('.', '')#把字符串中的小数点去掉

    l=len(s)#计算字符串的现在的长度

    s = strtoint(s)#把字符串编程整数

    return s/(10**n)#返回整数乘上10的负的次方

思路:

统计数字的个数

找到‘.’的位置

删除点这个字符

按照之前的代码把剩余的字符串进行对strtoint

然后除以点的位置对应的10的个数


  • 1

Reply