33/42
Part 7 of 8 · The worked exampleLearn and do
Check it against trying every set, then give the cost
On 3,000 random cases of up to 8 shifts, the greedy matched trying every set each time. Longest first got 129 of them wrong, and taking whatever extends got 364 wrong.
The sort is O(n log n) and the walk is O(n). Here is a measurement on n random shifts, each starting on a random day from 1 to n and lasting 5 to 30 days, plus a shift (1, 30). Covering days 1 through n took 2.93 ms for 10,000 shifts. It took 45.17 ms for 100,000 shifts, and 27.11 ms of that was the sort.
In the editor, write first_mismatch(fast, slow, cases): the twin check that compared each fast version above with a slow one. In the example, abs and lambda x: x stand in for the two, and they first differ on (-2,).