Back to Freecodecamp

Step 5

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

latest1.2 KB
Original Source

--description--

To get started with the loop, you need to find the middle index of the current search space.

You can get the middle index by calculating the average of the low and high variables. You will have to use floor division to get the average after adding the two values, so the answer would always be an integer rounded down, as the indices must be integers.

Remove the pass keyword from your while loop and create a mid variable set to the average of low and high.

--hints--

You should remove the pass keyword from the while loop and create a mid variable set to (low + high) // 2.

js
({
    test: ()=>
        assert(
            runPython(
                `_Node(_code).find_function("binary_search").find_whiles()[0].find_body().is_equivalent("mid = (low + high) // 2") or _Node(_code).find_function("binary_search").find_whiles()[0].find_body().is_equivalent("mid = (high + low) // 2")`))
})

--seed--

--seed-contents--

py
def binary_search(search_list, value):
    path_to_target = []
    low = 0
    high = len(search_list) - 1

--fcc-editable-region--
    while low <= high:
        pass
--fcc-editable-region--