Back to Freecodecamp

Step 12

curriculum/challenges/english/blocks/workshop-binary-search/683ec95f2f8781355556ff84.md

latest1.2 KB
Original Source

--description--

If the condition in the elif is true, then remove the pass keyword and update the value of the low variable by adding 1 to the mid variable.

This will extend the search to the right half of the current search areas in the list, because if the value is greater than value_at_middle, it means the value must be in the right half of the current search area.

--hints--

You should remove the pass keyword and update the low variable to mid + 1.

js
({
  test: () => assert(
    runPython(`_Node(_code).find_function("binary_search").find_whiles()[0].find_ifs()[0].find_bodies()[1].is_equivalent("low = mid + 1")`))
})

--seed--

--seed-contents--

py
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)

        if value == value_at_middle:
            return path_to_target
--fcc-editable-region--
        elif value > value_at_middle:
            pass
--fcc-editable-region--
        break
    return []

print(binary_search([1, 2, 3, 4, 5], 3))
print(binary_search([1, 2, 3, 4, 5, 9], 4))