Quarry School

Algorithms on paper: crash course

about 6 min to listen5 lessons0 questions to tryUnit 3 of 5

Short view. Each lesson's rule, its steps and its trap. Switch to Full for a worked example and a question to try in every lesson.Full view. Each lesson's rule, its steps, one worked example with Watch it solved, the trap, and one question to try.

Starts here no skill from another unit needed first

1 State inputs and outputs

about 1 minOpen the full lessonOnly this lesson

The rule

A problem statement names the input, says which inputs are allowed and names the output each allowed input needs.

An algorithm solves the problem when it ends with the correct output for every allowed input.

How

  1. Write the word Input and say what the algorithm is given. For the card problem, that is a row of cards.
  2. Add the limits on the input: how many items, and of what kind. Here the limits are one or more cards, each showing an integer.
  3. Write the word Output and say what must be handed back. Here it is the largest number in the row.
  4. Choose three or four allowed inputs. Include the smallest allowed input. For the card problem, add a row with a repeated number as well.
  5. Work out the correct output for each chosen input by hand and write it beside the input. Keep the table for testing.

The trap

The common slip is writing steps before the limits are settled. A statement that says a row of cards and stops there leaves the empty row open. One reader decides the output is 0, and another decides there is none. Decide which inputs are allowed and write the decision into the statement. In this unit the empty row is not allowed.

A second slip is naming the wrong output. The largest number in 5 5 3 is 5. The position of the largest card is a different output, and for this row two positions hold it.

2 Write an unambiguous instruction

about 1 minOpen the full lessonOnly this lesson

The rule

Each step of an algorithm has exactly one reading.

The steps are carried out in the order written unless a step says where to go next. Two readers who follow the same steps on the same input do the same things and reach the same output.

How

  1. Number the steps and write each one as a short instruction.
  2. Give a name to each thing the steps use, such as the noted number, and use the same name every time.
  3. Replace each vague word with a test a reader can carry out. Step 3 of the largest-card procedure does not say a big card. It names one card, the card taken in step 2, and one test: its number is larger than the noted number.
  4. For each question a step asks, say what to do for yes. Say what to do for no as well, unless the reader then goes on to the next step in order, as after step 3 of the largest-card procedure. Step 2 covers both answers: go to step 5 when no card is left, and take the next card otherwise.
  5. Say where to go after a step whenever that is not the next step in order.
  6. Read the steps as a reader who knows nothing else would, and mark any place where two actions are possible.

The trap

The usual gap is a case nobody wrote down. Suppose step 2 said only this: take the next card. On the row 4 9 2 a reader reaches the end of the row with no instruction for what comes next. One reader stops, and another starts again from the first card. Write the instruction for the case where no card is left.

A second question sits in the word larger. If step 3 acted for a card that is larger than or equal to the noted number, the row 5 5 3 would still give 5, and the reader would rewrite the slip one more time on the way. Each wording has one reading. The step must say which of the two it means.

3 Trace a decision

about 1 minOpen the full lessonOnly this lesson

The rule

At a selection step, work out the condition with the current values each time the step is reached.

If it is true, carry out the action the step gives for true. If it is false, carry out the step's otherwise action when it has one. After either, follow any instruction about where to go next, and continue with the next step in order when there is none. The condition decides which action is carried out.

How

  1. Draw a table with a column for the step, a column for the condition with its values filled in and a column for what the procedure holds after the step.
  2. Write the input above the table.
  3. Carry out the steps in order. At a selection step, copy the condition with the real values in place of the names.
  4. Write true or false beside the condition. If it is true, do the action the step gives for true. If it is false, do the step's otherwise action when it has one. After either, follow any instruction about where to go next, and move to the next step in order when there is none.
  5. Stop at the step that says stop. The output is what that step announces.
  6. Trace again with inputs that take the other paths: one input that makes each selection step's action run, and one for which no condition is true. For the late-fee procedure, 0, 3 and 8 days late do that.

The trap

Do not skip a step because the answer seems clear. Carry out every step the procedure reaches, including the ones whose condition is false.

The opposite slip is carrying out an action whose condition was false, or going on to step 3 after step 2 has said go to step 5. For 0 days late that slip announces 1 coin where the rule gives 0. Sequencing means carrying out the steps in the order the procedure gives them. Follow each jump as written.

4 Explain when a repetition ends

about 1 minOpen the full lessonOnly this lesson

The rule

A loop repeats its steps until its stopping condition is met.

If the stopping condition can never be met, the loop never ends. To explain why a loop ends, name its stopping condition and the step in each pass that moves toward it.

How

  1. Find the steps that repeat, and mark the step that sends the reader back.
  2. Find the stopping condition, which is the test that lets the reader leave the loop.
  3. Find the step inside the loop that changes what the stopping condition tests. In the largest-card procedure, taking a card changes how many cards are left.
  4. Check the direction of that change. In the largest-card procedure every pass leaves one card fewer in the row, so the row runs out.
  5. Trace a small input and count the passes. For a row of three cards, step 3 is carried out twice.
  6. Write one sentence that names the stopping condition and the step that moves toward it.

The trap

A loop can look finished on paper because the tracer stops when the answer seems clear. Trace until the stopping condition is met, and note the step at which the reader leaves the loop.

The serious slip is a stopping condition that the passes cannot reach. Take a procedure invented for this paragraph. It writes 1, then repeats two steps: add 2 to the number, and stop if the number is 10. The number goes 3, 5, 7, 9, 11 and on, and never equals 10. The ending condition never becomes true, so the loop does not end. Compare the wording of the stopping condition with what each pass changes.

5 Check a procedure against a counterexample

about 1 minOpen the full lessonOnly this lesson

The rule

A test case is an allowed input together with the output expected for it.

In this unit the expected output is worked out by hand before the procedure is carried out. A procedure passes a test when it announces that output. An allowed input for which it announces anything else is a counterexample, and one counterexample shows that the procedure does not solve the problem. Passing tests cover only the inputs tried.

How

  1. Take the table of inputs and correct outputs made with the problem statement, as in lesson 1.
  2. Add inputs at the extremes of what is allowed, such as the smallest allowed row. Add unusual rows too: a row with a repeated value, and a row whose numbers are all below zero.
  3. Trace the procedure on each input, and write what it announces beside the expected output.
  4. Mark each row pass or fail. A failed row is a counterexample.
  5. For a counterexample, identify the error and correct it. One way to find it is to look for the first step where the trace differs from a correct run. Correct that step and any later step that depends on it.
  6. Carry out every test again after the correction, including the tests that passed before.

The trap

After several passing tests it is tempting to call the procedure correct. The three rows that pass all hold a number above 0, which hides the mistake. The changed procedure fails only on rows whose numbers are all below 0. Choose tests that differ from each other in kind, including inputs at the extremes of what is allowed.

A second slip is patching the counterexample alone, for instance with a new step that announces -3 for that one row. The row -9 -4 would then fail in the same way. Correct the steps that cause the error by restoring the first two steps of the largest-card procedure of lesson 2. Step 1 takes the first card and writes its number on the slip, and step 2 goes on with the cards that are left.