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 Caseif 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 sumreturn (testVariable[currentIndex] + average(testVariable, currentIndex + 1))# Driver codearr = [10, 2, 3, 4, 8, 0]print(average(arr))
Explanation
The average of an array containing only ...
Access this course and 1400+ top-rated courses and projects.