Find max value in list using recursion python

- [Instructor] For this example, we're going to use recursion one more time, and we're going to write a recursive function to find the maximum value in a list of values. And so here's how the function's going to work. Let me go ahead an open up the findmax_start. We have a function named findmax, and it's going to accept a list of of numeric values, and when the function is called, it will check to see if the list has only one item in it, and, if so, it will just return that value. Otherwise, it will get the first value in the list, and then assign the second comparison value to a call to itself with the remaining values. So after all the values are retrieved, each value will be compared with its neighbor to determine which is largest. And that's going to produce a chain of function calls, until we reach a point where the list has been exhausted, and then the chain will work its way back up, and compare all the individual values until we've found the last one. So, let's go ahead and…

Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.

Watch courses on your mobile device without an internet connection. Download courses using your iOS or Android LinkedIn Learning app.

Write a recursive Python function findMaxVal() that takes an input list of numbers and returns the maximum value stored the list. Do not use any max() function from any Python package. Your code should only contain if blocks and recursive function calls. Your function should return None if the input array is an empty list. For example,

findMaxVal([1,2,-3,4.5,2,-1])

4.5 True

Find max value in list using recursion python
Python

def findMaxVal(array): lenArray = len(array) if lenArray>1: maxVal = findMaxVal(array[1:]) if array[0]>maxVal: return array[0] else: return maxVal elif lenArray==1: return array[0] else: # lenArray < 1 return None

Find max value in list using recursion python

In the previous article, we have discussed Python Program For Division Two Numbers Operator Without Using Division(/) Operator

Given a list and the task is to find the maximum and minimum elements in a given List using recursion in python

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given List = [1, 6, 3, 7, 8, 4]

Output:

The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8 The Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1

Example2:

Input:

Given List = [20, 30, 40, 10, 50]

Output:

The Maximum element in a given list [20, 30, 40, 10, 50] = 50 The Minimum element in a given list [20, 30, 40, 10, 50] = 10

Program to Find Maximum and Minimum Elements in List/Array Using Recursion in Python

Below are the ways to find the maximum and minimum elements in a given List using recursion in python:

  • Using Recursion (Static Input)
  • Using Recursion (User Input)

Method #1: Using Recursion (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the max_elemnt, min_elemnt functions.
  • Create a recursive function to say max_elemnt which takes the given list and length of the given list as the arguments and returns the maximum element in a given list using recursion.
  • Check if the length of the given list is 1 using the if conditional statement.
  • If the statement is true, then return the first element of the list.
  • Return maximum of (gven_lst[len_lst – 1], max_elemnt(gven_lst, len_lst – 1) {Recursive logic}.
  • Create a recursive function to say min_elemnt which takes the given list and length of the given list as the arguments and returns the minimum element in a given list using recursion.
  • Check if the length of the given list is 1 using the if conditional statement.
  • If the statement is true, then return the first element of the list.
  • Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) {Recursive logic}.
  • Print the maximum element of the given list.
  • Print the minimum element of the given list.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say max_elemnt which takes the given list and # length of the given list as the arguments and returns the maximum element in a # given list using recursion. def max_elemnt(gven_lst, len_lst): # Check if the length of the given list is 1 using the if conditional statement. if len_lst == 1: # If the statement is true, then return the first element of the list. return gven_lst[0] # Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1) # {Recursive logic}. return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)) # Create a recursive function to say min_elemnt which takes the given list and # length of the given list as the arguments and returns the minimum element in a # given list using recursion. def min_elemnt(gven_lst, len_lst): # Check if the length of the given list is 1 using the if conditional statement. if len_lst == 1: # If the statement is true, then return the first element of the list. return gven_lst[0] # Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) # {Recursive logic}. return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)) # Give the list as static input and store it in a variable. gven_lst = [1, 6, 3, 7, 8, 4] # Calculate the length of the given list and store it in another variable. len_lst = len(gven_lst) # Pass the given list and length of the given list as the arguments to the max_elemnt, # min_elemnt functions. # Print the maximum element of the given list. print("The Maximum element in a given list", gven_lst, "=", max_elemnt(gven_lst, len_lst)) # Print the minimum element of the given list. print("The Minimum element in a given list", gven_lst, "=", min_elemnt(gven_lst, len_lst))

Output:

The Maximum element in a given list [1, 6, 3, 7, 8, 4] = 8 The Minimum element in a given list [1, 6, 3, 7, 8, 4] = 1

Method #2: Using Recursion (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list and store it in another variable.
  • Pass the given list and length of the given list as the arguments to the max_elemnt, min_elemnt functions.
  • Create a recursive function to say max_elemnt which takes the given list and length of the given list as the arguments and returns the maximum element in a given list using recursion.
  • Check if the length of the given list is 1 using the if conditional statement.
  • If the statement is true, then return the first element of the list.
  • Return maximum of (gven_lst[len_lst – 1], max_elemnt(gven_lst, len_lst – 1) {Recursive logic}.
  • Create a recursive function to say min_elemnt which takes the given list and length of the given list as the arguments and returns the minimum element in a given list using recursion.
  • Check if the length of the given list is 1 using the if conditional statement.
  • If the statement is true, then return the first element of the list.
  • Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) {Recursive logic}.
  • Print the maximum element of the given list.
  • Print the minimum element of the given list.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say max_elemnt which takes the given list and # length of the given list as the arguments and returns the maximum element in a # given list using recursion. def max_elemnt(gven_lst, len_lst): # Check if the length of the given list is 1 using the if conditional statement. if len_lst == 1: # If the statement is true, then return the first element of the list. return gven_lst[0] # Return maximum of (gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1) # {Recursive logic}. return max(gven_lst[len_lst - 1], max_elemnt(gven_lst, len_lst - 1)) # Create a recursive function to say min_elemnt which takes the given list and # length of the given list as the arguments and returns the minimum element in a # given list using recursion. def min_elemnt(gven_lst, len_lst): # Check if the length of the given list is 1 using the if conditional statement. if len_lst == 1: # If the statement is true, then return the first element of the list. return gven_lst[0] # Return minimum of (gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1) # {Recursive logic}. return min(gven_lst[len_lst-1], min_elemnt(gven_lst, len_lst-1)) # Give the list as user input using list(),map(),input(),and split() functions. # Store it in a variable. gven_lst = list(map(int, input( 'Enter some random List Elements separated by spaces = ').split())) # Calculate the length of the given list and store it in another variable. len_lst = len(gven_lst) # Pass the given list and length of the given list as the arguments to the max_elemnt, # min_elemnt functions. # Print the maximum element of the given list. print("The Maximum element in a given list", gven_lst, "=", max_elemnt(gven_lst, len_lst)) # Print the minimum element of the given list. print("The Minimum element in a given list", gven_lst, "=", min_elemnt(gven_lst, len_lst))

Output:

Enter some random List Elements separated by spaces = 20 30 40 50 10 The Maximum element in a given list [20, 30, 40, 50, 10] = 50 The Minimum element in a given list [20, 30, 40, 50, 10] = 10

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.