Recursive average in Python

Fore more information please refer to the following post. This post only implements the solution in Python

Code

def RecursiveAvg(A, i, n):

	# Base case
	if i == n-1:
		return A[i]/n

	return A[i]/n + RecursiveAvg(A, i + 1, n)

A = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
print "Average of A = {}".format(RecursiveAvg(A, 0, 10))

If you run the code snippet above you should get

Average of A = 5.5

For questions please use the comments section below.

Leave a Reply