Knowee
Questions
Features
Study Tools

Create a function that finds the highest integer in the list using recursion.Please use the recursion to solve this (not the max() method).Examplesfor, [-1, 3, 5, 6, 99, 12, 2] ➞ 99i/p:4012487o/p:87

Question

Create a function that finds the highest integer in the list using recursion.Please use the recursion to solve this (not the max() method).Examplesfor, [-1, 3, 5, 6, 99, 12, 2] ➞ 99i/p:4012487o/p:87

🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is a Python function that finds the highest integer in a list using recursion:

def find_max(lst):
    # Base case: if the list is empty, return None
    if not lst:
        return None
    # Base case: if the list has only one element, return that element
    elif len(lst) == 1:
        return lst[0]
    else:
        # Recursive case: find the maximum of the rest of the list
        max_of_rest = find_max(lst[1:])
        # Return the maximum of the first element and the maximum of the rest
        return lst[0] if lst[0] > max_of_rest else max_of_rest

You can use this function like this:

print(find_max([-1, 3, 5, 6, 99, 12, 2]))  # Output: 99
print(find_max([4, 0, 1, 2, 4, 8, 7]))  # Output: 8

This problem has been solved

Similar Questions

use reduce function to find the maximum element of the list.

1. List all the steps used by Algorithm "max" to find the maximum of the list 1, 8, 12, 9, 11,2, 14, 5, 10, 4.

Find the maximum element from the given array of integers.

Problem StatementYou are given a function,static String FindMax(int[] arr){}The function accepts an integer array 'arr' of length 'size' as its argument. Implement the function to find and return the maximum number that can be formed by any permutation or arrangement of all the digits obtained from all the numbers present in the array. You have to return the number formed as a string.Note: You may need to rearrange the digits of the numbers to form the maximum number.Example:Input:34 79 58 64Output:98765443Explanation:All digits obtained from all the numbers of array are 3, 4, 7, 9, 5, 8, 6, 4. Maximum number obtained after rearranging all these digit gives 98765443. Thus, output is 98765443.The custom input format for the above case:434 79 58 64(The first line represents the 'size', the second line represents the elements of the array 'arr')Sample input21 90 23Sample Output932210The custom input format for the above case:321 90 23(The first line represents the 'size', the second line represents the elements of the array 'arr')Instructions :This is a template based question, DO NOT write the "main" function.Your code is judged by an automated system, do not write any additional welcome/greeting messages."Save and Test" only checks for basic test cases, more rigorous cases will be used to judge your code while scoring.Additional score will be given for writing optimized code both in terms of memory and execution time.Now let's start coding :Language:Read-only code below . . .1class SolutionClass {2    public static void main(String[] args) throws java.lang.Exception {3        //Input read from STDIN4        String result = FindMax(arr);5        //Value in result printed to STDOUT6   }7}8​Write your code below . . .9static String FindMax(int[] arr) throws java.lang.Exception10{11    /* Write your code here. */12}13​

Create a list of integers from user input. Write the C program to find the element that occurs with the highest frequency and also find the second-highest number in the given elements.Sample Input:

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.