Back to homeDigital Culture · High School Year 1 · Change subject
Start for free
Prep 1 · Digital Culture
Digital content creation · Simple creation and programming

Algorithms and Computational Thinking

Imagine that Ximena walks into the corner store and is asked to hand out tortas (sandwiches) among her soccer teammates. If she only thinks "I'll hand them out," she could run into problems: there aren't enough tortas, someone gets a second one, or nobody knows who goes first. Before programming anything, you need to have the steps absolutely clear. That exact list of steps is an algorithm, and thinking this way (decomposing, looking for patterns, keeping what matters) is computational thinking. At the end of the page we solve Ximena's case.

What an algorithm is

An algorithm works with data (individual values, like 7, "Diego," or 25 °C) and transforms them into information (data that has meaning for the person using it, like "it's hot today, turn on the fan"). That's why it helps to always think in three parts: input (the data you start with), process (what you do with it), and output (the result).

Linear flowchart showing, from top to bottom, the ordered steps of a recipe: start, gather ingredients, mix, serve, and end. Start Step 1: gather ingredients Step 2: mix Step 3: serve End

To solve a problem with digital tools, you follow five steps:

  1. Identify the problem: what do I want to achieve.

  2. Understand it: what data do I have and what result do I expect.

  3. Analyze alternatives: what different ways are there to solve it.

  4. Select the best one: the fastest, simplest, or safest.

  5. Use a method or technique, such as a flowchart, to write and test the solution.

The same problem can have several algorithms. To sort a deck of cards, for example, you can go through it card by card, putting each one in its place, or first split it into piles by suit. One is "better" if it needs fewer steps, less time, or fewer errors for the same result.

Practice★☆☆Not started
Write the algorithm, as a numbered list of steps, to make a cheese sandwich, so that a person who has never made one can follow it without doubt. Review your list and…

Write the algorithm, as a numbered list of steps, to make a cheese sandwich, so that a person who has never made one can follow it without doubt. Review your list and check that no steps you took for granted are missing.

Solve
Practice★☆☆Not started
Write the algorithm for preparing a glass of cocoa milk in six to eight steps, so precise that a robot could follow them: quantities, order of steps, and how to check…

Write the algorithm for preparing a glass of cocoa milk in six to eight steps, so precise that a robot could follow them: quantities, order of steps, and how to check that the cocoa has dissolved before finishing.

Solve
Practice★☆☆Not started
In the "frying an egg" recipe, identify the input (ingredients and utensils), the process (steps involving heat and the frying pan), and the output (the result). Explain…

In the "frying an egg" recipe, identify the input (ingredients and utensils), the process (steps involving heat and the frying pan), and the output (the result). Explain why thinking in this way helps to write clear algorithms.

Solve
Practice★☆☆Not started
A recipe says "stir the sauce until it thickens". Explain why an algorithm must always terminate and what could happen if the sauce never thickens. Rewrite that step so…

A recipe says "stir the sauce until it thickens". Explain why an algorithm must always terminate and what could happen if the sauce never thickens. Rewrite that step so that the cook does not end up stirring forever.

Solve

Decisions and repetitions in the flowchart

A flowchart draws the algorithm so you can see it at a glance. Besides steps in a straight line, it has two key structures: the decision (conditional structure: if this happens, I do A; if not, I do B) and the repetition (repetitive structure: I repeat a step while a condition holds).

First, a decision: the automatic reading lamp.

Flowchart with a decision: if there is little light the lamp is turned on, otherwise it is left off; both paths end at the end. Start is there little light? yes turn on lamp no lamp off End

Now, a repetition: the arrow returns to the diamond as long as the condition holds.

Flowchart with a repetition: while tortas remain, one is handed out and the arrow returns to the diamond; when none remain, it ends. Start are there tortas left? yes hand out one repeat no End

To follow an algorithm by hand, you use a variable: a named space where you store a piece of data that can change (like a "counter" that starts at 0 and goes up by one). Writing down the value of the variables after each step is called doing a trace.

Practice★☆☆Not started
These steps of the "going to school" algorithm are out of order: (a) enter the classroom, (b) get up, (c) have breakfast, (d) leave the house, (e) get dressed, (f) walk…

These steps of the "going to school" algorithm are out of order: (a) enter the classroom, (b) get up, (c) have breakfast, (d) leave the house, (e) get dressed, (f) walk to school. Put them in the correct order and add a decision to the algorithm: what to do if it is raining when leaving the house.

Solve
Practice★☆☆Not started
The figure shows, using interlocking blocks, the algorithm for an automatic reading lamp that should turn on only when light is low, but one block is missing. Indicate…

The figure shows, using interlocking blocks, the algorithm for an automatic reading lamp that should turn on only when light is low, but one block is missing.

on start repeat forever if light is low turn on the lamp else ?

Indicate which block you would place in the gap and explain in your own words how the complete algorithm works, step by step.

Solve
Practice★☆☆Not started
Read this algorithm: (1) pick up the deck of cards, (2) while cards remain in your hand, deal one to each player starting on your left, (3) when no cards remain,…

Read this algorithm: (1) pick up the deck of cards, (2) while cards remain in your hand, deal one to each player starting on your left, (3) when no cards remain, announce "dealt". Explain in one sentence what the algorithm does and point out which part is the loop and when it terminates.

Solve
Practice★★☆Not started
Trace this algorithm with $n = 7$: (1) start with the value of $n$, (2) repeat three times: add 2, (3) subtract 5, (4) write the result. Note down the value after each…

Trace this algorithm with $n = 7$: (1) start with the value of $n$, (2) repeat three times: add 2, (3) subtract 5, (4) write the result. Note down the value after each step, give the final result, and explain what a trace is and why performing it by hand is useful.

Solve
Practice★★☆Not started
Write the algorithm to count how many people enter through the classroom door using only a "counter" variable starting at 0: what you do each time someone enters, how…

Write the algorithm to count how many people enter through the classroom door using only a "counter" variable starting at 0: what you do each time someone enters, how you know when to stop counting, and what value the counter will have at the end.

Solve

Decomposing, recognizing patterns, and abstracting

A big problem, shaped like a puzzle piece, is divided by a cut into two smaller subproblems that fit together. big problem subproblem 1 subproblem 2

Decomposing helps because each small part is easier to solve, to test, and to split among several people. Recognizing patterns lets you write a rule instead of a long list: in 5, 10, 15, 20… the rule is "add 5"; in 1, 2, 4, 8… it's "multiply by 2."

You also have to compare algorithms. If you look up "murciélago" in a dictionary, going page by page can take hundreds of steps; opening to the middle and discarding the half that doesn't apply takes very few, but it only works if the pages are in order. Each opening divides the pages by 2: from 500 you go to 250, 125, 63… and in about 9 openings you reach a single page.

Practice★★☆Not started
Decompose the problem "organising the end-of-year party" into four smaller sub-problems and write two concrete steps to solve each one. Explain the advantage of breaking…

Decompose the problem "organising the end-of-year party" into four smaller sub-problems and write two concrete steps to solve each one. Explain the advantage of breaking down a large problem in this way.

Solve
Practice★★☆Not started
To sort a deck of cards there are several different algorithms, and some are faster than others. Explain with an everyday example how the same problem can be solved by…

To sort a deck of cards there are several different algorithms, and some are faster than others. Explain with an everyday example how the same problem can be solved by multiple algorithms and what it means for one to be "better" than another.

Solve
Practice★★☆Not started
Recognising patterns means finding the repeating rule. Find the pattern in these three sequences and express it as a clear rule: (a) 5, 10, 15, 20, ..., (b) 100, 90, 80,…

Recognising patterns means finding the repeating rule. Find the pattern in these three sequences and express it as a clear rule: (a) 5, 10, 15, 20, ..., (b) 100, 90, 80, 70, ..., (c) Monday: 1 press-up, Tuesday: 2, Wednesday: 4, Thursday: 8, ... State what value would come next in each one.

Solve
Practice★★☆Not started
A tube map does not draw the real city: it removes distances, curves, and streets, keeping only stations and connections. Explain what abstraction is using this example…

A tube map does not draw the real city: it removes distances, curves, and streets, keeping only stations and connections. Explain what abstraction is using this example and then apply it to the floor plan of your school: which three features would you include and which three would you remove?

You can do it on paper and send a photo with your answer.

Solve
Practice★★★Not started
You are looking for a word in a 500-page dictionary. Compare these two algorithms: opening at the first page and advancing one by one, or opening in the middle and…

You are looking for a word in a 500-page dictionary. Compare these two algorithms: opening at the first page and advancing one by one, or opening in the middle and discarding the unneeded half. Explain which is more efficient, why it only works if the pages are sorted, and estimate how many "page openings" each method requires.

Solve

Dependencies and tasks that run at the same time

When you decompose a problem into tasks, not all of them can be done in any order. A task depends on another if it can't start until the other one finishes: you can't serve the agua de jamaica (hibiscus drink) before preparing it. Two tasks are independent if neither needs the other; then they can be done at the same time (in parallel) if there are two people, or two processes in a computer.

Example: Diego and Regina are preparing a dinner of quesadillas and agua de jamaica for four guests.

Dependency graph: boiling the hibiscus leads to sweeten and chill; heating the comal leads to making quesadillas; setting the table doesn't depend on anything. Boil the hibiscus Sweeten and chill Heat the comal Make quesadillas Set the table no arrows: independent
Practice★★★Not started
Two people prepare breakfast together for four guests: coffee needs to be made, bread toasted, juice squeezed, and the table set. Divide the tasks between the two while…

Two people prepare breakfast together for four guests: coffee needs to be made, bread toasted, juice squeezed, and the table set. Divide the tasks between the two while respecting dependencies (you cannot serve coffee before making it) and explain which tasks can be done simultaneously and which cannot, giving their order.

Solve

Edge cases and errors in algorithms

Flowchart illustrating an edge case through a prior check before carrying out an action. Start are there tortas left? yes hand out one no announce: no more left

So we close Ximena's case: before handing out each torta, the algorithm checks whether any are left; if none are, it announces that and hands out in turns whatever there is. It no longer fails when there are more teammates than tortas.

Practice★★☆Not started
A recipe says: "add salt and simmer for a while on medium heat". Explain why a computer could not follow that instruction and rewrite it as a precise algorithm step,…

A recipe says: "add salt and simmer for a while on medium heat". Explain why a computer could not follow that instruction and rewrite it as a precise algorithm step, stating quantities, timings, and how to check that the step is complete.

Solve
Practice★★☆Not started
This "doing the laundry" algorithm contains a bug: (1) put all clothes into the washing machine, white and coloured together, (2) add detergent, (3) choose the long…

This "doing the laundry" algorithm contains a bug: (1) put all clothes into the washing machine, white and coloured together, (2) add detergent, (3) choose the long hot-water program, (4) hang out to dry. Find two problematic steps, explain what could go wrong in each, and rewrite the corrected algorithm including a prior sorting step.

Solve
Practice★★★Not started
The algorithm for a canteen hands out a sandwich to each attendee, but fails when there are more attendees than sandwiches. Explain what an "edge case" is in an…

The algorithm for a canteen hands out a sandwich to each attendee, but fails when there are more attendees than sandwiches. Explain what an "edge case" is in an algorithm, propose how distribution should behave in that situation, and rewrite the problematic step so that the algorithm does not fail.

Solve
Practice★★★Not started
A cash machine only contains $\$400$ notes and its algorithm dispenses the requested amount in notes. Explain what happens when someone requests $\$1\,500$, why this is…

A cash machine only contains $\$400$ notes and its algorithm dispenses the requested amount in notes. Explain what happens when someone requests $\$1\,500$, why this is an edge case, and rewrite the cash machine rules so that it always provides a sensible response.

Solve
Practice★★★Not started
Design the complete algorithm, with numbered steps that include decisions and loops, for a pedestrian crossing with a button: what happens from when someone presses the…

Design the complete algorithm, with numbered steps that include decisions and loops, for a pedestrian crossing with a button: what happens from when someone presses the button until the lights return to their initial state, including the timings for each light.

Solve
Practice★★★Not started
Think of a program that plays noughts and crosses. Describe the strategy it would follow on each turn: what board data it must inspect, in what order, and what rules you…

Think of a program that plays noughts and crosses. Describe the strategy it would follow on each turn: what board data it must inspect, in what order, and what rules you would use to decide where to place the mark. There is no need to write code: state the rules in natural language.

Solve
Practice★★★Not started
The figure shows, using interlocking blocks, the skeleton of a secret number guessing game, but inside the loop a piece is missing so that the game can give clues to the…

The figure shows, using interlocking blocks, the skeleton of a secret number guessing game, but inside the loop a piece is missing so that the game can give clues to the player.

on start repeat until correct ask for a number ? say Correct!

Describe which block (or blocks) you would place in the gap so that the game tells the player whether their number is greater or smaller than the secret number, and explain the complete operation of the algorithm: what is repeated, when it terminates, and what happens if the player gets it right on the first try.

Solve
Practice★★★Not started
Two algorithms are proposed for classroom recycling: (a) each day, the person responsible sorts all the day's rubbish into three bins, and (b) each week, in turns, a…

Two algorithms are proposed for classroom recycling: (a) each day, the person responsible sorts all the day's rubbish into three bins, and (b) each week, in turns, a pair sorts everything accumulated. Evaluate both against criteria of total time, sorting errors, and distribution of effort, and choose one with justification.

Solve

Summary

See all the exercises in this section