def char2num(s): return {'.':'.', '0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9'}[s] L = list(map(char2num,'12345.6'))
L1 = [] n = 0 while n < len(L): if L[n] != ".": L1.append(L[n]) else: break n = n + 1
L2 = [] if n == len(L): L2 = [] else: m = n + 1 while m < len(L): L2.append(L[m]) m = m + 1 L2rev = L2[-1::-1]
from functools import reduce
def L1tofloat(a): def fn(x1, y1): return x1 * 10 + y1 def char1tonum(a): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[a] return reduce(fn, map(char1tonum, a))
def L2tofloat(b): def fm(x2, y2): return x2 / 10 + y2 def char2tonum(b): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[b] return reduce(fm, map(char2tonum, b))/10
if n == len(L): resultL = (L1tofloat(L1)) else: resultL = (L1tofloat(L1) + L2tofloat(L2rev))
print (resultL)
print ('isinstance = float:', isinstance(resultL, float))
我擦,怎么我的注释变成那么大的字了。。。
Sign in to make a reply
上辈子是hr
step 1: 得到列表化的数字串
def char2num(s): return {'.':'.', '0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6', '7': '7', '8': '8', '9': '9'}[s] L = list(map(char2num,'12345.6'))
step 2: 分离小数点前后的数字为L1和L2,其中L2采用倒序
L1 = [] n = 0 while n < len(L): if L[n] != ".": L1.append(L[n]) else: break n = n + 1
step 2.1: 如果n=len(L),说明一直没发现小数点,说明是整数,L2直接为空
L2 = [] if n == len(L): L2 = [] else: m = n + 1 while m < len(L): L2.append(L[m]) m = m + 1 L2rev = L2[-1::-1]
step 3: 这次是来真的了!那个……题目是什么来着?
from functools import reduce
def L1tofloat(a): def fn(x1, y1): return x1 * 10 + y1 def char1tonum(a): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[a] return reduce(fn, map(char1tonum, a))
def L2tofloat(b): def fm(x2, y2): return x2 / 10 + y2 def char2tonum(b): return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[b] return reduce(fm, map(char2tonum, b))/10
step 3.1: 同2.1的处理
if n == len(L): resultL = (L1tofloat(L1)) else: resultL = (L1tofloat(L1) + L2tofloat(L2rev))
print (resultL)
step 3.2: 增加验证是否为浮点数
print ('isinstance = float:', isinstance(resultL, float))