4.2.3 Reduce
my_reduce
takes in a two argument function combiner
and a non-empty sequence seq
and combines the elements in seq
into one value using combiner
.
def my_reduce(combiner, seq):
"""Combines elements in seq using combiner.
seq will have at least one element.
>>> my_reduce(lambda x, y: x + y, [1, 2, 3, 4]) # 1 + 2 + 3 + 4
10
>>> my_reduce(lambda x, y: x * y, [1, 2, 3, 4]) # 1 * 2 * 3 * 4
24
>>> my_reduce(lambda x, y: x * y, [4])
4
>>> my_reduce(lambda x, y: x + 2 * y, [1, 2, 3]) # (1 + 2 * 2) + 2 * 3
11
>>> # check that your code consists of nothing but an expression (this docstring)
>>> # and a return statement
>>> import inspect, ast
>>> [type(x).__name__ for x in ast.parse(inspect.getsource(my_reduce)).body[0].body]
['Expr', 'Return']
"""
"*** YOUR CODE HERE ***"