wk02.p3Practice exerciseWeek 02 · Organizing dataOpen · due in week 2

Word frequency counter

Count how many times each word appears in a sentence. The classic exercise that puts dictionaries to honest work.

What you'll build

Count how many times each word appears in a sentence. The classic exercise that puts dictionaries to honest work.

Requirements

The must-do parts. If any are missing, we'll ask you to take another pass.

  1. Start with this sentence: "the rain in spain falls mainly on the plain and the plain is wide".
  2. Split it into words with .split() and build a dictionary mapping each word to how many times it appears.
  3. Print the dictionary in a readable way — one word: count pair per line.
  4. Print the top three most common words by count.
Bonus, if you're feeling brave
  • Take the sentence from input() instead of hard-coding it.
  • Lowercase everything before counting so "The" and "the" are treated the same.

Examples

What your program should look like when it runs. Lines starting with $ are typed by you; the rest is your program.

Example output (partial)
the: 3
rain: 1
in: 1
spain: 1
...

Top 3:
  the (3)
  plain (2)
  rain (1)

Where to start

Copy this scaffold into a new file. You don't have to use it — it's just a friendly nudge.

word_counter.py
sentence = "the rain in spain falls mainly on the plain and the plain is wide"
words = sentence.split()

counts: dict[str, int] = {}
# TODO: loop over words and fill in counts

# TODO: print each word and its count

# TODO: print the top three words by count

How we'll grade it

Four checks, four points. Three or above is passing — we'll ask you to revise anything we can't tick.

CheckWhat we look forPt
It runsTop to bottom, no errors.1
Counts correctly`the` shows up 3 times, `plain` shows up 2.1
Readable outputEach `word: count` pair is on its own line.1
Top threeThe top three words by count are printed clearly at the end.1
Ready?
Hand it in
You can submit a draft and revise later if you're not done.
Begin submission →