7/28
Part 2 of 6 · Filling a rowLearn and do

Fill the slots of one row left to right

One slot is best_free. A row is that, width times, with one change between slots: the title you just placed is on the screen now.

Call the part of a row on the screen its head.

Adding the pick to shown at once does two jobs. Rows below can't show it, which is the page rule. And the next slot of this row can't take it again, which is the row rule. With ["C", "C", "F"], slot 0 takes C, and the second C is then blocked for slot 1, so slot 1 takes F.

Stopping at the first slot that nothing fits is the decision about running out. If no title fits slot 1, slot 2 can't be filled either: nothing becomes free as you go right. So the row stops.

Here the shared shown set carries the screen from one row to the next:

shown = set()
for row in PAGE:
    print(fill_row(row, 2, shown))
['A', 'B']
['D', 'E']
['C', 'F']

Rows 0 and 2 keep their first two titles. Row 1 skips B and A and shows D and E.

The editor holds best_free from the last step. Write fill_row(row, width, shown): up to width titles, each the best one not in shown, added to shown as you place it.