3
Say it in fewer steps
Computing 3: Unit 1, pp. 8–27
GoalMake an algorithm shorter by joining steps that are the same and next to each other.
New words
repeat · to do the same thing againconcise · short, with nothing extraefficient · doing a job with the fewest steps
Explanation
Look at this algorithm: clap, clap, clap, nod, nod. Some steps are the same and come one after another. We can join them: “Clap 3 times. Nod 2 times.” It does the same job, but it has 2 steps instead of 5. A short algorithm is called concise. An algorithm that does the job in the fewest steps is efficient. It is quicker to write and easier to check. Take care: you can only join steps that are the same and side by side.
| Long algorithm | Short algorithm |
|---|---|
| Clap | Clap 3 times |
| Clap | Nod 2 times |
| Clap | |
| Nod | |
| Nod |
Worked examples
A floor robot is told: forward, forward, forward, forward. All four steps are the same, so we write “Forward 4 times”, or F4. Four steps become one.
| Long | Short |
|---|---|
| Stamp | Stamp 2 times |
| Stamp | Jump |
| Jump | Stamp |
| Stamp |
An artist draws a star three times and then a heart. The long algorithm has 4 steps. The short one is: Draw star 3 times. Draw heart. That is only 2 steps.
Class activity
Shorten it (with a friend): one of you says a long list of actions with some repeats, for example “hop, hop, hop, clap, spin, spin”. The other writes it as short as possible: “Hop 3 times, clap, spin 2 times”. Then do the long list and the short list with your body. Do they look the same? Swap jobs.
Practice
1
Make this algorithm shorter: Jump, Jump, Jump, Jump, Spin.
Jump 4 times. Spin. (2 steps instead of 5.)
2
A toy robot follows these six steps. Write a shorter algorithm that does the same job.
| Step | Instruction |
|---|---|
| 1 | Move forward |
| 2 | Move forward |
| 3 | Move forward |
| 4 | Turn right |
| 5 | Move forward |
| 6 | Move forward |
Move forward 3 times. Turn right. Move forward 2 times. (3 steps instead of 6.)
3
Look at the two lists. In which list can two steps be joined? Hop, Hop, Skip or Hop, Skip, Hop.
In the first list, the two Hops are side by side, so we can write “Hop 2 times, Skip”. In the second list no two steps that are the same are side by side.
4
The robot cards show 5 steps. Write them with only 3 steps.
F2, R, F2 (forward 2 times, turn right, forward 2 times).