Back to Freecodecamp

Step 7

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

latest865 B
Original Source

--description--

Now, include value_at_middle in the path_to_target list to track the steps taken during the search, regardless of whether it's the target.

--hints--

You should use the append() method to add value_at_middle to the path_to_target list.

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

--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:
        mid = (low + high) // 2
        value_at_middle = search_list[mid]
        
--fcc-editable-region--