Video lecture
Watch on YouTube(opens in a new tab)Sub topic 4
Stating the Problem Precisely: Inputs, Outputs and Conditions
This chapter assumes you can read simple Python functions and use the remainder operator %. It also builds on the previous chapter's point that a problem is separate from the algorithm that solves it. Before you can judge whether an algorithm is right, you need a precise statement of what "right" means. That statement is called a specification.
The Four Parts of a Specification
A specification describes a problem without saying how to solve it. It has four parts.
| Part | Question it answers | GCD example |
|---|---|---|
| Inputs | What values does the algorithm receive? | Two integers `a` and `b` |
| Outputs | What value does it produce? | One integer `g` |
| Precondition | What must be true of the inputs before it starts? | `a >= 0`, `b >= 0`, not both zero |
| Postcondition | What must be true of the output when it finishes? | `g` divides `a` and `b`, and no larger integer divides both |
The precondition is the caller's promise. The postcondition is the algorithm's promise. Together they form a contract: if the caller meets the precondition, the algorithm must deliver the postcondition. If the caller breaks the precondition, the algorithm owes nothing.
Why the Precondition Matters
Look at the GCD precondition. It excludes a = 0, b = 0 because every integer divides 0, so no greatest common divisor exists. It excludes negative numbers so the remainder behaves predictably. These exclusions are deliberate decisions. A missing precondition is a common source of bugs: the algorithm gets an input the author never considered and returns nonsense.
Writing the Postcondition as a Checkable Property
A strong postcondition describes the output by its properties, not by the steps that produce it. "Returns the result of repeated remainders" describes the method. "Returns the largest integer dividing both a and b" describes what the answer must be. Only the second lets you test any GCD algorithm against it.
You can turn a specification into executable checks:
def satisfies_precondition(a, b):
return a >= 0 and b >= 0 and not (a == 0 and b == 0)
def satisfies_postcondition(a, b, g):
if g <= 0:
return False
divides_both = a % g == 0 and b % g == 0
limit = max(a, b)
no_larger = all(not (a % d == 0 and b % d == 0)
for d in range(g + 1, limit + 1))
return divides_both and no_larger
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
for a, b in [(48, 18), (7, 0), (0, 5), (17, 13)]:
assert satisfies_precondition(a, b)
g = gcd(a, b)
print(a, b, g, satisfies_postcondition(a, b, g))Every line prints True. The checker is slow, but that doesn't matter: it states what correct output looks like, independent of Euclid's method.
Common Errors
- Postcondition too weak. "Returns a number that divides
aandb" is satisfied by always returning 1. - Postcondition restates the algorithm. If it says "returns what the loop computes", it cannot catch a wrong loop.
- Missing edge cases in the precondition. Empty lists, zero, and negative values are the usual ones to forget.
- Unstated output for impossible cases. Linear search must say what happens when the target is absent (return
-1).
Exercise
Write a specification for linear search: given a list items and a value target, return an index. Include inputs, output, precondition and postcondition, and make sure it handles a missing target and duplicate values.
Show answer
Inputs: a list items and a value target. Output: an integer i. Precondition: elements of items can be compared to target with == (an empty list is allowed). Postcondition: if target occurs in items, then items[i] == target and items[j] != target for every j < i, so i is the first match. Otherwise i == -1.
Exercise
A student specifies find_max with the postcondition "returns an element of xs". Why is this too weak, and how would you fix it?
Show answer
Returning xs[0] would satisfy it every time. A fixed version: the result m is an element of xs, and m >= x for every x in xs. The precondition must also require xs to be non-empty.