Problem

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
findMaxVal([]) is None
True

Solution

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

Comments