Coding Exercises · Practice
Quiz & exercises
Answer each question to see the explanation. Your best score is saved in this browser.
Quiz
Coding exercises
Write your code, then press Check (or Shift+Enter) to run the tests. Drafts are saved as you type.
Exercise 1: Exercise 1a: merge with defaultdict ✓ Passed
Write merge(*dicts) that combines any number of word-frequency dictionaries into one plain dict with the total count of each word, sorted by count from highest to lowest. Words with equal counts keep the order in which they were first seen.
Accumulate the totals in a defaultdict. Don't modify the input dictionaries.
from collections import defaultdict
def merge(*dicts):
totals = defaultdict(int)
for d in dicts:
for word, count in d.items():
totals[word] = count
return totals
d1 = {'python': 10, 'java': 3, 'c#': 8, 'javascript': 15}
d2 = {'java': 10, 'c++': 10, 'c#': 4, 'go': 9, 'python': 6}
d3 = {'erlang': 5, 'haskell': 2, 'python': 1, 'pascal': 1}
def test_totals():
"""counts are added up across dictionaries"""
result = merge(d1, d2, d3)
assert result == {'python': 17, 'javascript': 15, 'java': 13, 'c#': 12, 'c++': 10,
'go': 9, 'erlang': 5, 'haskell': 2, 'pascal': 1}, f"got {result}"
def test_sorted():
"""the result is sorted by count, highest first"""
result = merge(d1, d2, {'modula-2': 100})
assert list(result) == ['modula-2', 'python', 'javascript', 'java', 'c#', 'c++', 'go'], f"order: {list(result)}"
def test_ties_stable():
"""equal counts keep first-seen order"""
assert list(merge({'a': 1, 'b': 2}, {'c': 2})) == ['b', 'c', 'a']
def test_plain_dict():
"""returns a plain dict"""
assert type(merge(d1)) is dict, f"returned {type(merge(d1)).__name__}"
def test_edge_cases():
"""no input gives {}, inputs are not modified"""
assert merge() == {}
before = dict(d1)
merge(d1, d2)
assert d1 == before, "an input dictionary was modified"
Hints (2)
totals[word] += countworks for new words too, because thedefaultdict(int)supplies0.sorted(totals.items(), key=lambda e: e[1], reverse=True)sorts the pairs by count;sortedis stable, so ties keep their order. Wrap it indict(...).
Show a solution
from collections import defaultdict
def merge(*dicts):
totals = defaultdict(int)
for d in dicts:
for word, count in d.items():
totals[word] += count
return dict(sorted(totals.items(), key=lambda e: e[1], reverse=True))Exercise 2: Exercise 1b: merge with Counter ✓ Passed
Solve the same problem with a Counter: merge(*dicts) returns a plain dict of total counts, sorted from highest to lowest (ties in first-seen order), without modifying the inputs.
Hint: a Counter's repr looks sorted, but its iteration order isn't.
from collections import Counter
def merge(*dicts):
totals = Counter()
for d in dicts:
totals.update(d)
return totals
d1 = {'python': 10, 'java': 3, 'c#': 8, 'javascript': 15}
d2 = {'java': 10, 'c++': 10, 'c#': 4, 'go': 9, 'python': 6}
d4 = {'modula-2': 100}
def test_totals():
"""counts are added up across dictionaries"""
assert merge(d1, d2) == {'python': 16, 'javascript': 15, 'java': 13, 'c#': 12, 'c++': 10, 'go': 9}
def test_sorted():
"""the result is really sorted, not just displayed sorted"""
result = merge(d1, d2, d4)
assert list(result)[0] == 'modula-2', f"order: {list(result)}"
counts = list(result.values())
assert counts == sorted(counts, reverse=True), f"order: {list(result)}"
def test_ties_stable():
"""equal counts keep first-seen order"""
assert list(merge({'a': 1, 'b': 2}, {'c': 2})) == ['b', 'c', 'a']
def test_plain_dict():
"""returns a plain dict"""
assert type(merge(d1)) is dict, f"returned {type(merge(d1)).__name__}"
def test_inputs_unchanged():
"""inputs are not modified"""
before = dict(d1)
merge(d1, d2)
assert d1 == before
Hints (2)
Counter.update(mapping)adds the counts, which is exactly the merge.most_common()with no argument returns every pair sorted by count. Pass it todict.
Show a solution
from collections import Counter
def merge(*dicts):
totals = Counter()
for d in dicts:
totals.update(d)
return dict(totals.most_common())Exercise 3: Exercise 2: count eye colors, zeros included ✓ Passed
Write count_eye_colors(persons, possible_eye_colors). Each person has an eye_color attribute. Return a mapping (a dict or Counter) with an entry for every color in possible_eye_colors, in that order, giving how many people have it. Colors nobody has must appear with 0.
People whose color isn't in possible_eye_colors are ignored.
from collections import Counter
def count_eye_colors(persons, possible_eye_colors):
return Counter(p.eye_color for p in persons)
class Person:
def __init__(self, eye_color):
self.eye_color = eye_color
colors = ("amber", "blue", "brown", "gray", "green")
people = [Person(c) for c in ['brown', 'gray', 'brown', 'green', 'brown']]
def test_counts():
"""counts each color"""
result = count_eye_colors(people, colors)
assert result['brown'] == 3 and result['gray'] == 1 and result['green'] == 1, f"got {dict(result)}"
def test_zero_entries():
"""colors nobody has are present with 0"""
result = count_eye_colors(people, colors)
assert 'amber' in result and result['amber'] == 0, f"got {dict(result)}"
assert 'blue' in result and result['blue'] == 0
assert len(result) == 5, f"expected 5 entries, got {len(result)}"
def test_order():
"""entries follow the order of possible_eye_colors"""
assert list(count_eye_colors(people, colors)) == list(colors)
def test_unknown_ignored():
"""colors outside the list are ignored"""
result = count_eye_colors(people + [Person('gren')], colors)
assert 'gren' not in result, "an unknown color was counted"
def test_nobody():
"""with no people, every color is 0"""
assert dict(count_eye_colors([], colors)) == dict.fromkeys(colors, 0)
Hints (2)
- A
Counteronly holds colors it has seen. Build the result by looping overpossible_eye_colorsinstead. - Looking up a missing key in a
Counterreturns0, so{color: counts[color] for color in possible_eye_colors}fills in the zeros and ignores unknown colors.
Show a solution
from collections import Counter
def count_eye_colors(persons, possible_eye_colors):
counts = Counter(p.eye_color for p in persons)
return {color: counts[color] for color in possible_eye_colors}Exercise 4: Exercise 3: layered settings with ChainMap ✓ Passed
common.json, dev.json and prod.json are in the working directory.
1. Write chain_recursive(d1, d2) that returns a ChainMap in which d1 overrides d2, at every level: whenever a key holds a dict in both d1 and d2, its value in the result must be a chain_recursive of the two sub-dicts. 2. Write settings(env) that loads <env>.json and common.json and returns chain_recursive(env_settings, common_settings).
Don't build merged copies of the data: the result and its nested sections must be ChainMap objects.
import json
from collections import ChainMap
def load_settings(env):
with open(f'{env}.json') as f:
return json.load(f)
def chain_recursive(d1, d2):
return ChainMap(d1, d2)
def settings(env):
return chain_recursive(load_settings(env), load_settings('common'))
from collections import ChainMap
def test_top_level():
"""environment values override, missing sections fall back to common"""
prod = settings('prod')
assert isinstance(prod, ChainMap), f"returned {type(prod).__name__}"
assert prod['logs']['level'] == 'info'
assert settings('dev')['logs']['level'] == 'trace'
def test_nested():
"""nested sections are chained too"""
prod = settings('prod')
assert isinstance(prod['database'], ChainMap), "database section is not a ChainMap"
assert prod['database']['user'] == '$PG_USER'
assert prod['database']['port'] == 5432, "database.port from common.json is missing"
def test_deeper_nesting():
"""two levels deep"""
dev = settings('dev')
assert dev['data']['numerics']['type'] == 'float'
assert dev['data']['numerics']['precision'] == 6
assert settings('prod')['data']['numerics']['type'] == 'Decimal'
def test_env_only_section():
"""a section that exists only in the environment file"""
assert dict(settings('dev')['data']['operators']) == {'add': '__add__'}
def test_chain_recursive_generic():
"""chain_recursive works on any nested dicts"""
a = {'x': {'y': {'z': 1}}, 'k': 'a'}
b = {'x': {'y': {'z': 2, 'w': 3}, 'v': 4}, 'k': 'b', 'only_b': 5}
c = chain_recursive(a, b)
assert c['x']['y']['z'] == 1 and c['x']['y']['w'] == 3 and c['x']['v'] == 4
assert c['k'] == 'a' and c['only_b'] == 5
def test_type_mismatch():
"""a dict that overrides a non-dict value simply wins"""
c = chain_recursive({'x': {'a': 1}}, {'x': 'flat'})
assert dict(c['x']) == {'a': 1}
Hints (2)
- A single
ChainMapreturns the whole sub-dict from the first map. Loop overd1.items()and look for values that are dicts in both maps. - For each such key, store
chain_recursive(d1[k], d2[k])in the chain (chain[k] = ...), so the nested lookup is layered too.
Show a solution
import json
from collections import ChainMap
def load_settings(env):
with open(f'{env}.json') as f:
return json.load(f)
def chain_recursive(d1, d2):
chain = ChainMap(d1, d2)
for k, v in d1.items():
if isinstance(v, dict) and isinstance(d2.get(k), dict):
chain[k] = chain_recursive(v, d2[k])
return chain
def settings(env):
return chain_recursive(load_settings(env), load_settings('common'))