Skip to the workspace
Pebellslearn by building
Recursion, Searching & Sorting  ›  Lesson

Recursion & Nested Playlists

Recursion 20 minA function that solves a problem by calling itself on a smaller piece

dsa / Recursion & Nested Playlists

Explore each scene at your own pace.

01 · Flatten a nested playlist

Flatten a nested playlist

Recursion & Nested Playlists

Definition
Recursion is a way of solving a problem by having a function call itself on a smaller version of the same problem, stopping at a base case small enough to answer directly.
In plain words
It is like opening a set of nesting dolls: you do one small step, hand the smaller doll to yourself, and keep going until you reach the tiniest one that cannot be opened. Then you work back out.

In short: a recursive function solves a problem by calling itself on a smaller version of the same problem. Every recursive function needs a base case (an input small enough to answer directly, no further call) and a recursive case (do a little work, then call itself on something closer to the base case). Each call gets its own private frame on the call stack — frames pile up on the way down and come off, in reverse order, on the way up. Skip the base case and nothing ever stops the calls.

A hand holding open folder "Road Trip" that contains folder "Night" which contains folder "Late", drawn as nested boxes opening into a tree with one arrow per level labelled "same function, smaller playlist"

Flatten a nested playlist

Tunebox lets people put playlists inside playlists: "Road Trip" holds "Morning" and "Night", and "Night" holds "Late". One loop handles one level of nesting; two nested loops handle two. Recursion handles any depth, because it hands every sub-playlist to itself:

python

The mistake people really make: leaving the base case out, or writing it so the recursive call never gets closer to it. Then nothing ever stops:

What happens when you run this?
def tracks_left(n):
    return 1 + tracks_left(n - 1)

print(tracks_left(3))

Flattening is the same shape, collecting titles instead of counting down. This version is one word away from right — a second real mistake:

broken — fix it

This should print one flat list of four titles. It prints the sub-playlist as a list nested inside the result.

Scene 1 of 8
Project context and further resources
You're building a piece ofTunebox — search & charts
This piece — flatten_playlist(): Playlists inside playlists — flattened to a running order.
Scenario A listener's "Road Trip" folder holds "Morning" and "Night" playlists, and "Night" holds "Late". Before Tunebox can press play it needs one running order — recursion walks the whole tree to build it.
flatten_playlist.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
flatten_playlist(node) → listEvery title from a nested playlist, flattened into play order.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.