Skip to content

Commit 63653f2

Browse files
committed
median_low examples
1 parent 5c0730c commit 63653f2

File tree

7 files changed

+184
-0
lines changed

7 files changed

+184
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections.abc import Iterable
2+
from typing import Any, TypeVar, Protocol
3+
4+
5+
class StatisticsError(ValueError):
6+
pass
7+
8+
9+
class Sortable(Protocol):
10+
def __lt__(self, other: Any) -> bool: ...
11+
12+
13+
SortableT = TypeVar('SortableT', bound=Sortable)
14+
15+
16+
def median_low(data: Iterable[SortableT]) -> SortableT:
17+
"""Return the low median value of data."""
18+
data = sorted(data)
19+
n = len(data)
20+
if n == 0:
21+
raise StatisticsError('no median for empty data')
22+
if n % 2 == 1:
23+
return data[n // 2]
24+
else:
25+
return data[n // 2 - 1]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from math import isclose
2+
3+
from new_statistics import median_low
4+
5+
6+
class SortableThing:
7+
def __init__(self, value):
8+
self.value = value
9+
10+
def __lt__(self, other):
11+
return self.value < other.value
12+
13+
def __repr__(self):
14+
return f'SortableThing({self.value})'
15+
16+
17+
def test_float() -> None:
18+
l = [1 / n for n in range(1, 10)]
19+
m = median_low(l)
20+
assert isclose(m, 1 / 5)
21+
22+
23+
def test_str() -> None:
24+
l = list('abcd')
25+
m = median_low(l)
26+
assert m == 'b'
27+
28+
29+
def test_sortable_thing() -> None:
30+
l = [SortableThing(n) for n in range(1, 8)]
31+
m = median_low(l)
32+
print(m)
33+
assert m.value == 4
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from collections.abc import Iterable
2+
from typing import Any, TypeVar, Protocol
3+
4+
5+
class StatisticsError(ValueError):
6+
pass
7+
8+
9+
class SupportsLessThan(Protocol):
10+
def __lt__(self, __other: Any) -> bool: ...
11+
12+
13+
SupportsLessThanT = TypeVar('SupportsLessThanT', bound=SupportsLessThan)
14+
15+
16+
def median_low(data: Iterable[SupportsLessThanT]) -> SupportsLessThanT:
17+
"""Return the low median value of data."""
18+
data = sorted(data)
19+
n = len(data)
20+
if n == 0:
21+
raise StatisticsError('no median for empty data')
22+
if n % 2 == 1:
23+
return data[n // 2]
24+
else:
25+
return data[n // 2 - 1]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from new_statistics import median_low
2+
3+
4+
def test_object() -> None:
5+
l = [object() for _ in range(1, 10)]
6+
try:
7+
median_low(l)
8+
except TypeError as exc:
9+
print(repr(exc))
10+
11+
12+
if __name__ == '__main__':
13+
test_object()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from math import isclose
2+
3+
from new_statistics import median_low
4+
5+
6+
class SortableThing:
7+
def __init__(self, value):
8+
self.value = value
9+
10+
def __lt__(self, other):
11+
return self.value < other.value
12+
13+
def __repr__(self):
14+
return f'SortableThing({self.value})'
15+
16+
17+
def test_float() -> None:
18+
l = [1 / n for n in range(1, 10)]
19+
m = median_low(l)
20+
assert isclose(m, 1 / 5)
21+
22+
23+
def test_str() -> None:
24+
l = list('abcd')
25+
m = median_low(l)
26+
assert m == 'b'
27+
28+
29+
def test_sortable_thing() -> None:
30+
l = [SortableThing(n) for n in range(1, 8)]
31+
m = median_low(l)
32+
print(m)
33+
assert m.value == 4
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from collections.abc import Iterable
2+
from decimal import Decimal
3+
from fractions import Fraction
4+
from typing import TypeVar
5+
6+
_Number = TypeVar('_Number', float, Decimal, Fraction)
7+
8+
9+
class StatisticsError(ValueError):
10+
pass
11+
12+
13+
def median_low(data: Iterable[_Number]) -> _Number:
14+
"""Return the low median value of data."""
15+
data = sorted(data)
16+
n = len(data)
17+
if n == 0:
18+
raise StatisticsError('no median for empty data')
19+
if n % 2 == 1:
20+
return data[n // 2]
21+
else:
22+
return data[n // 2 - 1]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from math import isclose
2+
3+
from old_statistics import median_low
4+
5+
6+
class SortableThing:
7+
def __init__(self, value):
8+
self.value = value
9+
10+
def __lt__(self, other):
11+
return self.value < other.value
12+
13+
def __repr__(self):
14+
return f'SortableThing({self.value})'
15+
16+
17+
def test_float() -> None:
18+
l = [1 / n for n in range(1, 10)]
19+
m = median_low(l)
20+
assert isclose(m, 1 / 5)
21+
22+
23+
def test_str() -> None:
24+
l = list('abcd')
25+
m = median_low(l)
26+
assert m == 'b'
27+
28+
29+
def test_sortable_thing() -> None:
30+
l = [SortableThing(n) for n in range(1, 8)]
31+
m = median_low(l)
32+
print(m)
33+
assert m.value == 4

0 commit comments

Comments
 (0)