Capstone — Smart Shuffle
Definition
Smart Shuffle combines recursion to flatten nested playlists, sorting to order artists, and binary search insertion to keep a sorted queue, so the same artist never plays twice in a row whenever that is possible.
In plain words
It is a DJ who unpacks every folder, always plays from the artist with the most songs left, and never repeats an artist back to back unless there is no choice. Three earlier tools do the work behind that rule.
In short: this capstone chains everything in the section: recursion to
flatten nested playlists, sorting to put each artist's tracks and the
artist queue in order, and binary search (bisect.insort) to keep that
queue sorted as it changes. The payoff is Tunebox's Smart Shuffle: the
same artist never plays twice in a row when that's possible, and when it
isn't, it says exactly where the repeat has to happen.

Flatten the road-trip folder (recursion)
A playlist is a list of tracks — {"title": ..., "artist": ...} — and nested
sub-playlists, to any depth. Flattening is the nested-playlist walk from the
first lesson, with track dicts as the leaves:
python