এই বইটি এখনো বাংলা ভাষায় পাওয়া যায় না। সংস্করণ: English।
Primary & Middle School Computer Science · শ্রেণি 1–9
2Instructions in Order
Tom’s little brother Leo is hungry. Tom has to go out, so he writes the steps for making a jam sandwich on a piece of paper: “Spread jam on a slice of bread. Open the jar. Take two slices of bread. Put one slice on the other.” Leo follows the steps exactly as they are written. He tries to spread jam with the jar still closed. Then he opens the jar, and only then does he take the bread. When Tom comes back, the kitchen is a mess.
Every step on Tom’s paper was right. What went wrong was their order. A computer is like Leo: it follows the steps it is given, exactly, in the order they are given.
You already know
A computer is a machine that follows instructions (Chapter 1). People write the instructions; the computer carries them out, very fast, and nothing else.
2.1 A task as a list of steps
Many things we do every day are made of steps: making a sandwich, getting dressed, brushing our teeth, planting a seed. To tell somebody how to do such a task, we can write the steps one under the other.
Definition 2.1 (Instruction)
An instruction is one step that tells exactly what to do: “take two slices of bread”, “turn left”, “stop”.
A good instruction is short and clear. “Make the sandwich nicely” is not a good instruction: Leo cannot know what “nicely” means. “Cut the sandwich in two” is clear.
Example 2.2 (Brushing your teeth)
1. Wet the toothbrush. 2. Put toothpaste on the toothbrush. 3. Brush your teeth for two minutes. 4. Rinse your mouth. 5. Rinse the toothbrush. Each line is one instruction.
2.2 The order matters
Definition 2.3 (Sequence of instructions)
A sequence of instructions is a list of instructions to follow one after the other, from the first to the last, in the order in which they are written.
If two steps are swapped, the result can be wrong. You must put on your socks before your shoes. Tom’s sandwich went wrong because “open the jar” came after “spread jam”.
Sometimes two steps can be swapped without any harm. You can put on your left glove first or your right glove first: both orders give two warm hands. Before you change the order of two steps, ask yourself: does the second step need the first one to be done?
2.3 Guiding a robot on a grid
In the classroom, Mia and Sami play with a small floor robot on a mat made of squares. The robot understands only three instructions:
- forward (): move one square forward, in the direction it faces;
- left (): turn a quarter turn to the left, on the spot, without moving to another square;
- right (): turn a quarter turn to the right, on the spot.
We write a sequence for the robot as a row of instructions, read from left to right. The robot starts on the square marked S, facing to the right.
Example 2.4 (Reaching the star)
Mia writes the sequence
(forward, forward, left, forward, forward, right, forward). The robot goes two squares to the right, turns to face up, goes up two squares, turns to face right again, and goes one more square. It stops on the star. The drawing below shows its path.
Remark 2.5 (Turning does not move)
After “left” or “right” the robot is still on the same square: only the direction it faces has changed. That is why no square of the drawing carries the numbers 3 and 6: instructions 3 and 6 are turns.
2.4 Finding the wrong step
Sami wants the robot to reach the flag. He writes
but the robot stops far from the flag. Which instruction is wrong?
Method 2.6 (Finding a wrong instruction)
Put your finger on the start square, facing the right way. Read the sequence one instruction at a time and move your finger for each one, as the robot would. After each instruction, check: is the finger still on a good path to the goal? The first instruction after which the answer is “no” is the wrong one. Change only that instruction, then follow the whole sequence again from the start.
Example 2.7 (Fixing Sami’s sequence)
With a finger: forward, forward, forward — the finger is three squares to the right of S. The flag is four squares to the right, so the next instruction should be “forward” again, not “left”. The fourth instruction is the wrong one. The fixed sequence is forward four times, left, then forward three times: the robot goes up the column of the flag and stops on it.
2.5 Exercises
Exercises 3 to 5 and 7 to 10 use the grid below; exercise 6 uses the grid of Mia’s robot, which has no walls. The robot always starts on S, facing to the right. The grey squares are walls: the robot cannot enter them.
Exercise 2.1 ★
Put these five steps of getting dressed in a good order: put on your shoes; put on your coat; put on your socks; put on your trousers; put on your underpants.
Solution
Solution of Exercise 2.1.
Underpants, trousers, socks, shoes, coat. (Socks may also come before the trousers; the shoes must come after the socks and after the trousers.)
Exercise 2.2 ★
In the list of exercise 1, find two steps that can be swapped without any harm, and two steps that cannot.
Solution
Solution of Exercise 2.2.
Can be swapped: the trousers and the socks. Cannot be swapped: the socks and the shoes (shoes first, and the socks cannot go on).
Exercise 2.3 ★
The robot follows . On which letter does it stop?
Solution
Solution of Exercise 2.3.
Two squares to the right, a turn to face up, three squares up: the robot stops on C.
Exercise 2.4 ★
Write a sequence that takes the robot from S to B.
Solution
Solution of Exercise 2.4.
For example: forward five times, left, forward four times: (10 instructions).
Exercise 2.5 ★
Lina wants to reach A. She writes , and the robot stops one square to the right of A. Which instruction is wrong? Write the fixed sequence.
Solution
Solution of Exercise 2.5.
A is three squares to the right of S, so the fourth instruction must be “left”, not “forward”. One “forward” too many at the start: .
Exercise 2.6 ★
On the grid of Mia’s robot, with no walls, which of these sequences draws the path of its drawing (the path to the star)? (a) ; (b) ; (c) .
Solution
Solution of Exercise 2.6.
(b). Sequence (a) turns right after two squares and leaves the grid at the bottom. Sequence (c) also ends on the star, but by another path: it goes up from the second column, not from the third.
Exercise 2.7 ★
The sequences and both take the robot to the same square. Which square? How many instructions does each sequence have?
Exercise 2.8 ★★
Write the shortest sequence you can find that takes the robot from S to D without entering a wall. How many instructions does it have?
Solution
Solution of Exercise 2.8.
The walls block the two left squares of the middle row, so the robot must go up in the third column or farther right: , 10 instructions. It needs at least 2 squares to the right, 4 up and 2 back to the left (8 forwards) and at least 2 turns, so 10 is the shortest.
Exercise 2.9 ★★
Write a sequence that makes the robot leave S and come back to S.
Solution
Solution of Exercise 2.9.
For example (two squares out, a half turn, two squares back), or (around a small square).
Exercise 2.10 ★★
The left instruction of the robot is broken: you may only use and . Write a sequence that takes the robot from S to A.
Solution
Solution of Exercise 2.10.
Three right turns make the same turn as one left turn: .