Discuss / Python / 第三题

第三题

Topic source

def str2float(s):

      a=0

      def char2num(s):

            if s=='.':

                  return '.'

            else:

            return int(s)

      def exp(b):

            c=1

            while b>=1:

                  c=c*10

                  b=b-1

            return c

      def fn(x,y):

            nonlocal a

            if y=='.':

                  a=1

                  return x

            elif a==0:

                  return x*10+y

            else:

                  a=a+1

                  return x+y/exp(a-1)

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


  • 1

Reply