Back to Freecodecamp

Step 10

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

latest1.2 KB
Original Source

--description--

Be reminded that the function takes a list called search_list and the value you're looking for.

Now, call the function with binary_search([1, 2, 3, 4, 5], 3) and binary_search([1, 2, 3, 4, 5, 9], 4) and print the calls right away.

--hints--

You should call the function with binary_search([1, 2, 3, 4, 5], 3) and print it right away.

js
({
    test: () => assert(runPython(`
      _Node(_code).has_call("print(binary_search([1, 2, 3, 4, 5], 3))")
    `))
})

You should call the function with binary_search([1, 2, 3, 4, 5, 9], 4) and print it right away.

js
({
    test: () => assert(runPython(`
      _Node(_code).has_call("print(binary_search([1, 2, 3, 4, 5, 9], 4))")
    `))
})

--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
        break
    return []
          
--fcc-editable-region--

--fcc-editable-region--