Develop myReduce Function¶
Develop a function by name myReduce
which takes a collection and a function as arguments. The myReduce
function should do the following:
- Iterate through elements
- Perform aggregation operation using the argument passed. Argument should have necessary arithmetic logic.
- Return the aggregated result.
In [1]:
l = [1, 4, 6, 2, 5]
In [2]:
l[1:]
Out[2]:
[4, 6, 2, 5]
In [3]:
def myReduce(c, f):
t = c[0]
for e in c[1:]:
t = f(t, e)
return t
In [4]:
myReduce(l, lambda t, e: t + e)
Out[4]:
18
In [5]:
myReduce(l, lambda t, e: t * e)
Out[5]:
240
In [6]:
min(7, 5)
Out[6]:
5
In [7]:
myReduce(l, lambda t, e: min(t, e))
Out[7]:
1
In [8]:
myReduce(l, lambda t, e: max(t, e))
Out[8]:
6