import math from typing import Callable, Iterable, Optional, TypeVar _T = TypeVar("_T") def first( iterable: Iterable[_T], condition: Callable[[_T], bool] = lambda x: True ) -> Optional[_T]: return next((x for x in iterable if condition(x)), None) def percentile( N: Iterable[float], percent: float, key: Callable[[float], float] = lambda x: x ) -> Optional[float]: if not N: return None N = sorted(N) k = (len(N) - 1) * percent f = math.floor(k) c = math.ceil(k) if f == c: return key(N[int(k)]) d0 = key(N[int(f)]) * (c - k) d1 = key(N[int(c)]) * (k - f) return d0 + d1