Syllabus Lesson 80 of 141 · Bugs that lie
Bugs that lie

Off by one, in disguise

Why it matters When only one case looks broken, it is tempting to treat that case as special. Usually it is just the case somebody looked at.

An admin page shows a CSV export one page at a time. The first row of the CSV is the header. A support ticket says page 1 is missing a person:

rows = [["name", "city"], ["Ana", "Leeds"], ["Ben", "York"], ["Cai", "Bath"], ...]
get_page(rows, 1, 3)
# expected [["Ana", "Leeds"], ["Ben", "York"], ["Cai", "Bath"]]
# got      [["Ana", "Leeds"], ["Ben", "York"]]

The header row makes page 1 look like the odd one out. Before you agree, work out what page 2 returns.

Your turn

Fix get_page(rows, page, size). rows[0] is a header row and is never returned; pages start at 1 and hold size data rows each (the last may hold fewer, and pages past the end are empty). The hidden tests check every page of 80 tables with different sizes, and that reading the pages in order gives every data row exactly once.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.