Challenge 1: Average of Numbers

Given an array of numbers, compute the average of all the numbers recursively.

Problem Statement

Implement a function that computes the average of all the numbers in an array.

If we have nn numbers where each number is denoted by aia_i (wherei=0,1,2,...,n1)(where \: i = 0, 1, 2, ..., n-1), the average is the sumsum of the numbers divided by nn or;

average=1ni=0n1ai=a0+a1+a2++an1n{\displaystyle average={\frac {1}{n}}\sum _{i=0}^{n-1}a_{i}={\frac {a_{0}+a_{1}+a_{2}+\cdots +a_{n-1}}{n}}}

Try solving this problem recursively.

Input

  1. A testVariable containing an array of numbers.
  2. The currentIndex of the array.

Output

Average of the numbers in the input array.

Sample Input

testVariable = [10, 2, 3, 4, 8, 0] 
currentIndex = 0

Sample Output

4.5

Try it Yourself

Try to attempt this challenge by yourself before moving on to the solution. Good luck!

Press + to interact
def average(testVariable, currentIndex = 0) :
# Write your code here
return None

Let’s have a look at the solution review of this problem.