Created
November 11, 2019 11:03
-
-
Save mrschyte/5ad39ba7101babbb3ac3598212317baf to your computer and use it in GitHub Desktop.
Running average
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
_avg = None | |
_min = None | |
_max = None | |
num = [] | |
n = 0 | |
for i in range(10): | |
k = random.randint(0, 10) | |
num.append(k) | |
if _avg == None: | |
_min = _max = _avg = k | |
else: | |
if _min < k: | |
_min = k | |
if _max > k: | |
_max = k | |
_avg = (_avg + k / n) * n / (n + 1) | |
n += 1 | |
print(_avg, sum(num) / n, _min, _max) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment