Minor cleanup.
This commit is contained in:
parent
bad1a909c7
commit
92ffefd6f0
|
|
@ -18,17 +18,31 @@ def lil_divmod(A, B):
|
||||||
|
|
||||||
def find_greatest(low, high, f):
|
def find_greatest(low, high, f):
|
||||||
'''
|
'''
|
||||||
Return the highest number n: low <= n <= high
|
Return the highest number n: low <= n <= high for which:
|
||||||
for which f(n) and not f(n+1) is True.
|
|
||||||
The greatest n which makes f(n) True.
|
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
|
assert low <= high
|
||||||
if f(high):
|
if f(high):
|
||||||
return high
|
return high
|
||||||
pivot = (low + high) >> 1
|
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 (
|
return (
|
||||||
low
|
|
||||||
if low == pivot else
|
|
||||||
find_greatest(pivot, high - 1, f)
|
find_greatest(pivot, high - 1, f)
|
||||||
if f(pivot) else
|
if f(pivot) else
|
||||||
find_greatest(low, pivot - 1, f)
|
find_greatest(low, pivot - 1, f)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue