Syllabus Lesson 135 of 141 · Ten doors and the field kit
Ten doors and the field kit

Final 4: the missing row

AI off This is a final exam, matched to the baseline exam you did in the first module: the same kind of problem, new data, the same difficulty. The tutor is switched off and there are no hints. Work alone. The course lines it up against your baseline in the next lesson.

An admin panel pages through database tables. A user reports that the last row of a table never shows up. The ticket includes one example; the code is below.

def page_ranges(total, per_page):
    pages = []
    for p in range(total // per_page):
        pages.append((p * per_page + 1, (p + 1) * per_page))
    return pages

page_ranges(10, 3)   # [(1, 3), (4, 6), (7, 9)], expected (10, 10) as well
Your turn

Fix page_ranges(total, per_page) in the starter. It returns the pages of a list of total items (a whole number, 0 or more) as (first, last) item numbers, counting from 1, with per_page (at least 1) items per page. The rules: every item is on exactly one page, there are no empty pages (so 0 items means no pages), and only the last page may be short. The bug report says page_ranges(10, 3) misses item 10. The hidden tests try hundreds of totals and page sizes, and also very large tables: total can be as large as 1018.

AI off: no tutor, hints or worked solution on this one. Your own thinking only.

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.