Discuss / Python / 第3题

第3题

Topic source
from functools import reduce

def f(x,y):
  r = int(x)*10 + int(y)
  return r

def f1(L):
  return reduce(f,L)

def f2(L):
  return f1(L)/(10**(len(L)))

def str2float(s):
    index = s.find('.')
    return f1(s[:index]) + f2(s[index + 1:])

t = str2float('3456.17894')
print(type(t),t)

这次是对的,上次没有考虑到位数不足的情况

def f(x,y):
  r = int(x)*10 + int(y)
  return r

def f1(L):
  return reduce(f,L)

def f2(L):
  return f1(L)/(10**(len(L)))

def str2float(s):
    index = s.find('.')
    s = s + '00'
    if(index == 1 or index == 0):
        s = '00' + s
        print('s:',s)
        index = index + 2
    return f1(s[:index]) + f2(s[index + 1:])

t = str2float('2.2')
print(type(t),t)

  • 1

Reply