Q. Write a recursive Python function that has a parameter representing a list of integers and returns the maximum stored in the list. Thinking recursively, the maximum is either the first value in the list or the maximum of the rest of the list, whichever is larger. If the list only has 1 integer, then its maximum is this single value, naturally.

Hint. If A is a list of integers, and you want to set the list B to all of the integers in A except the first one, you can write B = A [1: len (A)]

 

Answer =

 



def maxim(maximum , lst , i ):
    if i == len(lst):
        print(maximum)
    elif maximum < lst [ i ] :
        maximum = lst [ i ]
        return maxim(maximum, lst , i+1 )
    else :
        return maxim(maximum , lst , i+1 )

lst = eval(input("Enter a list :-"))       
maxim(lst[ 0 ] , lst , 1)

 

Post a Comment

You can help us by Clicking on ads. ^_^
Please do not send spam comment : )

Previous Post Next Post