Exercise: implement the reduce function
my_reduce(function, a, b, c, ...)
- 'function' is expected to be a function that receives two arguments and returns a result.
- If only the function is given, return None.
- If only one value is given, return that value.
- Take the first two values, run the function on them. Then take the result and the next value and run the function on them. etc. When no more values are left, return the last result.
examples/advanced/my_reduce_skeleton.py
# print(my_reduce()) # TypeError: my_reduce() takes at least 1 argument (0 given) print(my_reduce(lambda x,y: x+y)) # None print(my_reduce(lambda x,y: x+y, 3)) # 3 print(my_reduce(lambda x,y: x+y, -1, 4, -2)) # 1 print(my_reduce(lambda x,y: x*y, -1, 4, -2)) # 8