Skip to content

Instantly share code, notes, and snippets.

@euikook
Created April 23, 2021 00:22
Show Gist options
  • Save euikook/888f2f8c1a7226995cae998e83eb9239 to your computer and use it in GitHub Desktop.
Save euikook/888f2f8c1a7226995cae998e83eb9239 to your computer and use it in GitHub Desktop.
Average Filter
def avg_batch(v):
return sum(v)/len(v)
def avg_recursive(a, v, n):
return n/(n + 1)*a + v/(n+1)
if __name__ == '__main__':
ages = [10, 12, 26, 30, 40, 33, 21, 34, 54, 3]
print(f'Average using Batch Expression: {avg_batch(ages)}')
avg = 0
for idx in range(len(ages)):
avg = avg_recursive(avg, ages[idx], idx)
print(f'Average using Recursive Expression: {avg}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment