...

/

Solution Review: Average of Numbers

Solution Review: Average of Numbers

This review provides a detailed analysis of the solution to find the average of numbers.

We'll cover the following...

Solution: Using Recursion

Press + to interact
def average(testVariable, currentIndex = 0) :
# Base Case
if currentIndex == len(testVariable) - 1 :
return testVariable[currentIndex]
# Recursive case1
# When currentIndex is 0, divide sum computed so far by len(testVariable).
if currentIndex == 0 :
return ((testVariable[currentIndex] + average(testVariable, currentIndex + 1)) / len(testVariable))
# Recursive case2
# Compute sum
return (testVariable[currentIndex] + average(testVariable, currentIndex + 1))
# Driver code
arr = [10, 2, 3, 4, 8, 0]
print(average(arr))

Explanation

The average of an array containing only 1 ...

Access this course and 1400+ top-rated courses and projects.