Reduce

Implement a generic function called reduce that accepts three arguments: a function (as the first argument), a list (as the second argument), and an initial value (as the third argument).
The purpose of the reduce function is to iteratively apply the given function to the elements of the list, while maintaining a running total initialized with the third argument, and finally return a single resulting value.
The reduce function should perform the following steps:
  1. Accept all the arguments.
  1. Iterate over the list, applying the provided function to the running total and the current list element at each step.
  1. After processing all elements in the list, return the final value of the running total.
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
 

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue