A backup tool walks a network drive whose folder listings are slow: each listing takes a round trip to the server. It usually needs only the first few files, or the first file that is large enough, so it must not list folders it never reaches.
A Folder has one method, entries(), which returns the folder's contents as a list of (name, item) pairs sorted by name. An item is either another Folder or an int, the size of a file in bytes. Every call of entries() is a listing, and the drive counts them.
Write a generator function walk(folder) that yields (path, size) for every file below folder, in this order: go through the folder's entries in the order entries() gives them; a file is yielded when it is reached, and a subfolder is walked completely at its place in the order. A path joins the names from below folder down to the file with "/" (the starting folder's own name is not part of it). Call entries() exactly once for each folder you walk, and only when the walk reaches that folder.
Setup helpers you can use with Run: build(spec) turns nested dictionaries into folders, demo_drive(), random_drive(seed, folders, files), listings(folder) (the number of listings made so far on that drive), first_files(root, k) and first_large(root, min_size), which take part of your walk and report the listings it needed.
Examples
Input: first_files(demo_drive(), 3)
Output: ([("data/2025/feb.csv", 850), ("data/2025/jan.csv", 900), ("data/raw.bin", 4096)], 3)
Explanation: the root, "data" and "data/2025" were listed; "empty", "src" and "src/util" were not needed yet.
Input: first_files(demo_drive(), 10)
Output: ([("data/2025/feb.csv", 850), ("data/2025/jan.csv", 900), ("data/raw.bin", 4096), ("readme.md", 120), ("src/main.py", 300), ("src/util/io.py", 80)], 6)
Constraints
- At most 2,000 folders and 5,000 files; folders are nested at most 40 deep.
- An empty folder yields nothing but is still listed when the walk reaches it.
Goals
- Write a recursive generator that hands over a subtree's values with `yield from`
- Build each file's path while descending, without collecting lists
- Do no more work than the caller has asked for: a caller that stops early stops the walk