Discuss / Python / 习题1-3

习题1-3

Topic source

钱老板boss

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

习题一:

def normalize(name): name = name[0].upper() + name[1:].lower() return name 习题二: def prod(L): def multiply(x,y): return x*y

return reduce(multiply,L)

习题三 digits = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def char2Float(s): return digits[s]

def multiply(x,y):
    return x * 10 + y

temp= s.split('.')    #将浮点型数字分为两部分,整数前为字符串s1='123',小数点后为字符串s2='456'
s1 = temp[0]
s2 = temp[1]
s = s1 + s2    #将两个字符串合并成一个字符串'123456'
length = len(s2)



result1 = reduce(multiply,(map(char2Float,s)))
result = result1 / (10**length) #所得结果除以10的length次方
return result

  • 1

Reply