X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=src%2Fpyutils%2Fmath_utils.py;h=ce9940d085ff8935a5f2a451d6398faa6608b4b4;hb=a3257d6eba8699af801991cec28a0c49eb034d40;hp=97bb635ac7af5cb31a680338a87ffaecbb4adce5;hpb=2ca7b9cb3a9ad208b4fac1e179048b8e5305c832;p=pyutils.git diff --git a/src/pyutils/math_utils.py b/src/pyutils/math_utils.py index 97bb635..ce9940d 100644 --- a/src/pyutils/math_utils.py +++ b/src/pyutils/math_utils.py @@ -21,6 +21,8 @@ class NumericPopulation(object): >>> pop.add_number(1) >>> pop.add_number(10) >>> pop.add_number(3) + >>> len(pop) + 3 >>> pop.get_median() 3 >>> pop.add_number(7) @@ -59,6 +61,15 @@ class NumericPopulation(object): if not self.minimum or number < self.minimum: self.minimum = number + def __len__(self): + """Return the population size.""" + n = 0 + if self.highers: + n += len(self.highers) + if self.lowers: + n += len(self.lowers) + return n + def _rebalance(self): if len(self.lowers) - len(self.highers) > 1: heappush(self.highers, -heappop(self.lowers))