Find the max and min of a set of values using recursion Find the max and min of a set of values using recursion. First input the values. The first value inputted is the number of additional data items to input. For instance, if the input is: 3 1 2 3

Respuesta :

Answer:

This question is answered using Python programming language

def MaxSet(mylist, count):  

     if (count == 1):  

           return mylist[0]  

     return max(mylist[count - 1], MaxSet(mylist, count - 1))  

def MinSet(mylist, count):  

     if (count == 1):  

           return mylist[0]  

     return min(mylist[count - 1], MinSet(mylist, count - 1))  

count = int(input("Length of set: "))

mylist = []  

for i in range(count):

     inp= int(input("Input: "))

     mylist.append(inp)

   

print("Minimum: "+str(MinSet(mylist, count)) )

print("Maximum: "+str(MaxSet(mylist, count)) )

Explanation:

This defines the recursion that returns the maximum

def MaxSet(mylist, count):  

This following checks for the maximum using recursion

     if (count == 1):  

           return mylist[0]  

     return max(mylist[count - 1], MaxSet(mylist, count - 1))  

This defines the recursion that returns the minimum

def MinSet(mylist, count):  

This following checks for the minimum using recursion

     if (count == 1):  

           return mylist[0]  

     return min(mylist[count - 1], MinSet(mylist, count - 1))  

The main begins here

This prompts user for length of set

count = int(input("Length of set: "))

This defines an empty list

mylist = []  

The following iteration gets user input

for i in range(count):

     inp= int(input("Input: "))

     mylist.append(inp)

This calls the minimum function    

print("Minimum: "+str(MinSet(mylist, count)) )

This calls the maximum function

print("Maximum: "+str(MaxSet(mylist, count)) )

ACCESS MORE