Python Lists and Tuples Quiz: Sorting, Grids and deque Test
Test your Python lists, tuples and sequences with 12 questions and three programs on copying, comprehensions, multi-key sorting, unpacking, grids and deque.
- Course: Python study plan
- Module: Lists, tuples and sequences
- Kind: Checkpoint — cleared at 70%
- Reading time: 25 min
- Runtime: CPython 3.11
Checkpoint — Lists, tuples and sequences is the checkpoint that closes the Lists, tuples and sequences module: a graded quiz and whole-program exercises, passed at 70%.
Instructions
This checkpoint covers the whole module: lists and their method costs, aliasing and shallow versus deep copies, comprehensions in list, set, dict and generator form, sorting with keys and stability and bisect, tuples and unpacking with namedtuple, grids with neighbours, transposes and the multiplication trap, and the sequence tools with deque and the sequence protocol.
How it works. Twelve questions and three programs. You need 70% on the questions and every program accepted to clear the module. You can retake it as often as you like; your best score counts.
Before you start, make sure you can answer these from memory:
- Which list operations are O(1) and which are O(n), and what replaces
pop(0)? - After
b = a.copy()for a list of lists, what is shared? - What is the difference between
[x for x in xs if c]and[x if c else y for x in xs]? - Why does
rows.sort(key=name); rows.sort(key=score, reverse=True)give score-then-name order? - What does
first, *rest = [1]bind? - Why is
[[0] * C] * Rwrong, and what is right? - Which two methods make a class usable with
len, indexing, iteration andin?
The three programs are an inventory that is edited, sorted by several keys and reported in aligned columns, a grid simulation that counts live neighbours with a direction list and a bounds check, and a sliding-window statistics tool built on a bounded deque.
Common questions
After b = a.copy() for a list of lists, what is shared?
The inner lists. copy() is shallow: b is a new outer list holding references to the same row objects, so b[0].append(9) also changes a[0]. Rebinding b[0] = [0] leaves a alone, and copy.deepcopy(a) shares nothing.
Why does sorting by name and then by score descending give score-then-name order?
Python's sort is stable, so the second sort, by score, keeps rows with equal scores in the order the first sort left them — alphabetical by name. Sorting by the secondary key first and the primary key last composes a multi-key order, even with reverse=True.
What does first, *rest = [1] bind?
first is 1 and rest is an empty list. A starred target collects whatever is left over as a list, and that list may be empty; Python raises ValueError only when a plain target has no value left to take.
Exercises
Inventory report
Keep an inventory from commands read until report: add name qty price adds an item (or replaces an existing one), remove name deletes it (unknown item if absent), restock name qty adds to its quantity (unknown item if absent). On report print the items ordered by quantity descending then name ascending, as a table: name left-aligned in 10, quantity right-aligned in 5, price right-aligned in 8 with two decimals, value (qty * price) right-aligned in 10 with two decimals; then TOTAL with the total value in the last column.
Input: commands. Output: the error lines as they occur, then the table.
add bolt 100 0.05
add nut 250 0.02
restock bolt 50
remove screw
report
prints
unknown item
nut 250 0.02 5.00
bolt 150 0.05 7.50
TOTAL 12.50Islands
Count the islands in an R × C grid of # (land) and . (water), where land cells are connected horizontally or vertically. Use an explicit stack (a list with append/pop) to flood each island from its first unvisited cell, with a direction list and a bounds check, and a visited grid built with a comprehension. Print the number of islands and the size of the largest.
Input: R C, then R rows. Output: islands <n> then largest <size> (0 when there is no land).
3 4
##..
#..#
..##
prints
islands 2
largest 3Live leaderboard
Scores arrive one per line as name score. Keep a list of (-score, name) tuples sorted at all times with bisect.insort, and after every arrival print the current top three names, comma-separated (fewer if there are fewer entries). A name may appear again with a new score: remove its old entry first.
Input: lines name score. Output: one line per input line.
ada 50
bob 70
cy 60
ada 80
prints
ada
bob,ada
bob,cy,ada
ada,bob,cyIn this module: Lists, tuples and sequences
- Lists — the mutable sequence
- Comprehensions — building collections from expressions
- Sorting — sort, sorted, keys, stability and bisect
- Tuples, unpacking and named tuples
- Grids and nested lists
- The sequence tools — enumerate, zip, reversed, any, all, deque and the protocol
- Checkpoint — Lists, tuples and sequences (this lesson)
← The sequence tools — enumerate, zip, reversed, any, all, deque and the protocol · Dictionaries — the mapping at the centre of Python →