user10158754
user10158754

Reputation:

Picking the smallest value in a list using recursion

This is a function I defined in order to find the smallest value in a list using recursion.However,I called the function twice within itself, which I think it's a bit weird. Is there a way around the function append()?. We haven't studied it yet so I'm asking whether there could be a simpler way to get the same solution by not using append()?

def minimum(lst):
    """
    parameters : lst of type list
    return : the value of the smallest element in the lst
    """
    if len(lst) == 1:
        return lst[0]

    if lst[0] < lst[1]:
        lst.append(lst[0])
        return(minimum(lst[1:]))
    return(minimum(lst[1:])) 

Upvotes: 1

Views: 212

Answers (6)

ask
ask

Reputation: 151

You can use the following program:

def minimum(lst):
    """
    parameters : lst of type list
    return : the value of the smallest element in the lst
    """
    if len(lst) == 1:
        return lst[0]
    temp_min = minimum(lst[1:])
    if lst[0] < temp_min:
        return lst[0]
    else:
        return temp_min

Upvotes: -1

cdlane
cdlane

Reputation: 41872

In Python 3, I'd be tempted to try:

def minimum(lst):
    if not lst:
        return None

    a, *rest = lst

    if rest:
        b = minimum(rest)

        if b < a:
            return b

    return a

Most of the proposed solutions, with the exception of @roeen30 (+1) but including the currently accepted one, don't defend themselves against minimum([]). Many go into an infinite recursion as a result!

Upvotes: 0

john doe
john doe

Reputation: 117

If your task is simply to find the smallest value in a list with any given length, I don't exactly know why you are using recursion. There are more efficient ways to do this such as built-in functions of Python. For example:

list_of_ints = [4,2,7,6,8,1,5,9,2]
print(min(list_of_ints)

This will print out 1.

Upvotes: -1

Christian Sloper
Christian Sloper

Reputation: 7510

I guess you could do this to avoid append:

def minimum(lst):

    if len(lst)==1:
        return lst[0]

    if lst[0] < lst[1]:
        return minimum(lst[0:1]+ lst[2:])
    else:
        return minimum(lst[1:])

but i think this one is better with only one call to minimum:

def minimum(lst):        
    if len(lst) == 1:
        return lst[0]        
    s = minimum(lst[1:])

    return s if s < lst[0] else lst[0]

Upvotes: 3

timgeb
timgeb

Reputation: 78690

Here's a very explicit version that should be easy to read due to comments and variable names.

def minimum(lst):
    # base case
    if len(lst) == 1:
        return lst[0]

    # get first element and minimum of remaining list
    first = lst[0]
    rest = lst[1:]
    min_of_rest = minimum(rest)

    # return the smaller one of those two values
    if first < min_of_rest:
        return first
    else:
        return min_of_rest

Upvotes: 1

roeen30
roeen30

Reputation: 789

Use an additional variable?

def minimum(lst, current_min=None):
    if not lst:
        return current_min
    if current_min is None:
        current_min = lst[0]
    elif lst[0] < current_min:
        current_min = lst[0]
    return minimum(lst[1:], current_min)

Upvotes: 2

Related Questions