Practice: lists, sets, and dictionaries#

This lab introduces no new concept. It practises the patterns from Lists, loops, conditionals, and functions and Sets, dictionaries, and data organization, one at a time.

Most exercises use the same small catalogue of books. Each book is a list with three items: title, author, and year of publication.

books = [
    ["Mrs Dalloway", "Virginia Woolf", 1925],
    ["Le città invisibili", "Italo Calvino", 1972],
    ["Pride and Prejudice", "Jane Austen", 1813],
    ["Il nome della rosa", "Umberto Eco", 1980],
    ["Frankenstein", "Mary Shelley", 1818],
    ["To the Lighthouse", "Virginia Woolf", 1927],
    ["Die Verwandlung", "Franz Kafka", 1915],
    ["Gulliver's Travels", "Jonathan Swift", 1726],
    ["Se una notte d'inverno un viaggiatore", "Italo Calvino", 1979],
    ["Orlando", "Virginia Woolf", 1928],
    ["Le Comte de Monte-Cristo", "Alexandre Dumas", 1844],
]

Part 1: Loops that build a result#

Many functions follow the same shape: create an empty result before the loop, update it inside the loop, and return it after the loop.

Exercise 1.1: Count the older books#

Write a function count_before(books, year) that returns how many books were published before year.

print(count_before(books, 1900))  # Expected: 4
print(count_before(books, 1800))  # Expected: 1

Exercise 1.2: Collect the titles of an author#

Write a function titles_by(books, author) that returns a list with the titles of all the books by author, in catalogue order.

print(titles_by(books, "Umberto Eco"))      # Expected: ['Il nome della rosa']
print(titles_by(books, "Dante Alighieri"))  # Expected: []

Exercise 1.3: Find the oldest book#

Write a function oldest_book(books) that returns the title of the oldest book, without using min().

print(oldest_book(books))  # Expected: Gulliver's Travels

Exercise 1.4: Find the bugs#

This function should count the books published before year, but count_before(books, 1900) returns 0. Find the two bugs.

def count_before(books, year):
    for book in books:
        count = 0
        if book[2] < year:
            count += 1
        return count

Exercise 1.5: Predict the output#

What does this code print?

line = "Nevermore, said the raven."
for char in ",.":
    line.replace(char, "")
print(line)

Part 2: Sets#

Exercise 2.1: Find the missing letters#

A pangram is a sentence that uses every letter of the alphabet. A lipogram does the opposite: it leaves out one or more letters on purpose, like Georges Perec’s novel La Disparition (1969), written without the letter “e”.

Write a function missing_letters(text) that returns, in alphabetical order, the letters of the English alphabet that do not appear in text, ignoring case.

print(missing_letters("vedrò compirsi il miracolo: il nulla alle mie spalle, il vuoto dietro di me, con un terrore di ubriaco."))
# Expected: ['f', 'g', 'h', 'j', 'k', 'q', 'w', 'x', 'y', 'z']
print(missing_letters("Nevermore, said the raven."))
# Expected: ['b', 'c', 'f', 'g', 'j', 'k', 'l', 'p', 'q', 'u', 'w', 'x', 'y', 'z']

Exercise 2.2: The anagram trap#

Two words are anagrams when they use the same letters the same number of times, like “roma” and “amor”. Write a function is_anagram(word1, word2) that returns True when the two words are anagrams and False otherwise.

print(is_anagram("roma", "amor"))  # Expected: True
print(is_anagram("roma", "rome"))  # Expected: False
print(is_anagram("anna", "nan"))   # Expected: False

Part 3: Dictionaries#

Exercise 3.1: Look up a value#

This dictionary stores the birth year of each author:

birth_years = {
    "Jonathan Swift": 1667,
    "Jane Austen": 1775,
    "Mary Shelley": 1797,
    "Alexandre Dumas": 1802,
    "Virginia Woolf": 1882,
    "Franz Kafka": 1883,
    "Italo Calvino": 1923,
    "Umberto Eco": 1932,
}

For every book in the catalogue, print the title and the approximate age of the author at publication.

# Expected (first three lines):
# Mrs Dalloway: Virginia Woolf was about 43
# Le città invisibili: Italo Calvino was about 49
# Pride and Prejudice: Jane Austen was about 38

Exercise 3.2: Count with a dictionary#

Write a function count_by_author(books) that returns a dictionary from each author to the number of their books.

print(count_by_author(books))
# Expected: {'Virginia Woolf': 3, 'Italo Calvino': 2, 'Jane Austen': 1, 'Umberto Eco': 1, 'Mary Shelley': 1, 'Franz Kafka': 1, 'Jonathan Swift': 1, 'Alexandre Dumas': 1}

Exercise 3.3: Group items into lists#

Write a function titles_by_author(books) that returns a dictionary from each author to the list of their titles, in catalogue order.

groups = titles_by_author(books)
print(groups["Virginia Woolf"])  # Expected: ['Mrs Dalloway', 'To the Lighthouse', 'Orlando']
print(groups["Mary Shelley"])    # Expected: ['Frankenstein']

Exercise 3.4: Find the most prolific author#

Start from the counts of Exercise 3.2:

counts = count_by_author(books)

Write a function most_prolific(counts) that returns the author with the most books, without using max().

print(most_prolific(counts))  # Expected: Virginia Woolf