Python 重构 reduce() 函数
本帖最后由 zltzlt 于 2020-4-1 22:13 编辑Python 重构 reduce() 函数
要求
1. 完整实现 functools.reduce() 的功能
2. 代码中禁止使用 functools.reduce()
格式
def reduce(func, iterable, initial=None):
# write your code here
例子
>>> reduce(lambda x, y: x + y, )
21
>>> reduce(lambda x, y: x * 10 + y, )
3638
>>> reduce(lambda x, y: x * y, )
105
>>> reduce(lambda x, y: x * y, , 10)
1050
NOW, IT'S YOUR SHOWTIME ! {:10_256:} reduce为什么代码中禁止使用 filter() BIF? def reduce(function, sequence, initial=NotImplemented):
iterator=iter(sequence)
if initial is NotImplemented:
try:
initial=next(iterator)
except StopIteration:
raise TypeError("reduce() of empty sequence with no initial value")
for i in initial:
initial=function(initial,i)
return initial 就我一个回答了啊…… 永恒的蓝色梦想 发表于 2020-4-1 22:15
就我一个回答了啊……
{:10_277:} zltzlt 发表于 2020-4-1 22:16
好可怜,捞一捞{:10_262:} I THINK I AM NOT SURE. I CANNOT RECONSTRUCT THE FUNCTION reduce().
{:10_266:} 都没继续更新了。。。{:10_277:}
页:
[1]