Back to Freecodecamp

Step 8

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

latest1.3 KB
Original Source

--description--

The next thing to do is to create a condition that will check if the target value is in the middle.

Inside the while loop, create an if statement that checks if the target value is equal to value_at_middle. If it is, return the path_to_target variable.

--hints--

You should create an if statement that checks if the value is equal to value_at_middle.

js
({ test: () => runPython(`
_cond = _Node(_code).find_function("binary_search").find_whiles()[0].find_body().find_ifs()[0].find_conditions()[0]
assert _cond.is_equivalent("value == value_at_middle") or _cond.is_equivalent("value_at_middle == value")
`) })

You should return the path_to_target variable inside the if statement.

js
({
    test: () =>
      assert(
        runPython(
          `_Node(_code).find_function("binary_search").find_whiles()[0].find_body().find_ifs()[0].find_body().is_equivalent("return path_to_target")`
        )
      )
})

--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)
--fcc-editable-region--
        
--fcc-editable-region--