From a messy request to an algorithm
By UK GCSE Computer Science · independent study · myAudience

Original AI-generated teaching raster via built-in image_gen; author inspected 2026-10-04. · Generated project artwork; no copied exam or publisher illustration. Independent rights/publication review pending.
From a messy request to an algorithm
Suppose a fictional library wants to calculate how many shelf labels to print. “Count the books” is too vague: damaged copies may not need labels, each shelf has a capacity, and an empty shelf needs no label. Computational thinking begins by choosing an explicit question and a contract. Here the input is a non-negative whole number of usable books and a positive whole shelf capacity. The output is the smallest number of shelves able to hold them. We deliberately ignore authors, cover colours and borrowing histories because they cannot change this answer. That choice is abstraction, not a claim that those details never matter. Entry skills are whole-number division and interpreting a remainder; no prior programming is assumed.
Decomposition gives named tasks: validate the two quantities, calculate whole shelves and any remainder, then present the result. Each part has an identifiable purpose and can be tested separately. An algorithm is the finite procedure that solves the specified problem; a Python program is one implementation of it. The same algorithm could be represented in prose, a flowchart or pseudocode. A flowchart here would have input, a validity decision leading to rejection or calculation, a remainder decision, and an output. Every decision exit needs a label. A diagram containing only “process books” hides the very reasoning we need to review.
Divide book count by capacity to get quotient q and remainder r. If r is zero, q shelves suffice; otherwise one additional shelf is necessary. The justification is stronger than a sample: q full shelves hold q times capacity books, and the remaining r is strictly between zero and capacity. One more shelf therefore holds the remainder, while q shelves cannot. This assumes books are indivisible, all shelves have the same usable capacity, and no reserved shelf must stay empty. A changed assumption changes the problem, even when the arithmetic code still runs.
The implementation uses parameters rather than an unseen global count. Its local quotient and remainder exist within the call; return passes the answer to the caller. Raising ValueError is a deliberate failure result for an invalid quantity, rather than quietly computing a negative number of shelves. For this first example the caller must supply integers: type checking is added later. A docstring records units and assumptions, while meaningful names make the correspondence to the algorithm visible. Neither a comment nor a passing example proves correctness for every input.
Trace 25 books with capacity 12: q becomes 2, r becomes 1, the decision chooses 3. Trace zero books: q and r are zero, giving zero. Before running a program, predict these values in a table of parameters, intermediate state and return value. The generated accumulator visual supports the broader idea that state must have a defined meaning; it does not depict this shelves calculation. The later lessons use that same discipline to explain loops, search and compression without treating code syntax as the algorithm itself.
Runnable original Python 3 example (also saved verbatim in examples for CLI execution):
```python
def shelves(books, capacity):
"""Return shelves for integer book counts; capacity is books per shelf."""
if books < 0 or capacity <= 0:
raise ValueError("invalid shelf quantities")
quotient = books // capacity
remainder = books % capacity
return quotient + (1 if remainder else 0)
assert shelves(25, 12) == 3
assert shelves(24, 12) == 2
assert shelves(0, 12) == 0
```
Worked example: A label printer batches 49 labels in groups of 20. Give an algorithm and justify the result.
Define labels as indivisible and batch capacity as 20; reject negative count or nonpositive capacity.
49 = 2 × 20 + 9. Two full batches leave nine labels.
Choose three batches because two hold only 40 but three hold 60.
Answer: Three batches; the last has nine labels, not twenty.
Check your understanding: Transfer: shelves can now be of capacity 8 or 12, with at most two 12-capacity shelves available. Find the minimum number for 35 books, justify optimality, and explain why the original function is insufficient.
Answer: Use two 12-capacity shelves and two 8-capacity shelves: capacity 40 in four shelves. Three shelves can hold at most 12+12+8=32, so four is minimal. A single uniform capacity parameter cannot express limited mixed shelf types.
Why: This introduces a resource constraint rather than a cosmetic change in numbers. The answer must exhibit a feasible arrangement and a lower bound. Trying shelves(35,12) ignores the inventory and produces an infeasible claim.