Algorithms & Testing — Lesson 4: Build a data-informed event report
New skill: Counting, sorting, formatting and testing
Previously learned: Units 01–09
Why this matters
Real apps do more than count data: they communicate what happened in a format people can use.
Step 1 — Explore working Python
Read the program below. Before running it, write down the result you expect.
counts = {'jump': 1, 'run': 2}
parts = []
for key in sorted(counts):
parts.append(key + ':' + str(counts[key]))
print(' | '.join(parts))
Step 2 — Trace the code
- Build a frequency table while visiting the event list.
- Sort keys alphabetically for predictable output.
- Convert counts into strings and join labeled pieces using the exact separator.
Step 3 — Check your understanding
Predict how ['jump', 'run', 'run'] becomes the text jump:1 | run:2.
Step 4 — Watch for this mistake
Dictionary insertion order is not the same as alphabetical order. Explicitly sort keys for the report.
Practice: Build a data-informed event report
Complete event_report(events) to return alphabetically ordered label:count pairs separated by ` |
`. Return the empty string for an empty input. |
Function contract: event_report(events)
Starter file: main.py
Work in the editor, using Run to explore and Check to compare against the published examples. Your submitted code, not the public check, determines the recorded score.
Hint 1 — Start
Build a dictionary of counts using `.get()`.Hint 2 — Build
Use `sorted(counts)` to visit names alphabetically.Hint 3 — Test
Create strings `name + ':' + str(count)` and join them using `' | '`.Reflect
In 3–5 sentences, explain the algorithm you chose, one test case you predicted, and how you would detect an incorrect answer.
Optional reading
For another perspective on this skill, see How to Think Like a Computer Scientist (interactive textbook). This Playground lesson, worked example, and exercise are original; no textbook passages were reproduced.