第一题:
def normalize(name): return name.capitalize()
第二题:
from functools import reduce def prod(L): return reduce(lambda x, y: x * y, L)
第三题:
from functools import reduce def str2float(s): # 将字符串按小数点分割成整数部分和小数部分 parts = s.split('.') # 将整数部分和小数部分分别转换为数字 integer = reduce(lambda x, y: x * 10 + y, map(int, parts[0])) decimal = reduce(lambda x, y: x * 10 + y, map(int, parts[1])) / (10 ** len(parts[1])) # 返回整数部分和小数部分的和 return integer + decimal
都用到int()了为何不直接使用float()?
def str2float(s): return float(s)
强啊,我弄了那么长的一串,其实一个强转就行,牛逼啊,我怎么没想到呢
Sign in to make a reply
Super-String
第一题:
第二题:
第三题: