Skip to content
KALANBLON
Show contents

Chapter 1 of 4 · 15 min

An algorithm is a recipe

By the end of this chapter, you'll be able to

  • Define what an algorithm is in your own words
  • Write a sequence of precise, ordered steps that solves a problem
  • Spot an ambiguous step and make it precise
  • Read an algorithm written in pseudocode

From a recipe to an algorithm

Take a recipe for mint tea: heat the water, rinse the tea, add the mint and sugar, let it brew, serve. Each step is an instruction, and the order matters: serving before heating the water doesn't make much sense.

The exact definition: an algorithm is a finite sequence of precise, ordered instructions that solves a problem or accomplishes a task. Three words matter:

  • finite: the algorithm must stop at some point;
  • precise: each instruction must have only one possible interpretation;
  • ordered: the instructions are carried out in a set order.

A computer program is an algorithm translated into a language the machine understands, such as Python. So we start by thinking about the algorithm, then we translate it.

Pseudocode

To write an algorithm without worrying about a specific language, we often use pseudocode: structured plain English, with one instruction per line. Here is an algorithm that computes the price of a basket of 3 mangoes at €1.50 each, plus €2 delivery:

unit_price ← 1.5
quantity ← 3
total ← unit_price × quantity + 2
display total

The arrow ← means "store the value on the right in the box on the left". A variable is exactly that: a named box that keeps a value, like total.

Line 4: we display the contents of total. The algorithm is finished.

Step 6 of 6
Text description of the animation
  1. The algorithm has four instructions. We'll carry them out one by one, from top to bottom.
  2. Line 1: we store 1.5 in a box called unit_price.
  3. Line 2: we store 3 in the box quantity.
  4. Line 3: we first compute the right-hand side with the values already stored…
  5. … then we store the result, 6.5, in the box total.
  6. Line 4: we display the contents of total. The algorithm is finished.

The same thing in Python

Here is the exact translation into Python. You don't need to understand it in detail: you'll learn Python in the next course. Just notice that each line of pseudocode becomes one line of code.

unit_price = 1.5
quantity = 3
total = unit_price * quantity + 2
print(total)
6.5

Your turn

Mini-lab: your own basketPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Worked exercise

Worked exercise: the walk to school

Write an algorithm in pseudocode that computes how many minutes it takes to walk 3 km at 5 km/h. The algorithm must use three variables, distance, speed and minutes, then display the result.

Hint 1
The time in hours is the distance divided by the speed.
Hint 2
To convert hours into minutes, multiply by 60.
Show the solution and common mistakes
distance ← 3
speed ← 5
minutes ← distance ÷ speed × 60
display minutes

The result is 36 minutes.

Common mistakes:

  • Forgetting to convert to minutes: you get 0.6, which is a number of hours.
  • Writing "display the time" without saying which variable to display: the instruction is vague.

Summary

  1. 1An algorithm is a finite sequence of precise, ordered instructions.
  2. 2Pseudocode lets you write an algorithm without choosing a language.
  3. 3A variable is a named box that keeps a value.
  4. 4Every instruction must be possible to carry out without ambiguity.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.