Discuss / Python / 练习1,2,3

练习1,2,3

Topic source

Gssdragon

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

#练习1 def normalize(name): return name.title()

#练习2 def prod(L): def f(x,y): return x * y return reduce(f,L)

#练习3 DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}

def str2int(s):
    def fn(x, y):
        return x * 10 + y
    def char2num(s):
        return DIGITS[s]
    return reduce(fn, map(char2num, s))

for i,value in enumerate(s):
    if value == ".":
        break
s = s[:i] + s[i+1:]
result = str2int(s) 
return result * pow(10,-i)

请多多指教

dG2-

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

练习3的最后一行代码有问题。


  • 1

Reply