Searching
Every problem here is a binary search in some disguise: on a list, on a boundary, or on the range of possible answers. Before writing code, say out loud what is being halved and what condition flips from false to true.
The lesson is Searching.
Last occurrence
Write last_occurrence(items, target) that returns the index of the last target
in a sorted list that may contain repeats, or -1 if it is absent.
last_occurrence([1, 2, 2, 2, 3, 5], 2) -> 3
last_occurrence([1, 2, 2, 2, 3, 5], 4) -> -1
The point is to practise binary search that keeps going right after a match.
def last_occurrence(items, target):
...
Count in a sorted list
Write count_sorted(items, target) that returns how many times target appears in
the sorted list items.
count_sorted([1, 2, 2, 2, 3, 5], 2) -> 3
count_sorted([1, 2, 3], 4) -> 0
The point is to practise combining two boundary searches. One check uses a million
items and ten thousand targets; items.count(target) for each is ten billion steps.
def count_sorted(items, target):
...
The peak of a mountain
heights strictly increases up to one peak and then strictly decreases, with at least
three values. Write peak_index(heights) that returns the index of the peak.
peak_index([1, 3, 5, 4, 2]) -> 2
peak_index([0, 10, 5]) -> 1
The point is to practise binary search where the comparison is between a value and its neighbour rather than against a target.
def peak_index(heights):
...
Smallest in a rotated list
A sorted list of distinct numbers was rotated: some number of items were moved from the
front to the back, so [1, 2, 4, 7, 9] might have become [7, 9, 1, 2, 4]. Write
rotated_min(items) that returns the smallest value.
rotated_min([7, 9, 1, 2, 4]) -> 1
rotated_min([1, 2, 4, 7, 9]) -> 1 (rotated by zero)
rotated_min([3]) -> 3
The point is to practise binary search on a list that is sorted in two pieces.
def rotated_min(items):
...
Search a rotated list
A sorted list of distinct numbers has been rotated, as in the previous problem. Write
search_rotated(items, target) that returns the index of target, or -1.
search_rotated([4, 5, 6, 7, 0, 1, 2], 0) -> 4
search_rotated([4, 5, 6, 7, 0, 1, 2], 3) -> -1
The point is to practise working out which half is sorted and whether the target lies in it.
def search_rotated(items, target):
...
First true
flags is a list of booleans that is False for a while and then True for the rest,
like [False, False, True, True]. Either part may be empty. Write first_true(flags)
that returns the index of the first True, or len(flags) if there is none.
first_true([False, False, True, True]) -> 2
first_true([True]) -> 0
first_true([False, False]) -> 2
The point is to practise the boundary form of binary search, which is the template for every "search on an answer" problem.
def first_true(flags):
...
Whole cube root
Write cube_root_floor(n) that returns the largest whole number whose cube is at most
n, for n zero or more.
cube_root_floor(27) -> 3
cube_root_floor(30) -> 3
cube_root_floor(0) -> 0
cube_root_floor(10 ** 18) -> 1000000
The point is to practise binary search on an answer. Floating-point n ** (1/3) is off
by one for large n, so search over whole numbers.
def cube_root_floor(n):
...
Slowest speed that finishes in time
piles lists how many bananas are in each pile, and you have hours to eat them all.
Each hour you pick one pile and eat up to k bananas from it; if the pile has fewer than
k left you finish it and the rest of that hour is wasted. Write min_speed(piles, hours)
that returns the smallest k that lets you finish within hours. hours is at least
len(piles).
min_speed([3, 6, 7, 11], 8) -> 4
min_speed([30, 11, 23, 4, 20], 5) -> 30
min_speed([30, 11, 23, 4, 20], 6) -> 23
The point is to practise binary search on an answer with a helper that checks a guess.
def min_speed(piles, hours):
...
Closest value in a sorted list
Write closest(items, target) that returns the value in the non-empty sorted list
items closest to target. If two values are equally close, return the smaller.
closest([1, 4, 6, 9], 5) -> 4
closest([1, 4, 6, 9], 7) -> 6
closest([1, 4, 6, 9], 100) -> 9
The point is to practise using the insertion position: the answer is one of the two
values either side of where target would go.
def closest(items, target):
...
The missing number
items is sorted and contains every whole number from 0 to len(items) except one.
Write missing(items) that returns the missing number.
missing([0, 1, 2, 4, 5]) -> 3
missing([1, 2, 3]) -> 0
missing([0, 1, 2]) -> 3
The point is to practise binary search on a property: before the gap items[i] == i,
after it items[i] == i + 1.
def missing(items):
...
Next: Sorting.