3/28
Part 1 of 6 · The screen as a setLearn and do

A set answers "is it on the screen yet?" without a scan

Counting works when you have the whole screen. To fix the screen you go the other way: walk the slots in the order they load, and ask each title one question, is it on the screen already?

A seen-set answers that in O(1) on average. Make it once, before the loop over rows, and it remembers every row above:

seen = set()
for r, row in enumerate(rows):
    for j, title in enumerate(row[:width]):
        # check first
        if title in seen:
            # (r, j) repeats a title
            ...
        # then remember it
        seen.add(title)

Two lines decide whether this is right.

Where seen is made. Put seen = set() inside the row loop and it forgets every row above. It still catches a title twice in one row, so a test with a repeat inside one row passes. The page in the last step slips through.

Check, then add. Add first, and every title is "already there" when you check it.

Predict

A homepage has 40 rows on its first screen, 6 slots each. With a list as the seen-list, how many comparisons can checking the last slot take?

Pick an answer above to see the rest