blob: 1c0cd20444ff8f0546d021f328d16e3978270495 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# DESCRIPTION: The Weekly Challenge 122
# Task 1 - Average of Stream
#
# AUTHOR: Lubos Kolouch
# CREATED: 07/24/2021 10:27:06 AM
# ===============================================================================
def print_moving_average(what):
total = what[0]
arr_str = str(what[0])
# print the first MA to avoid unnecessary IF in the loop for each number
print(f"Average of the first number is {total}")
for pos, i in enumerate(what[1:]):
total += i
arr_str += f"+{str(i)}"
print(f"Average of the first {pos+1} numbers ({arr_str})/{pos+2} = {total/(pos+2)}")
return total/len(what)
assert print_moving_average([10, 20, 30, 40, 50, 60, 70, 80, 90]) == 50
|