计算机科学 · 第 1 册 · 1–9 年级

Primary & Middle School Computer Science

本书暂未推出简体中文版。现有版本:English。

Primary & Middle School Computer Science · 1–9 年级

10Algorithms: Repeat and Decide

Lina, Tom and Noor play a card game. Cards are laid face up in a row, and the first to point at the largest card wins it. Tom looks at all the cards again and again, comparing them in pairs. Lina is much faster. She looks at each card only once, from left to right, and remembers just one thing: the largest card seen so far. When she reaches the end of the row, she knows. Lina is following a method, and a method that always works has a name.

Which card has the most dots? Lina looks at each card once.
Which card has the most dots? Lina looks at each card once.

You already know

An instruction tells exactly one thing to do; a sequence of instructions is carried out one after another, in order. Our floor robot understands forward, turn left and turn right (Chapter 2).

10.1 What an algorithm is

Definition 10.1 (Algorithm)

An algorithm is a precise method, written as instructions, that solves a problem: it always comes to an end and gives the result, whatever the starting situation it is given.

Lina’s method is an algorithm: it works for a row of 5 cards or of 50, and it always ends, when the last card has been looked at. You already know other algorithms: adding two numbers in columns is one. Start with the units, add the digits, write the units of the sum and carry the tens to the next column, then do the same with each next column. The same steps work for any two numbers.

An algorithm is written in words, or in drawings, so that a person or a machine can follow it without having to guess. Later in this course, we write algorithms in a programming language, so that a computer can carry them out.

10.2 Repeating

To make the robot walk around a square of 3 by 3 squares, we could write twelve instructions. But they are the same three instructions four times over: forward, forward, turn left. We can say it in one line.

Definition 10.2 (Loop)

A loop is a group of instructions that is carried out again and again: a given number of times (“repeat 4 times”), or until something happens (“repeat until the square ahead is a wall”).

The loop “repeat 4 times: forward, forward, turn left”. The robot starts on S, facing right, walks around a square and comes back to S, facing right again.
The loop “repeat 4 times: forward, forward, turn left”. The robot starts on S, facing right, walks around a square and comes back to S, facing right again.

The second kind of loop does not know in advance how many times it will run. It looks at the situation before each turn.

Example 10.3 (Forward until the wall)

The robot stands on S, facing a wall somewhere ahead. The algorithm is:

repeat until the square ahead is a wall:
forward

Before each step, the robot checks the square ahead. Here it moves three times, then sees the wall and stops. With the wall one square further, the same algorithm would move four times: it works wherever the wall is.

“Repeat until the square ahead is a wall: forward”. The dark square is the wall.
“Repeat until the square ahead is a wall: forward”. The dark square is the wall.

10.3 Deciding

Sometimes the next instruction depends on the situation: if the way is free, go on; otherwise, turn.

Definition 10.4 (Condition)

A condition is a question about the situation whose answer is yes or no. An algorithm can decide: if the condition is true, it carries out some instructions; otherwise it carries out others.

“Is the square ahead free?”, “Is this card larger than the best one so far?” and “Is it raining?” are conditions. “Turn left” is not a condition: it is an instruction. “How many cards are there?” is not a condition either: its answer is a number, not yes or no.

A decision drawn as a diamond: the condition inside, one arrow for each answer. “If the square ahead is free, forward; otherwise, turn left.”
A decision drawn as a diamond: the condition inside, one arrow for each answer. “If the square ahead is free, forward; otherwise, turn left.”

10.4 Following an algorithm by hand

To check an algorithm, we follow it ourselves, step by step, exactly as written, and write down what happens. Nothing may be guessed.

Example 10.5 (A robot that finds its way)

The robot starts on S, facing right, on a grid with two walls. The algorithm is:

repeat until the robot is on the star:
if the square ahead is free then forward
otherwise turn left

A square is free if it is not a wall and is still on the grid: the edge of the grid counts as a wall. It moves forward three times; then the square ahead is a wall, so it turns left. It moves forward twice; the next square ahead is a wall again, so it turns left. Two more steps forward, and it is on the star: the loop stops, after 7 steps forward and 2 turns.

The robot’s path. At each dark wall it turns left; it stops on the star.
The robot’s path. At each dark wall it turns left; it stops on the star.

Method 10.6 (Finding the largest card)

Take the first card as the best so far. Then, for each next card, from left to right: if it is larger than the best so far, then it becomes the best so far. When there is no card left, the best so far is the largest card.

Finding the largest card of a row. The best so far is the first card, then changes twice (the outlined cards); the answer is 12. Each card is looked at once.
Finding the largest card of a row. The best so far is the first card, then changes twice (the outlined cards); the answer is 12. Each card is looked at once.

Two algorithms can reach the same result with different amounts of work. Tom compares every card with every other card: for 6 cards that makes 15 comparisons. Lina makes one comparison for each card after the first: 5 comparisons. For a row of 50 cards, Tom would need 12251225 comparisons and Lina 49. When two algorithms give the same result, we choose the one that takes fewer steps.

10.5 Exercises

Exercise 10.1 ★

The robot starts in the bottom left square of an empty grid, facing right. Follow “repeat 3 times: forward, forward, turn left”. Draw its path. On which square does it stop, and which way does it face?

Solution

Solution of Exercise 10.1.

It walks 2 squares right, 2 squares up, then 2 squares left: three sides of a square. It stops on the third square up of the leftmost column (2 squares above the start), facing down.

Exercise 10.2 ★

Write a loop that makes the robot walk around a rectangle 4 squares wide and 2 squares high, and come back to where it started.

Solution

Solution of Exercise 10.2.

repeat 2 times:
forward, forward, forward, turn left, forward, turn left

The robot walks along a row of 4 squares, goes up one, comes back along the row above, and goes down one: it is back on its start square, facing right.

Exercise 10.3 ★

Find the largest card of the row 5, 11, 2, 8, 14, 6, 9 with Lina’s method. Write the table “card looked at / best so far”.

Solution

Solution of Exercise 10.3.

card looked at511281469
best so far5111111141414

The largest card is 14.

Exercise 10.4 ★

Change one word of Lina’s method so that it finds the smallest card. What is the smallest card of the row of the previous exercise?

Solution

Solution of Exercise 10.4.

Replace “larger” by “smaller” (and read “best so far” as “smallest so far”). The smallest card is 2.

Exercise 10.5 ★

Which are conditions? (a) Is the square ahead a wall? (b) Go forward. (c) Is the card red? (d) How many dots are on the card? (e) Is the robot on the star? (f) Turn left.

Solution

Solution of Exercise 10.5.

(a), (c) and (e) are conditions: their answer is yes or no. (b) and (f) are instructions. (d) is a question, but its answer is a number, so it is not a condition.

Exercise 10.6 ★

The robot starts in the bottom left square, facing right; the third square of the bottom row is a wall. Follow “repeat 4 times: if the square ahead is free then forward, otherwise turn left”. Where does the robot stop?

Solution

Solution of Exercise 10.6.

Step 1: the second square is free, forward. Step 2: the square ahead is the wall, turn left (now facing up). Steps 3 and 4: forward, forward. The robot stops on the second square of the third row, facing up.

Exercise 10.7 ★

Add 56+2756 + 27 in columns, and write each step of the algorithm in a sentence.

Solution

Solution of Exercise 10.7.

Units: 6+7=136 + 7 = 13; write 3 and carry 1 to the tens. Tens: 5+2+1=85 + 2 + 1 = 8; write 8. There is no column left: the sum is 83.

Exercise 10.8 ★

The robot starts on S, facing right. Which program draws the path of the square in the drawing of the loop: “repeat 4 times: forward, forward, turn left”, or “repeat 4 times: forward, forward, turn right”? Where does the other one go?

Solution

Solution of Exercise 10.8.

“Forward, forward, turn left”: after the first two steps it turns up, so it walks around the square above and to the right of S. With “turn right”, it would turn down after two steps and walk around a square below S, which is off this grid.

Exercise 10.9 ★★

The robot starts in the bottom left square, facing right, and must reach the square 2 to the right and 2 up. Count the instructions of each program, and check that each one reaches the goal. Which is the shortest? (a) forward, forward, turn left, forward, forward. (b) turn left, forward, forward, turn right, forward, forward. (c) forward, forward, forward, turn left, forward, forward, turn left, forward.

Solution

Solution of Exercise 10.9.

(a) 5 instructions: 2 right, turn, 2 up. (b) 6 instructions: 2 up, turn, 2 right. (c) 8 instructions: 3 right, 2 up, 1 left. All three reach the goal; (a) is the shortest.

Exercise 10.10 ★★

On an empty grid of 4 by 4 squares, the robot starts in the bottom left square, facing right; the star is on the second square of the second row. The algorithm is the one of the robot that finds its way. Follow it for a while. Does it ever stop? What is missing for this to be an algorithm?

Solution

Solution of Exercise 10.10.

The robot walks along the edge of the grid: right to the corner, up, left, down, and back to its start square, turning left at each corner. It never passes through the star, which is inside, so it goes round and round forever. An algorithm must always come to an end: this one needs, for example, a way to reach squares that are not on the edge, or a rule that stops it when it is back where it started.

Exercise 10.11 ★★

Write an algorithm that counts the red cards of a row of cards. Use a loop and a decision.

Solution

Solution of Exercise 10.11.

For example:

Start with a count of 0.
repeat for each card of the row, from left to right:
if the card is red then add 1 to the count
When there is no card left, the count is the number of red cards.

本章定义的术语

在术语表中查看全部 1050 个术语