I am trying to resolve the average of numbers using a "divide and conquer" algorithm but the program that i have written only works when the number of elements is power of two (2,4,8,etc).
The code is written in python and it is as follows
def averageDyC(ini, end, notes):
if (ini == end):
return notes[ini]
mid = int((ini + end) / 2)
x = averageDyC(ini, mid, notes)
y = averageDyC(mid + 1, end, notes)
return float((x + y) / 2)
notes = [1, 4, 7, 8, 9, 2, 4]
average = averageDyC(0, len(notas) - 1, notes)
This code displays 3,75 but the correct average is 3,5
How can I chage this code to allow number of elements different of power of 2?