Programming

Problem solving: a word counter

Turn a real problem into a program, step by step.

All lessons · Python lessons: learn from scratch

How to approach a problem

Before writing code, explain the task in plain sentences. Then split it into small steps and test each one separately. This turns a big program into something manageable.

  1. What is given and what is wanted? Write a sample input and the expected output.
  2. Solve it by hand and note the steps.
  3. Turn the steps into functions.
  4. Test on small examples, then try edge cases.

Example: a word counter

Task: count how many times each word appears in a text. Plan: lowercase the text, strip punctuation, split into words, count in a dict, show the most frequent.

def clean(text):
    keep = "abcdefghijklmnopqrstuvwxyz əöüğışç"
    return "".join(ch for ch in text.lower() if ch in keep)

def count_words(text):
    counts = {}
    for word in clean(text).split():
        counts[word] = counts.get(word, 0) + 1
    return counts

def top(counts, n=3):
    return sorted(counts.items(), key=lambda p: p[1], reverse=True)[:n]

sample = "The cat and the dog. The end!"
print(top(count_words(sample)))   # [('the', 3), ('cat', 1), ('and', 1)]

Each function does one job and can be tested alone. Extending the program is then easy: read from a file, save the result, skip very short words.

Finding bugs

When a program behaves unexpectedly, print the intermediate values and find where reality departs from your expectation. Small assert checks protect you when you later change a function.

assert count_words("a a b") == {"a": 2, "b": 1}
assert top({"x": 1, "y": 5}, 1) == [("y", 5)]
print("all checks passed")

Author: Nibras Code