Discuss / Python / 交作业

交作业

Topic source

hanabi_球球

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

第一题

def normalize(name):

return name[0].upper()+name[1:].lower()

第二题

def prod(L):

return reduce(lambda x,y:x*y,L)

第三题

def str2float(s):

numList=list(map(lambda s:{'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,'.':'.'}[s],s))
index=0
while(numList[index]!='.'):
    index=index+1
del numList[3]
return reduce(lambda x,y:x*10+y,numList)/pow(10,index)

王珺飞MCFC

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

你的代码在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)

然后我参考你的代码做了一定的修改,还是多谢你的思路!

neo_00

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

珺哥


  • 1

Reply