The Loop That Stalls
f is monotone: true for every and false for every . This is meant to return the largest with f(x) true.
def last_true(lo, hi, f):
while lo < hi:
mid = (lo + hi) // 2
if f(mid):
lo = mid
else:
hi = mid - 1
return lo
last_true(1, 10, f)
What happens?
Sign in to answer questions and track your progress
Sign In