Complete the function that
accepts two integer arrays of equal length
compares the value each member in one array to the corresponding member in the other
squares the absolute value difference between those two values
and returns the average of those squared absolute value difference between each member >pair.
Examples
[1, 2, 3], [4, 5, 6] --> 9 because (9 + 9 + 9) / 3
[10, 20, 10, 2], [10, 25, 5, -2] --> 16.5 because (0 + 25 + 25 + 16) / 4
[-1, 0], [0, -1] --> 1 because (1 + 1) / 2
題目理解:設計一函式代入兩組皆為整數的相同長度列表,依序將兩列表成員間的絕對值差平方,並將每組成員間此值的平均值返還。
由於已經確定題目兩列表長度相符,故可利用len(range([lst]))的模式在迴圈中同時對兩列表處理計算。
def solution(array_a, array_b):
sum = 0
#利用列表表長度範圍相同,來達到遞迴中不斷檢索同一位置的效果
for num in range(len(array_a)):
sum += abs(array_a[num]-array_b[num])**2
return sum/len(array_a)