Problem solving: from a vague question to steps
Lesson 3 of 4
3Problem solving: from a vague question to steps
The big pictureA good programmer is not the fastest typist but the one who understands the problem before starting. Most wasted time comes from solving a question other than the one asked.
What the picture shows: Five stops before a problem counts as solved. Notice writing code comes last.
Map of this part
The concepts in order, click any one to jump to it:
Two ways into the same idea
Like planning a trip: you know the destination first (understand), split it into stops (break down), picture a full day (example), then ask "what if the flight is late?" (edge case). Only then do you book.
This is George Pólya's "How to Solve It" in programming terms: pin down inputs, outputs and constraints, decompose into small functions, derive the algorithm from hand-solved examples, list edge cases (empty, one item, negatives, maximum size), then verify with a trace table and then with tests.
Before You Start
📦Data made simple
You know lists and dictionaries.
🧩The three moves
Sequence, decision, repetition.
The Core Idea
1Understand: inputs, outputs, constraints
Restate the question in your own words. What goes in? What must come out? What is forbidden? Example: "give me the largest number in a list". Input: a list of numbers. Output: one number. Hidden question: what if the list is empty?
2Break down: a big problem = small problems
"Compute each student's average and show the best" = (1) for each student sum the marks, (2) divide by their count, (3) compare the averages. You solve and test each small part on its own.
3Examples and edge cases
Solve two normal examples by hand, then ask about the edges: an empty list, one item, all equal, negative numbers, a huge size. Edge cases are where most bugs hide.
def largest(numbers):
if not numbers: # edge case: empty
return None
best = numbers[0] # not 0: the list may be all negative
for n in numbers:
if n > best:
best = n
return best
print(largest([3, 9, 2]))
print(largest([-5, -2, -9]))
print(largest([]))best = 0. It works for positive numbers and silently fails on an all-negative list, returning 0, which is not even in it.4The trace table: run the code in your head
Write a column per variable and a row per loop turn. You run the algorithm by hand and record the values. When the table differs from what you expected, you have found the bug.
numbers = [3, 9, 2] turn | n | best -----+---+----- start | 3 1 | 3 | 3 2 | 9 | 9 3 | 2 | 9 -> returns 9
Worked example
Problem: is a word a palindrome (reads the same both ways, like "level")? Understand: text in, yes/no out. Examples: level → yes, hello → no. Edges: empty word, one letter, mixed case (Level). Algorithm: compare the first and last letters, then move in from both ends.
def is_palindrome(word):
word = word.lower()
left, right = 0, len(word) - 1
while left < right:
if word[left] != word[right]:
return False
left += 1
right -= 1
return True
for w in ["level", "hello", "Level", "", "a"]:
print(repr(w), is_palindrome(w))Myth vs Reality
| Myth | Reality |
|---|---|
| A good programmer writes the solution straight away. | Professionals spend longer understanding and on examples, so they write less code and fix less. |
| If it works on one example it is correct. | One example proves it works once. Edge cases prove it does not break. |
Test your understanding
A function returns the average of a list. Which edge case matters most?
Dividing by the count becomes division by zero. You must decide what to return.
What is a trace table for?
You record each variable after every step, so you see where behaviour drifted from what you expected.
What is the first step on a new problem?
Until you know what goes in and what must come out, any code you write is a guess.
Quick review cards
Check Your Understanding
Why solve examples by hand before coding?
Because the steps you take by hand are the algorithm itself. You then write them down instead of inventing them at the editor.
Name three edge cases for a function that reverses text.
An empty string, one character, text with spaces, symbols or Arabic letters.
Why is best = numbers[0] better than best = 0?
Because starting from a real item works for any numbers, while 0 may be larger than every negative item.
Resources for this lesson
Grouped by type. Start with the official docs for precision, or an article or video for the explanation.
Done with this lesson?
Sign in to save your progress.