Can you tell what a school sports day app does by tracing three race times?
At your school sports day, an app takes three race times and prints just one number. It never tells you what that number means. Can you follow the instructions and work out what the app is reporting?
You know that a variable stores a value and an if test chooses a path. Store the first time in best. For each later time, replace best if the new time is smaller. Finally, output best. An algorithm is a sequence of steps for solving a problem. Here it’s written in pseudocode.
The first result is fifty-four seconds, so write fifty-four under best. The next result puts fifty-eight under time. Fifty-eight is higher than fifty-four, so best stays the same. Leave that cell blank. This is a trace table: it records initial values and then shows what changes as you follow the steps.
Try the same test with a different second result. Best starts at fifty-four seconds and the next time is fifty-seven seconds. What goes in the best column on that row? Pick an answer.
Decomposition means splitting the problem into jobs. This Python program implements the algorithm. Read the first time, then read and test two more, updating best when a time is smaller. Finally, print best. Leaving out runner names and lane numbers because they don’t affect the answer is abstraction.
Start with fifty-four in best. Set time to fifty-eight. Is fifty-eight less than fifty-four? No, so leave best blank on that row. Set time to fifty-one. Is fifty-one less than fifty-four? Yes, so record fifty-one under best. The loop ends and the algorithm outputs fifty-one seconds.
Now trace forty-eight, forty-five and forty-seven seconds. Start by recording forty-eight under best. Then test each remaining time in order. Which numbers should appear in the best column once you skip the blank cells? Pick an answer.
After you test fifty-eight seconds, the best cell is blank. If you read that blank as zero, the next test wrongly compares fifty-one with zero. Carry the last recorded value forward in your head. Fifty-one is less than fifty-four, so best changes to fifty-one.
Start by reading the first result into best. If another time remains, read it and check whether it’s less than best. If yes, update best. If no, skip the update. Both paths loop back to check for more times. When none remain, output best and stop.