Deliver the answer: top-k rankings, ASCII bar charts, polished summary lines — and rounding that stays honest.
01
Learning objectives
Produce a top-k ranking by sorting records with a key function and slicing.
Build an ASCII bar chart where each row prints a label, a bar and a value.
Write a one-line summary sentence combining computed statistics.
Apply honest rounding: full precision while computing, one round at display.
Choose between a table, a ranking and a chart for a given question.
02
Lesson notes
Numbers computed are not numbers communicated. The last mile of analysis is reporting: rank the records (who are the top three?), draw the shape (which day sold most?), and say the conclusion in one sentence a principal could read aloud. A perfectly correct mean buried in bare printouts reads as half an answer. Weak reporting loses marks on otherwise-correct analysis — examiners explicitly look for labelled, readable output in long questions.
Top-k is sorting plus slicing. Sort the records by the field of interest with key=, set reverse=True for descending order, then take the first k with a slice: sorted(scores, key=score_of, reverse=True)[:3]. The key function extracts the field to rank by; the slice does the top part. Stability guarantees that ties keep their original order — worth a mark when a question asks about equal scores.
The ASCII bar chart turns a frequency table into a picture using nothing but strings: each row prints the label, then '*' * count, then the number. Rows align because each occupies its own line, and the eye compares lengths instantly. For big values, scale — one star per 5 units — and say so in the output, because an unscaled chart of 500-item days is useless.
Rounding has an honesty protocol. Compute with full precision; round exactly once, at the moment of display, with round(value, 1) or round(value, 2). Rounding at every step accumulates error into the answer itself, while showing 87.456789012 pretends a precision the data never had. How many decimals you show is itself a claim about how sure you are. Pick decimals that match the data: whole marks need none, percentages one, averages two.
The summary sentence closes the report: combine your computed values into plain English — 'Average: ' + str(round(average, 1)) — or print the parts separated by commas. Check that the sentence claims only what the data shows: highest on Wednesday must mean Wednesday actually holds the max. Board tip: the long 'write a program' question usually wants exactly this shape — read, compute, sort, print — so rehearse the whole chain on paper.
03
Worked code examples
The top-k ranking
reverse=True ranks highest first, and the slice [:2] keeps only the top two — Bilal with 95 and Hamza with 91.
def score_of(record):
return record["score"]
scores = [
{"name": "Ayesha", "score": 88},
{"name": "Bilal", "score": 95},
{"name": "Sana", "score": 72},
{"name": "Hamza", "score": 91},
]
top = sorted(scores, key=score_of, reverse=True)[:2]
for s in top:
print(s["name"], s["score"])
Output
Bilal 95
Hamza 91
An ASCII bar chart
String repetition turns each count into a bar of stars; the label and the number make every row self-explanatory without a legend.
sales = {"Mon": 5, "Tue": 3, "Wed": 8}
for day in sales:
print(day, "*" * sales[day], sales[day])
Output
Mon ***** 5
Tue *** 3
Wed ******** 8
A summary line with honest rounding
The mean keeps full precision through the division and is rounded only once, at printing — 157 over 5 is exactly 31.4.
temps = [31, 29, 34, 30, 33]
average = sum(temps) / len(temps)
print("Average:", round(average, 1))
print("Hottest:", max(temps), "Coolest:", min(temps))
Output
Average: 31.4
Hottest: 34 Coolest: 29
04
Key terminology
top-k
The k best records, produced by a descending sort followed by a slice [:k].
key function
The function passed as key= that extracts the field a sort ranks by.
ASCII bar chart
A text chart where each row shows a label, a bar of repeated characters and the value.
scaling
Compressing large values onto the chart, e.g. one star per 5 units, with the scale stated.
honest rounding
Keeping full precision during calculation and rounding exactly once, for display.
05
Common misconceptions
Common mistake
Round every intermediate result to keep the numbers tidy.
Correct understanding
Each premature round injects error that the final answer inherits. Keep full precision; round once, for display.
Common mistake
A bar chart must print exactly as many stars as units.
Correct understanding
Scaling (1 star = 5 units) is standard for large values — as long as the chart states its scale, the picture stays truthful.
Common mistake
sorted(data)[:k] gives the top-k directly.
Correct understanding
The default sort is ascending, so that gives the smallest k. Use reverse=True before slicing to get the top.
06
Check your understanding
1. '*' * 4 produces —
2. After sorted(scores, key=score_of, reverse=True), which slice gives the top 3 records?
3. average = 157 / 5 is exactly 31.4. Which output line reports it most honestly?