Q:

Write a Python Program to implement your own myreduce() function which works exactly like Python's built-in function reduce()

def myreduce(anyfunc, sequence):

 # Get first item in sequence and assign to result
  result = sequence[0]
 # iterate over remaining items in sequence and apply reduction function 
  for item in sequence[1:]:
   result = anyfunc(result, item)

  return result
1

New to Communities?

Join the community