18/30
Part 5 of 7 · No repeatsLearn and do
Skip the title shown last, and keep it waiting
The fix: when the best waiting title is the one shown on the call just before, show the next best instead. The skipped title doesn't lose its turn. It stays in waiting, by its score, and since it's still the best there, it shows on the very next call.
| Call | waiting before | Last shown | Shows |
|---|---|---|---|
| 6 | Tundra, Comet, Harbor, Maple, Orbit | Tundra | Comet |
| 7 | Tundra, Harbor, Maple, Orbit | Comet | Tundra |
| 8 | Harbor, Maple, Orbit | Tundra | Harbor |
So the run reads Comet, Harbor, Maple, Orbit, Tundra, Comet, Tundra, Harbor, Maple, Orbit. Every title still shows once per loop. Only the order inside the new loop changed.
Two details make the rule exact:
- Compare titles, not keys. The key stored for the last title may hold an old score. The title doesn't change.
- A lone title still shows. With one title in the catalog, no other title can take its place, so it shows every time. Without that case,
getwould have nothing to return.
The cost is small. At most one key is skipped, so get looks at two keys at most and stays O(1) beyond the pop.
The editor holds pick. Write pick(waiting, last) to return the index of the first key whose title isn't last, or 0 when no other title can show.