
Warning
This page shows last year’s version. We might still make small changes, but you’re welcome to take a look. We’ll remove this notice once the page is final.
Week 8: Closure#
Syllabus#
Dictionary, a collection of key-value pairs
Creating dictionaries: enclosing key-value pairs in curly brackets
The keyword
infor dictionaries operates on keys, not valuesTraversing a dictionary
Adding key-value pairs to the dictionary
Dictionaries are mutable
Tuple, an immutable sequence of values
Creating a tuple with or without parenthesis
Unpacking, i.e
a, b = my_function()Using a tuple as a return value
Checkpoints#
Checkpoint 8.1: Booklet Layout #
A booklet may be made by folding sheets of paper, as in the illustration below. When only one sheet of paper is used, the booklet has 4 pages. If two sheets are used, the booklet has 8 pages. Every additional sheet contributes with 4 pages. Therefore, the number of pages in a booklet is always a multiple of 4.
If we have a certain number of pages with content, and this number is not a multiple of 4, there will be up to 3 blank pages at the end of the booklet.

Given a number of pages with content to be placed in a booklet, we want to know two things:
the total number of pages in the smallest booklet that can accommodate the content,
the number of blank pages in such a booklet.
Write a function that takes as input the number of pages of content. The function should return the total number of pages in the smallest appropriate booklet, and the number of blank pages.
As an example, consider having 17 pages with content. Number 17 is not a multiple of 4, so pages need to be added. Adding one or two blank pages will not be enough, since neither 18 nor 19 are multiples of 4. Adding three blank pages will give 20, which is a multiple of 4. Therefore, the booklet has 20 pages, and there will be 3 blank pages. The desired output is shown in the code cell below.
>>> booklet_layout(17)
(20, 3)
The filename and requirements are in the box below:
booklet_layout.pybooklet_layout(content_pages)
Return the number of total and blank pages given content.
Parameters:
|
|
The number of pages with content. |
Returns:
|
The number of total pages and the number of blank pages. |
Use the following script to check your function test_booklet_layout.py. If your function fails the test in this script, it will also fail when you hand it in.
Checkpoint 8.2: Name Frequency #
Given a list of full names, we need to know how many times each first name occurs in the list. Here, the first name is the part of the full name before the first space.
Write a function that takes a list of full names as input. The function should return a dictionary where the keys are the first names from the list. The value of each key should be the number of times this first name occurs in the list.
As an example, consider the input below.
>>> names = ['Liv Ea Jensen',
... 'Mads Oliver',
... 'Steve Madsen',
... 'Anna Simon',
... 'Simon Gade',
... 'Mads Kai Jensen']
The first names are Liv, Mads, Steve, Anna, Simon, and Mads. The first name Mads occurs twice, and the other first names occur once. The function should therefore return the dictionary with keys and values as shown below.
>>> name_frequency(names)
{'Liv': 1, 'Mads': 2, 'Steve': 1, 'Anna': 1, 'Simon': 1}
The filename and requirements are in the box below:
name_frequency.pyname_frequency(names)
Return frequency of names in the list.
Parameters:
|
|
The names to analyze. |
Returns:
|
The frequency of names. |
Use the following script to check your function test_name_frequency.py. If your function fails the test in this script, it will also fail when you hand it in.