Binary search
When a list is already sorted, you can find a value without looking at
every item. Keep two positions, low and high, marking the
part of the list that could still contain the target; at first that is the
whole list. Look at the middle item. If it equals the target, you are done.
If it is smaller than the target, the target can only be to the right, so
move low to just after the middle. If it is larger, move high to just
before the middle. Repeat until the two positions cross, which means the
target is not there.
Write binary_search(sorted_items, target) that returns the position of
target in a sorted list with no repeated values, or -1.
binary_search([2, 5, 8, 12, 16, 23], 12) -> 3
binary_search([2, 5, 8], 6) -> -1
def binary_search(sorted_items, target):
...