X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=math_utils.py;h=37fcec5f6c557cdf1a66d39b671fd8d9438ba29c;hb=804988efe7c8b131fc0c4b30088de2b5c5bd0b74;hp=e0e3f6c10732b9a3ab20a251a225a0e963c362e9;hpb=eb9e6df32ed696158bf34dba6464277b648f5c74;p=python_utils.git diff --git a/math_utils.py b/math_utils.py index e0e3f6c..37fcec5 100644 --- a/math_utils.py +++ b/math_utils.py @@ -1,9 +1,11 @@ #!/usr/bin/env python3 +"""Mathematical helpers.""" + import functools import math +from heapq import heappop, heappush from typing import List -from heapq import heappush, heappop class RunningMedian(object): @@ -39,7 +41,7 @@ class RunningMedian(object): def get_median(self): if len(self.lowers) == len(self.highers): - return (-self.lowers[0] + self.highers[0])/2 + return (-self.lowers[0] + self.highers[0]) / 2 elif len(self.lowers) > len(self.highers): return -self.lowers[0] else: @@ -76,8 +78,8 @@ def truncate_float(n: float, decimals: int = 2): 3.141 """ - assert decimals > 0 and decimals < 10 - multiplier = 10 ** decimals + assert 0 < decimals < 10 + multiplier = 10**decimals return int(n * multiplier) / multiplier @@ -143,12 +145,12 @@ def is_prime(n: int) -> bool: # This is checked so that we can skip middle five numbers in below # loop - if (n % 2 == 0 or n % 3 == 0): + if n % 2 == 0 or n % 3 == 0: return False i = 5 while i * i <= n: - if (n % i == 0 or n % (i + 2) == 0): + if n % i == 0 or n % (i + 2) == 0: return False i = i + 6 return True @@ -156,4 +158,5 @@ def is_prime(n: int) -> bool: if __name__ == '__main__': import doctest + doctest.testmod()