Week 26 · Unit 7.6: Sequencing and pattern recognition · 3 lessons
3
Spot the pattern, reuse it
Computing 7: Unit 7.6, pp. 182–224
GoalUse pattern recognition to compare block and text code, to copy and edit a program, and to continue a sequence.
New words
pattern recognition · spotting what repeats or stays the same, and using it to help solve a problemblock-based programming · making programs by joining blocks on the screen, as in Scratchtext-based programming · making programs by typing lines of code, as in Pythonindentation · moving a line in from the left to show that it belongs to the line above
Explanation
Pattern recognition means spotting what repeats or stays the same and using it to help solve a problem. You can use it to continue a pattern, such as a row of lights. You can also use it to compare two programs. A block program and a text program share the same key words, such as button, A, show and heart. In the text program, the first line asks about the button, and the indented line under it is what happens inside it. When you see this pattern, you can copy a program and edit only the parts that change, such as the button name and the picture name. Capital letters are part of the pattern too: HEART and ARROW_W are written in capitals. Flowcharts can also be put side by side to find what stays the same.
a heart appears when button A is pressed (and stays, because nothing clears it)
Here is the same program in text. The block “on button A pressed” matches the line if button_a.is_pressed():. The block “show icon heart” matches the indented line display.show(Image.HEART). The key words button, a, show and heart appear in both.
pressing A shows an arrow pointing left; pressing B shows an arrow pointing right (each stays until another picture replaces it)
Copy and edit. Amir makes a bicycle indicator. Button A shows a left arrow. To add button B he copies the A lines and changes only two things: button_a becomes button_b, and ARROW_W becomes ARROW_E. Everything else is the same pattern.
Part of the flowchart
Button A flowchart
Button B flowchart
Shape of the question
decision diamond
decision diamond (the same)
The question
Is button A pressed?
Is button B pressed?
The output
Display a left arrow
Display a right arrow
Order of the steps
question, then output
question, then output (the same)
The flowcharts for the two buttons also share a pattern. Look at what stays the same and what changes.
Class activity
Pattern hunt (no computer). Write the bicycle indicator program from the lesson on a card. Colour the parts of each if block that stay the same in blue, and the parts that change in orange. Then invent a third program: button A shows Image.SMILE and button B shows Image.TARGET. Ask someone to copy and edit the program, using only your colours as hints. Check the capital letters.
Practice
1
This flowchart shows a pattern. Continue it so that the same two images are shown again, in the same order. Draw the extra boxes and write the four extra lines of MicroPython.
The sequence is house, duck. Add: Display a house, wait 2 seconds, Display a duck, wait 2 seconds. Code: display.show(Image.HOUSE), sleep(2000), display.show(Image.DUCK), sleep(2000). The pattern is a picture followed by a wait, and 2 seconds is 2000 milliseconds.
2
Hana copied the A lines to make the B lines. When A is held down, the program shows an arrow pointing down, not up. When B is held down, nothing happens. Find the mistake, and say what to change.
She changed the image but forgot to change button_a to button_b in the second if. So both if lines test button A. When A is pressed, the up arrow is shown and then straight away replaced by the down arrow. Fix: change the fifth line to if button_b.is_pressed():.
3
These blocks make a program. Write the MicroPython lines that do the same, using the pattern from the lesson.
if button_b.is_pressed(): then, indented underneath, display.show(Image.YES). On a micro:bit this is inside while True: so that the program keeps checking.
4
Explain how pattern recognition helps a programmer who copies and edits a program instead of writing a new one from nothing.
The programmer sees which parts of the code are the same pattern (the if line, the colon, the indentation, the display.show line) and which parts must change (the button name and the image name). Copying the pattern is faster, and it makes fewer typing mistakes than writing everything again.