7.4.2. Ch07 Homework#

7.4.2.1. Problem 1 — Word Frequency#

Write a function word_freq(text) that takes a string and returns a dictionary mapping each word (lowercased, stripped of punctuation) to the number of times it appears.

Test it on: "To be or not to be that is the question"

# Your solution here

7.4.2.2. Problem 2 — Dictionary Lookup#

Given this dictionary of course rosters:

rosters = {
    'CS101': ['alice', 'bob', 'carol', 'dave'],
    'DS201': ['bob', 'carol', 'eve', 'frank'],
}

Write code that builds a dictionary mapping each student name to the list of courses they are enrolled in.

# Your solution here

7.4.2.3. Problem 3 — defaultdict Grouping#

Given a list of (city, temperature) tuples, use defaultdict(list) to group temperatures by city, then compute the average temperature per city.

readings = [
    ('Rolla', 72), ('Springfield', 68), ('Rolla', 78),
    ('KC', 85), ('Springfield', 71), ('KC', 82), ('Rolla', 69),
]

# Your solution here