forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatistics.py
More file actions
29 lines (21 loc) · 786 Bytes
/
statistics.py
File metadata and controls
29 lines (21 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
infinity = float('inf')
infinities = (infinity, -infinity)
def get_median(iterable):
'''Get the median of an iterable of numbers.'''
sorted_values = sorted(iterable)
assert len(sorted_values) != 0
if len(iterable) % 2 == 0:
higher_midpoint = len(iterable) // 2
lower_midpoint = higher_midpoint - 1
return (sorted_values[lower_midpoint] + sorted_values[higher_midpoint]) / 2
midpoint = len(iterable) // 2
return sorted_values[midpoint]
def get_mean(iterable):
'''Get the mean (average) of an iterable of numbers.'''
sum_ = 0
count = 0
for count, value in enumerate(iterable):
sum_ += value
return sum_ / (count + 1)