Python, Concept by Concept › Checkpoint 04: Dictionaries & Collections
Questions: Checkpoint 04: Dictionaries & Collections
Complete all seven questions. Define each requested function, return its result, then use the worked calls to check your work. Use only the forms named in each specification. Do not use input().
Each question ends with a Real version note showing how the program can read its values with input().
Question 1: Build a Trail Lookup
Specification: Define trail_distance(name). Inside the function, build the dictionary {'Creek': 3, 'Hill': 5, 'Lake': 8}. Return the integer stored at key name with square-bracket access. Each sample name exists in the lookup.
Worked sample calls:
trail_distance("Creek") -> 3
trail_distance("Lake") -> 8
trail_distance("Hill") -> 5
Real program
The real program reads a trail name on one line with input().
How this is checked: Checked by tests that run your function trail_distance.
Question 2: Default Locker Count
Specification: Define locker_count(lockers, item). Return lockers.get(item, 0) so a missing item gives the integer 0. Do not change lockers.
Worked sample calls:
locker_count({"balls": 6, "cones": 9}, "cones") -> 9
locker_count({"balls": 6, "cones": 9}, "bibs") -> 0
locker_count({}, "balls") -> 0
Real program
The real program reads a count of locker entries, that many item-count lines, and an item name on the final line with input().
How this is checked: Checked by tests that run your function locker_count.
Question 3: Color Frequency Tally
Specification: Define color_counts(colors). Start with an empty dictionary. Loop directly with for color in colors:. If color in counts, assign counts[color] = counts[color] + 1. Otherwise assign counts[color] = 1. Return counts.
Worked sample calls:
color_counts(["red", "blue", "red"]) -> {'red': 2, 'blue': 1}
color_counts(["gold", "gold", "green", "gold"]) -> {'gold': 3, 'green': 1}
color_counts([]) -> {}
Real program
The real program reads space-separated colors on one line with input().
How this is checked: Checked by tests that run your function color_counts.
Question 4: Inventory Total
Specification: Define inventory_total(stock). Start total at 0. Loop with for item, amount in stock.items():. Add each amount to total, then return the final total.
Worked sample calls:
inventory_total({"notebooks": 4, "pencils": 10, "erasers": 3}) -> 17
inventory_total({"folders": 8}) -> 8
inventory_total({}) -> 0
Real program
The real program reads a count of stock entries followed by that many item-count lines with input().
How this is checked: Checked by tests that run your function inventory_total.
Question 5: Most Common Choice
Specification: Define most_common(counts) for a non-empty dictionary of non-negative integer counts. Start best_key = "" plus best_count = -1. Loop over counts.items(). When the current count is strictly greater than best_count, replace both best values. Return best_key. Every sample has one unique largest count.
Worked sample calls:
most_common({"robot": 5, "rocket": 8, "maze": 3}) -> "rocket"
most_common({"sun": 1, "moon": 4}) -> "moon"
most_common({"solo": 6}) -> "solo"
Real program
The real program reads a count of choices followed by that many item-count lines with input().
How this is checked: Checked by tests that run your function most_common.
Question 6: Level Lookup Message
Specification: Define level_message(level_text). Convert the parameter with level = int(level_text). Build points = {1: 4, 2: 7, 3: 10}, then read score = points[level]. Use one if/elif/else ladder: label score 10 as "Gold", score 7 as "Silver", then use "Bronze" for the remaining score. Return an f-string in the form "label: score". Save one call in message after the function definition, then display it with print(f"Result: {message}").
Worked sample calls:
level_message("3") -> "Gold: 10"
level_message("2") -> "Silver: 7"
level_message("1") -> "Bronze: 4"
Expected printed line for level 3:
Result: Gold: 10
Real program
The real program reads a level number on one line with input().
How this is checked: Checked by tests that run your function level_message.
Question 7: First-Word Report
Specification: Define first_word_report(words) for a non-empty list. Build a frequency dictionary by looping directly with for word in words: plus the explicit missing-key if/else pattern from Question 3. After that loop, read the word by position with first = words[0]. Return an f-string in the form "first: its_count of total_count"; use counts[first] for its count plus len(words) for the total.
Worked sample calls:
first_word_report(["map", "star", "map"]) -> "map: 2 of 3"
first_word_report(["red", "blue", "green", "red"]) -> "red: 2 of 4"
first_word_report(["solo"]) -> "solo: 1 of 1"
Real program
The real program reads space-separated words on one line with input().
How this is checked: Checked by tests that run your function first_word_report.