AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Mix and test programs

Week 3 · Unit 1: Making programs clear · 3 lessons
2

Count and combine

Computing 3: Unit 1, pp. 8–27
GoalSpot repeated blocks, combine them, count the blocks and check that the shorter program does the same job.
New words
pattern · something that repeats in the same waycombine · to join several things into onecompare · to look at two things and see how they are the same or different
Explanation

To make a program concise, look for patterns. A pattern is a block that repeats, side by side. We combine the repeats into one block with a bigger number. Then we count the blocks again. Fewer blocks should mean the same result, so always compare the two programs. They should end in the same place, at the same size. Take care: if another block sits between two blocks, they are not side by side.

Program A (6 blocks)whenclickedmove20stepsmove20stepsmove20stepschange size by10change size by10Program B (3 blocks)whenclickedmove60stepschange size by20
Worked examples
Program AProgram B
x at the end−100 + 20 + 20 + 20 = −40−100 + 60 = −40
size at the end100 + 10 + 10 = 120100 + 20 = 120
blocks63
A snail crawls along a leaf and gets bigger as it comes closer. Program A moves 20 steps three times and grows by 10 twice. Program B uses one move block and one size block. The snail starts at x: −100, size 100. Both programs end at x: −40 and size 120. B has 3 blocks, A has 6.
Long algorithmShort algorithm
Water plantWater 4 plants
Water plantPull 2 weeds
Water plant
Water plant
Pull weed
Pull weed
The same idea works for steps in any algorithm. A gardener’s algorithm has 6 steps: water a plant four times, then pull a weed twice. Joined, it says: Water 4 plants. Pull 2 weeds. Six steps become two.
Awhenclickedmove10stepsturn ↻90degreesmove10stepsBwhenclickedmove20stepsturn ↻90degrees
Do these programs do the same? A moves 10, then turns, then moves 10. B moves 20, then turns. No. In A the sprite turns before its second move, so it ends in a different place. Only blocks that are side by side can be joined.
Class activity

Pattern hunt (with a friend): write a long list of actions on a strip of paper, for example “clap, clap, stamp, stamp, stamp, jump”. Underline the groups of repeated actions. Rewrite the list in the shortest way: “Clap 2 times, Stamp 3 times, Jump”. Count the steps in each list. Then do both lists out loud. Do they feel the same?

Practice
1
Make this program more concise. Which blocks repeat, and how many blocks does the new program have?
whenclickedchange size by5change size by5change size by5change y by10change y by10change y by10
2
Here is a baker’s algorithm. Rewrite it with joined steps. How many steps does your algorithm have?
StepInstruction
1Add a scoop of flour
2Add a scoop of flour
3Add a scoop of flour
4Stir
5Add an egg
6Add an egg
7Bake
3
A program has 10 blocks. Two pairs of blocks are joined into one block each. How many blocks does the new program have?
4
Do programs A and B do the same job? Explain.
Awhenclickedchange y by20change y by20change y by20change size by5Bwhenclickedchange y by50change size by5