CJCoding With Joseph
15free / daySign inCreate account

Recursive Binary Search

Write binary_search(values: list[int], target: int, low: int, high: int) -> int that returns the index of target in a SORTED list, or -1 when it is absent, searching only the range from index low to index high inclusive, by recursion: when low is greater than high the range is empty, so return -1; otherwise compute the middle index mid as (low + high) // 2; if values[mid] equals target return mid; if it is less than target search the right half (from mid + 1 to high); otherwise search the left half (from low to mid - 1). Do NOT write a main block or any top-level test code; the tests construct the calls with low 0 and high len(values) - 1. Input: two lines, the sorted list as integers separated by spaces (it may be empty) and then the target, which the tests read and pass to your function before printing the value it returns.

Expected Output:

3

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Recursion
📥 Auto-Input:
1 3 5 7 9 11\n7\n
This input is automatically fed to your program's stdin
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Ctrl+Space to expand shortcuts (pr, inp, def)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.