CJCoding With Joseph
15free / daySign inCreate account

Count Binary Search Steps

Write binary_search_steps(values: list[int], target: int) -> int that runs a binary search on the sorted list but returns how many times a middle element was examined, whether or not the target was found: keep low, high and a steps counter, and while low is at most high compute mid with (low + high) // 2, add one to steps, and either return steps on a hit, move low to mid + 1 when the middle is too small, or move high to mid - 1. Return steps after the loop too. Sixteen values never take more than five steps, which is the O(log n) promise made visible. Do NOT write a main block or any top-level test code; the tests construct the calls. Input: two lines, a space-separated sorted list of integers (possibly empty) and then the target; the tests print the count your function returns.

Expected Output:

5

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

Hints4 available
Topics:
Data Structures & AlgorithmsBig-O
📥 Auto-Input:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16\n16\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.