import numpy as np
import typing
def tiles(seq: typing.Sequence[float], n: int) -> list[tuple[float, float]]:
"""
Returns the n-tiles of the sequence seq, after sorting.
"""
l = sorted(seq)
ranges = np.linspace(0, len(l) - 1, n + 1)
ret = []
for idx in range(1, len(ranges)):
ret.append((
l[round(ranges[idx-1])],
l[round(ranges[idx])]
))
return ret
print(tiles([2, 4, 6, 0, 1, 8, 6, 7, 5, 3, 0, 9], 4))
[(0, 2), (2, 5), (5, 6), (6, 9)]
class StatsList:
"""
Stores a list of numbers and provides some simple statistics.
"""
lst: list[float]
x = StatsList()
x.lst = []
print(x.lst)
[]
class StatsList:
"""
Stores a list of numbers and provides some simple statistics.
"""
lst: list[float]
def __init__(self) -> None:
self.lst = []
def foo(self) -> None:
print("foo was called on", self)
x = StatsList()
x.foo()
print(x.lst)
foo was called on <__main__.StatsList object at 0x7f4d2efc5ae0> []
import math
class StatsList:
"""
Stores a list of numbers and provides some simple statistics.
"""
lst: list[float]
sum: float
product: float
min: float
max: float
def __init__(self) -> None:
self.lst = []
self.sum = 0.0
self.product = 1.0
# We don't know a min or max yet, so nonsense
self.min = 0.0
self.max = 0.0
def append(self, val: float) -> None:
"""
Append val to the stored list, while also maintaining StatsList's
internal statistics.
"""
self.lst.append(val)
self.sum += val
self.product *= val # Watch for zero[e]s!
if len(self.lst) == 1:
self.min = val
self.max = val
else:
if val < self.min:
self.min = val
if val > self.max:
self.max = val
def mean(self) -> float:
"""
Returns the mean of the stored list.
"""
return self.sum / len(self.lst)
def geometricMean(self) -> float:
"""
Returns the geometric mean of the stored list.
"""
return self.product ** (1/len(self.lst))
x = StatsList()
x.append(42)
x.append(math.sqrt(2))
x.append(math.pi)
x.append(-1/12)
print("lst is", x.lst)
print("sum is", x.sum)
print("mean is", x.mean())
print("geomean is", x.geometricMean())
print("min is", x.min)
print("max is", x.max)
lst is [42, 1.4142135623730951, 3.141592653589793, -0.08333333333333333] sum is 46.47247288262955 mean is 11.618118220657388 geomean is (1.4041652819706363+1.404165281970636j) min is -0.08333333333333333 max is 42