Keep requirements up-to-date.
[python_utils.git] / math_utils.py
index fa0bc0e7bdbbb7789ed56420f17d64b66375cd92..37fcec5f6c557cdf1a66d39b671fd8d9438ba29c 100644 (file)
@@ -1,12 +1,14 @@
 #!/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:
+class RunningMedian(object):
     """A running median computer.
 
     >>> median = RunningMedian()
@@ -39,7 +41,7 @@ class RunningMedian:
 
     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()