Skip to content Skip to sidebar Skip to footer

How Do I Write A Recursive Function That Return The Minimum Element In An Array?

write a recursive function that return the minimum element in an array where C is the array and s is the size. this is my code. c = [2,5,6,4,3] def min (c, s):

Solution 1:

Apparently the computer doesn't know what s stands for in the line print min (c,s)

You need to tell a computer what you want s variable to be. I propose you to use 0 instead of s in function call, that way you will start looking for min number from 0.

That being said there are other issues with the code but this will fix your error and you will be able to move forward with your task.

Solution 2:

First of all, I'd caution the use of min as a function name. Min is a python built in so use as your own function name may cause unwanted results in your code.

If I'm understanding the code and your question correctly, it seems as if you are trying to get the minimum of the list length compared to the minimum integer in the list.

c = [2,5,6,4,3]
'''The function takes the name of the array as the parameter. In this case c'''defminimum(array): 
'''This function compares the length of array to the smallest integer in the array'''
    length = 0for i in c:
        length += 1'''In this case length is 5'''
    smallest = min(c) 
    '''smallest is 2 in this case'''printmin(smallest,length)

However if all you want is to get the minimum value of an array, just do:

defminval(array):
     printmin(array)

Solution 3:

A recursive function divides the task into smaller parts that can be solved by the function itself. The function gets repeatedly called with these smaller and smaller tasks until the tasks become so easy, that they can be computed easily.

Thus the smallest element of a list (when computed recursively) is the minimum of the first element and the smallest element of the rest of the list. The task becomes trivial, when there is only one element. In Python3:

defsmallest(lst):
   """recursive for learning, not efficient"""
   first, *rest = lst
   return first ifnot rest elsemin(first, smallest(rest))

Solution 4:

I agree with others that you should avoid using min as a function name, so that you don't collide with python's builtin implementation. I'm also operating under the assumption that you're not supposed to use min, because otherwise the solution is trivial.

Here's a recursive implementation that doesn't require a second argument, since the list's length can be determined via the len function.

def smallest(lst):
    l =len(lst)
    if l > 1:
        mid = l // 2
        m1 = smallest(lst[:mid])
        m2 = smallest(lst[mid:])
        return m1 if m1 < m2 else m2
    return lst[0]

This checks to see whether the argument list has 2 or more values. If so, it splits the list into two halves, determines the smallest value in each half, then returns the smaller of the results. If there's only one element, it's trivially the smallest and gets returned.

Halving the list on each recursive call bounds the depth of the call stack to O(log n), where n is the original list's length. This prevents stack overflow from occurring with any list you could actually create in Python. Another proposed solution whittles the list down one-by-one, and will fail on lists with more than a thousand or so values.

Post a Comment for "How Do I Write A Recursive Function That Return The Minimum Element In An Array?"