From 92ffefd6f076236821dc22330117531db3e63240 Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Thu, 6 Oct 2022 18:11:24 -0700 Subject: [PATCH] Minor cleanup. --- bigjoyints/lil_divmod.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/bigjoyints/lil_divmod.py b/bigjoyints/lil_divmod.py index a09fc7c..ec891b7 100644 --- a/bigjoyints/lil_divmod.py +++ b/bigjoyints/lil_divmod.py @@ -18,17 +18,31 @@ def lil_divmod(A, B): def find_greatest(low, high, f): ''' - Return the highest number n: low <= n <= high - for which f(n) and not f(n+1) is True. - The greatest n which makes f(n) True. + Return the highest number n: low <= n <= high for which: + + for all i in low..n f(i) is True + -and- + for all i in (n+1)..high f(i) is False. + + If the function f::int->bool doesn't behave like above + then I don't know what this function will do. + + There must be some number in the range or this function will + fail with an assertion error. (If you turn off assertions + it will succeed and return low in such a case.) ''' assert low <= high if f(high): return high pivot = (low + high) >> 1 + # If there isn't a pivot between low and high that means there's only + # two numbers it could be: low or high, and we already know it isn't + # high from the test above so it must be low. + if low == pivot: + assert f(low) and not f(low + 1) + return low + assert low < pivot < high return ( - low - if low == pivot else find_greatest(pivot, high - 1, f) if f(pivot) else find_greatest(low, pivot - 1, f)