Objective
Create a Python program that looks for a number in a sorted list using binary search and counts how many steps it needs.
Instructions
- Read a line with integers sorted from smallest to largest and, on the next line, the number to look for.
- Use two indexes
left = 0andright = len(list) - 1. Whileleft <= right: add 1 to the steps, computemiddle = (left + right) // 2and compare. If it is the target, finish; if the middle element is smaller, doleft = middle + 1; if it is larger,right = middle - 1. - Show
Position: Pon the first line (orNot foundif it is not there) andSteps: Kon the second.
1 3 5 7 9 11 13
11
Position: 5
Steps: 2
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise