15/28
Part 4 of 6 · Rows and columnsLearn and do

The screen is a grid of rows and columns

Q-nfx-063 puts the rule another way: within the first screen, no row and no column may hold a repeated show. Column j is slot j of every row, top to bottom.

Read literally, that is weaker than level 1. A title may appear twice on the screen, as long as the two copies share no row and no column:

Two screens, width 2
passes the grid rule
01row 0ABrow 1BA
breaks it
01row 0ABrow 1AC

On the left A and B each appear twice, in different rows and columns. On the right, column 0 holds A twice.

In an interview, ask which one they mean before you code. A product reason for the grid version: the eye scans across a row and down a column, so a repeat there is what a viewer notices.

Rows can be short, so the columns are ragged. That rules out the usual trick for columns, zip(*screen), which stops at the shortest row:

screen = [["A"], ["B", "C"], ["D", "C"]]
print(list(zip(*screen)))
print([row[1] for row in screen if 1 < len(row)])
[('A', 'B', 'D')]
['C', 'C']

zip drops column 1 entirely, along with the repeated C in it. Build each column with a comprehension that skips the rows too short to have that slot.

In the editor, write column(screen, j) and grid_ok(screen), which says whether every row and every column is free of repeats.