交作业
Topic source你的代码在del numList[3]这一句针对小数点的不同位置没有可扩展性。然后最后一句里pow(10,index)放置index是有逻辑错误的,只不过恰好测试案例123.456整数部分和小数部分长度相同。
def str2float(s):
def char2num(c):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9, '.':'.'}[c]
L = list(map(char2num, s))
count = index = 0
for x in L:
if x == '.':
index = count
count += 1
L.pop(index)
def fn(x, y):
return x * 10 + y
return reduce(fn, L) / pow(10, count - 1 - index)
然后我参考你的代码做了一定的修改,还是多谢你的思路!
- 1
hanabi_球球
第一题
def normalize(name):
第二题
def prod(L):
第三题
def str2float(s):