Skip to content
Learn Python with BNHR

Stretch · Get

On this page
  1. Overview
  2. S1.1 · A strict code
  3. S1.2 · Find the header row yourself
  4. S1.3 · What a careless join costs
  5. S1.4 · An API reader that cannot loop forever

Every exercise in 81_stretch_day1.py, 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.

Stretch S1.1. A strict code

The task

code_to_text pads whatever it is given. A strict version should refuse what cannot be a code: raise a ValueError with a clear message when the value (after removing spaces) is not made only of digits, is longer than width, or is a decimal that is not whole (12.5). Missing values still give None.

call gives
strict_code(100305000.0, 10) "0100305000"
strict_code("12A", 4) ValueError
strict_code("01234567890", 10) ValueError (11 digits)
strict_code(12.5, 4) ValueError

If the check is red

An empty text is not made of digits either.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Stretch S1.2. Find the header row yourself

The task

Exercise 3.1 used header=3 because you looked at the workbook. A pipeline that reads next quarter's edition should not depend on someone looking. Write find_header_row(path, first_heading): read the sheet with header=None, and return the position (from 0) of the first row whose first cell is first_heading. Raise a ValueError if there is none.

call gives
find_header_row(PATHS.reference / "psgc_2023Q4.xlsx", "Code") 3

If the check is red

Position from 0, and a ValueError when no row matches.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Stretch S1.3. What a careless join costs

The task

The careless join in section 3 copied the people of one city. How much does that change the weighted total? Write weighted_drift(round_df, code_list): the weighted total (sum of weight) after round_df.merge(code_list, left_on="area_code", right_on="code", how="left"), minus the weighted total before, rounded to a whole number. Use the code list with its duplicate (from your load_code_list).

The point of the exercise is the size of the number, compared with the region's population.

If the check is red

Only the people of the duplicated code are copied: their weights are counted twice.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. Open this when you have been stuck for ten minutes, or when your check is green and you want to compare.

Stretch S1.4. An API reader that cannot loop forever

The task

A broken API can name, as its next page, a page you have already read. Your Exercise 4.3 would then go round for ever. Write read_pages(folder): like read_api, but keep a set of the pages already read, and raise a ValueError naming the page when next points back to one. Return the records as a list of (region, year, value_text), in reading order.

If the check is red

Two records when the pages end; a ValueError when they loop.

The solution, and why it works

Try it yourself first: the check names the input that failed, and the check's hint above covers the usual slip. 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 module
ModuleStart here
AboutModule 1 of Arrays & DataFrames: the course folder ready, the farm survey read from its codebook, and one chart drawn three ways. Its sections, its notebooks to read on the web, and the companion's solutions and explanations.
CourseArrays & DataFrames, free