Discuss / Python / str2float 作业

str2float 作业

Topic source

daydayup233

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

def str2float2(s): DIGITS = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9} def char2num(s1): return DIGITS[s1] L = s.split('.') intNum = reduce(lambda x,y:x * 10 + y,map(char2num,L[0])) floatNum = reduce(lambda x,y:x * 10 + y,map(char2num,L[1]))

return intNum + float(floatNum)/(10**len(L[1]))

daydayup233

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

#首字母大写,参数校验省略 from functools import reduce def normalize(name) return name.capitalize()

L1 = ['adam', 'LISA', 'barT'] L2 = list(map(normalize, L1)) print(L2)

#求积,参数校验省略 def prod(L): return reduce(lambda x,y:x * y,L)

print('3 5 7 * 9 =', prod([3, 5, 7, 9])) if prod([3, 5, 7, 9]) == 945: print('测试成功!') else: print('测试失败!')

daydayup233

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

复制粘贴有毒啊,很多符号都需要加上\,不加符号不见了…伤不起


  • 1

Reply