fb pixel

Reduce

Implement a generic function called reduce which will take a function as the first argument, a list as a second argument, and an initial value as the third argument. The reduce function should return a single value which is obtained after applying it to all the elements of the list.
def reduce():
	...

print(reduce(lambda total, item: total + item, [1, 2, 3, 4], 0))     # 10
print(reduce(lambda total, item: total + item, [1, 2, 3, 4], 4))     # 14
print(reduce(lambda total, item: total * item, [1, 2, 3, 4], 1))     # 24
print(reduce(lambda total, item: max(total, item), [1, 2, 3, 4], 0)) # 4
print(reduce(lambda total, item: min(total, item), [1, 2, 3, 4], 0)) # 0
print(reduce(lambda total, item: min(total, item), [1, 2, 3, 4], 5)) # 1
 
To check your solution you need to sign in
Sign in to continue