0

I am still learning recursion principles and trying to write a function which returns max of all values in an array by recursive calls with lower and upper halves of the array.

I know i would need to find the mid point then split the list in two then recall the function to continue doing until each list is at length 1. I am just confused how to continue from there.

Any help is appreciated.

My function:

def bin_max(L):
    '''
    Returns max of all values in list L by recursive calls with lower and upper
    halves of L. If L is empty, this returns -1e20
    Ex. usage:  bin_max([ 2, 1, 6, 8, -3, 7 -5, 5])  -->  returns: 8
    
    '''
    minVal = -1e20
    if L == []:
        return minVal   
        
    if len(L) == 1:
        return L[0] 
    mid = len(L)//2
    
    Left  = L[:mid]
    Right = L[mid:]
    
    if len(Left)> 1:
        bin_max(Left)
    if len(Right)> 1:
        bin_max(Right)

1
  • what I can come up with is if len(L)> 1: L = bin_max(L) and if len(R)> 1: R = bin_max(R) Commented Jun 11, 2021 at 1:51

1 Answer 1

1

You were almost there, there were two mistake in code. The first is if condition if len(Left)> 1: and if len(Right)> 1: Those are not really needed. Infact they might even prevent you from reaching base case. The base case handles if the length of list reaches less than 1.

The other was merging the solution from left and right. From left sublist you know 6 is biggest number while in right sublist, you know that 8 is biggest number. These number will be retuned recursively calling bin_max(input_list). So you should then compare the output from left and right to get the biggest element in list.

def bin_max(input_list):
    """
    Returns max of all values in list L by recursive calls with lower and upper
    halves of L. If L is empty, this returns -1e20
    Ex. usage:  bin_max([ 2, 1, 6, 8, -3, 7 -5, 5])  -->  returns: 8
    """
    # This handles if the len of list reaches less than 1
    if len(input_list) == 0:
        return -1e20

    # If length if list is one, we return that single element
    if len(input_list) == 1:
        return input_list[0]

    # Splitting of the list
    mid = len(input_list) // 2
    left = input_list[:mid]
    right = input_list[mid:]

    # Finding the biggest element in left sub list
    left_max_value = bin_max(left)
    # Finding the biggest element in right sub list
    right_max_value = bin_max(right)

    # Comparing the result from both sides and returning the biggest value
    # You can use return max(left_max_value, right_max_value) as well if you
    # don't like if-else condition
    if right_max_value > left_max_value:
        return right_max_value
    else:
        return left_max_value
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.