curriculum/challenges/english/blocks/workshop-binary-search/683ec95f2f8781355556ff83.md
You can see that the first call returns [3]. That's because 3 is the middle value in [1, 2, 3, 4, 5].
On the other hand, the second call returns an empty list. This happens because, after checking the middle value, the loop currently lacks the logic to narrow the search range by updating the low or high variables. It only acts as a single midpoint check and then finishes without further searching.
To allow the binary search to continue narrowing its search, add an elif block that checks if value is greater than value_at_middle. Add the pass keyword inside the elif for now.
You should add an elif block for when value is greater than value_at_middle.
({ test: () => runPython(`
_cond = _Node(_code).find_function("binary_search").find_whiles()[0].find_ifs()[0].find_conditions()[1]
assert _cond.is_equivalent("value > value_at_middle") or _cond.is_equivalent("value_at_middle < value")
`) })
def binary_search(search_list, value):
path_to_target = []
low = 0
high = len(search_list) - 1
while low <= high:
mid = (low + high) // 2
value_at_middle = search_list[mid]
path_to_target.append(value_at_middle)
--fcc-editable-region--
if value == value_at_middle:
return path_to_target
--fcc-editable-region--
break
return []
print(binary_search([1, 2, 3, 4, 5], 3))
print(binary_search([1, 2, 3, 4, 5, 9], 4))