Skip to content
Learn Python with BNHR

Collections

On this page
  1. Overview
  2. 3.1 · The oldest heads of household
  3. 3.2 · Add a household without changing the list
  4. 3.3 · The crops a barangay grows
  5. 3.4 · One line about a household
  6. 3.5 · A corrected record

Every exercise in 20_collections.ipynb, with the solution and why it works. Each answer stays closed until you swear you tried it first. Opening one changes nothing in your notebook; your check still has to go green from your own code. Read the whole notebook, with its lessons.

Exercise 3.1. The oldest heads of household

The task

Finish oldest(ages, n) so that it returns the n highest ages, from highest down, as a list. It must not change ages.

call returns
oldest([47, 18, 63, 35, 52], 2) [63, 52]
oldest([30, 30, 41], 3) [41, 30, 30]

If the check is red: common causes

  • The lowest ages: the list is sorted from lowest up. Add reverse=True.
  • None: .sort() sorts in place and gives back nothing. Use sorted(...), which gives back the new list.
  • One age too many or too few: a slice [:n] stops before position n, so it already holds exactly n values.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.2. Add a household without changing the list

The task

The enumerators keep a sorted list of household IDs. Finish with_household(ids, new_id) so that it returns a new sorted list with new_id added, and leaves ids as it was. The cell after Try it says whether your function leaves the list it is given unchanged; the check after that, whether its results are right.

call returns
with_household(["H001", "H004"], "H002") ["H001", "H002", "H004"]

If the check is red: common causes

  • Green, but the cell above it is red: the result is right, but ids was changed. Use ids + [new_id] instead of .append.
  • TypeError: can only concatenate list (not "str") to list: wrap the new ID in brackets, [new_id].
  • Not in order: return sorted(...) of the new list.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.3. The crops a barangay grows

The task

Each farming household reports its main crop. Finish distinct_crops(reported) so that it returns the crops reported, each once, in alphabetical order, as a list.

call returns
distinct_crops(["Palay", "Corn", "Palay", "Mango"]) ["Corn", "Mango", "Palay"]

If the check is red: common causes

  • {...} in curly brackets: that is the set itself. Return sorted(...) of it, which is a list.
  • Repeats in the result: the list was sorted, but never made into a set.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.4. One line about a household

The task

Finish summary(record) so that it returns a line such as H001: 5 members in Malanday, from a household's dictionary.

call returns
summary({"id": "H001", "barangay": "Malanday", "members": 5, ...}) "H001: 5 members in Malanday"

If the check is red: common causes

  • KeyError: a key is misspelt. The keys are "id", "members" and "barangay", all lower case.
  • NameError: name 'members' is not defined: the f-string uses {members}; inside the function the value is record["members"].

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Exercise 3.5. A corrected record

The task

A revisit finds a household has a different number of members. Finish corrected(record, members) so that it returns a new dictionary, the same as record but with members changed, and leaves record as it was.

call returns
corrected({"id": "H003", "members": 2, ...}, 3) {"id": "H003", "members": 3, ...}

If the check is red: common causes

  • Green, but the cell above it is red: the result is right, but the record given was changed. Build a new dictionary with {**record, "members": members}.
  • The old number of members: "members": members is written before **record, so the old value overwrites it. Put it after.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the common causes above cover most red checks. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Something new

Identify results
LayerExplore a stretch exercises
Stretch exercises S2Stretch: polars
AboutStretch exercises of Arrays & DataFrames, after module 6: polars exercises on the survey and the price files, each with its check and its solution on the companion.
CourseArrays & DataFrames, free