def find_max_value(test_func, initial_value):
"""
Starting from an initial number (integer or float), find the maximum value
for which the test function does not yet fail, and return that maximum
value.
"""
assert isinstance(initial_value, int) and initial_value > 0
fails = FailsArray(test_func)
value = initial_value
# Advance the value exponentially beyond the max value
while fails[value] == 0:
value *= 2
# Search for the exact max value in the previous range. We search for the
# boundary where the fails array goes from 0 to 1.
boundary = 0.5
value = binary_search(fails, boundary, value // 2, value)
max_value = value - 1
# Verify that we found exactly the maximum:
assert fails[max_value] == 0 and fails[max_value + 1] == 1, \
"max_value={}, fails[+-2]: {}, {}, {}, {}, {}".\
format(max_value, fails[max_value - 2], fails[max_value - 1],
fails[max_value], fails[max_value + 1], fails[max_value + 2])
return max_value
评论列表
文章目录