CoreChallenge 17 of 34

Binary search

Write binary_search(sorted_list, target) that returns the index of target, or -1 if absent. Halve the search range each step; do not scan linearly.

Examples
binary_search([1, 3, 5, 7, 9], 7) → 3
Exercise

Binary search

5 checks
def binary_search(sorted_list, target):
    lo, hi = 0, len(sorted_list) - 1
    ...


print(binary_search([1, 3, 5, 7, 9], 7))
 
Python (loads on first run)Ctrl+Enter runs
Output
Press Run to try your code, then Check answer to grade it.