Skip to content

Commit 4f03be5

Browse files
committed
-
1 parent 49b4bc1 commit 4f03be5

File tree

4 files changed

+63
-1
lines changed

4 files changed

+63
-1
lines changed

README.markdown

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ GitHub: https://github.com/cool-RR/python_toolbox
2323

2424
CI server: https://jenkins.shiningpanda.com/python-toolbox/job/python_toolbox/
2525

26+
The Python Toolbox is released under the MIT license.
27+
2628
# Not backward-compatible yet #
2729

2830
Please keep in mind that Python Toolbox is still in alpha stage, and that backward compatibility would *not* be maintained in this phase.

python_toolbox/math_tools.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,23 @@ def convert_to_base_in_tuple(number, base):
5858

5959
return tuple(reversed(work_in_progress))
6060

61-
61+
62+
def get_median(iterable):
63+
sorted_values = sorted(iterable)
64+
65+
if len(iterable) % 2 == 0:
66+
higher_midpoint = len(iterable) // 2
67+
lower_midpoint = higher_midpoint - 1
68+
return (sorted_values[lower_midpoint] +
69+
sorted_values[higher_midpoint]) / 2
70+
else:
71+
midpoint = len(iterable) // 2
72+
return sorted_values[midpoint]
73+
74+
75+
def get_mean(iterable):
76+
sum_ = 0
77+
for i, value in enumerate(iterable):
78+
sum_ += value
79+
return sum_ / (i + 1)
80+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from __future__ import with_statement
5+
6+
import sys
7+
8+
import nose
9+
10+
from python_toolbox.math_tools import get_mean
11+
12+
13+
def test_mean():
14+
assert get_mean((1, 2, 3)) == 2
15+
assert get_mean((1, 2, 3, 4)) == 2.5
16+
assert get_mean((1, 2, 3, 4, 5)) == 3
17+
assert get_mean((-1, -2, -3, -4, -5)) == -3
18+
assert get_mean((-0.5, -2, -3, -4, -5.5)) == -3
19+
assert 1821.666 < get_mean((1000, 10000.5, -2, -4, -14, -50.5)) < 1821.667
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright 2009-2013 Ram Rachum.
2+
# This program is distributed under the MIT license.
3+
4+
from __future__ import with_statement
5+
6+
import sys
7+
8+
import nose
9+
10+
from python_toolbox.math_tools import get_median
11+
12+
13+
def test_median():
14+
assert get_median((1, 2, 3)) == 2
15+
assert get_median((1, 2, 3, 4)) == 2.5
16+
assert get_median((1, 2, 3, 4, 5)) == 3
17+
assert get_median((-1, -2, -3, -4, -5)) == -3
18+
assert get_median((-0.5, -2, -3, -4, -5.5)) == -3
19+
assert get_median((-0.5, -2, -3, -4, -50.5)) == -3
20+
assert get_median((10000.5, -2, -3, -14, -50.5)) == -3
21+
assert get_median((10000.5, -2, -4, -14, -50.5)) == -4
22+
assert get_median((1000, 1000, 10000.5, -2, -4, -14, -50.5)) == -2

0 commit comments

Comments
 (0)