## Want to keep learning?

This content is taken from the Raspberry Pi Foundation & National Centre for Computing Education's online course, Programming 102: Think Like a Computer Scientist. Join the course to learn more.
2.1

## Raspberry Pi Foundation

Skip to 0 minutes and 3 seconds Hello, and welcome to week two of the course. Last week, you looked at the use of functions and how you could use them to build reusable chunks of code. You also looked at how to include inputs and outputs in your functions as parameters and return values. This week, you’re going to learn what programmers mean by the term “algorithm” and how they relate to the programs we write. You’ll also have the chance to extend your Python skills to create and manipulate lists. You’ll use these new skills to convert algorithms from plain English into programs. So what is an algorithm? An algorithm can simply be defined as a set of instructions that precisely describes an activity.

Skip to 0 minutes and 47 seconds Algorithms are used everyday, all around us, to do all sorts of tasks, from finding a route from A to B, searching the internet for the most appropriate pages, or even just recommending movies based on your past viewing. To be called an algorithm, rather than just a set of instructions, it must be sufficiently precise and unambiguous, leaving nothing to interpretation. An algorithm must be repeatable, producing predictable outcomes based upon the same inputs. We can represent algorithms in a range of forms, including natural language, pseudocode, flow charts, or symbols. The terms “algorithm” and “program” are often confused. Whilst the two are related, they have different meanings.

Skip to 1 minute and 34 seconds An algorithm describes how to complete a specific task for any actor to carry out, either a human or computer. A program is the same algorithm but implemented for a specific computer to carry out, written in a language which is understood by that computer. For example, a square-drawing algorithm could be implemented as programs it either Scratch the Turtle model in Python or even using a floor robot, like this one.

Skip to 2 minutes and 5 seconds One of the really interesting aspects of algorithms is that, for any given problem, there are numerous solutions. Whether they be simple, elegant, speedy, or inefficient, they are all valid, as long as they produce the desired outputs. In a group of learners, it’s therefore entirely possible to get a different but correct algorithm from each of them. Students like to be correct. And so acknowledging the validity of their solution is important. Depending on where they are used, the performance of an algorithm can have a significant impact. And it can be compared in different ways. A route-finding algorithm for a sat nav needs to find a suitable route quickly. Therefore, speed is important.

Skip to 2 minutes and 47 seconds Algorithms that govern the behaviour of a self-driving car also need to respond quickly to changing situations. However, they are also making life and death decisions and so need to be robust and tested. Every time you search the internet, an algorithm is used to rank the most relevant pages for that search. The user cares about both the speed and the accuracy of the search results. Algorithms are indeed everywhere. And we have already solved many problems using them. However, there is still work to be done. As a society, we are finding new problems in science, medicine, arts, politics, and artificial intelligence– all needing new algorithms that are yet to be invented. Now, it’s your turn.

Skip to 3 minutes and 32 seconds Write a simple algorithm to draw a 2D shape, like a pentagon or a star. Share it in the comments and see if you can work out, from other learners’ algorithms, what the shape was. Good luck.

# What is an algorithm?

Welcome to Week 2. We’re glad you’re still with us!

In Week 1, we spent time reviewing some essential programming skills, and in particular the use of functions. We discussed how programmers use functions to build reusable chunks of code and manage complexity, and proposed an analogy with a machine as a useful tool to explain functions to beginners. This analogy was again useful when we looked at inputs and outputs of functions, or parameters and return values.

This week we will discuss what programmers mean by the term algorithm, and how algorithms relate to the programs we write. There will be an opportunity to write some plain English algorithms, and then use our new found list skills to turn these algorithms into programs. You’ll also extend your Python skills by learning to create and manipulate lists.

Once again, we have plenty of quizzes to help you check your learning as well as opportunities to share and discuss with other learners on the course. I’d like to encourage you to take these opportunities in order to make the most of Week 2.

## Algorithms

One of the challenges associated with learning (and teaching) computer science is the additional vocabulary it introduces, either complex terms for simple enough ideas (algorithm, decomposition, abstraction), or terms that have specific meanings in other areas of learning (‘variable’ means something subtly different in maths, science and computer science). Whilst it is important that learners understand terminolgy, more important is their grasp of the concept or idea in question.

An algorithm can simply be defined like this: - An algorithm is set of instructions that precisely describe an activity. - The instructions should be such that any ‘actor’ could follow to produce the same output. - An ‘actor’ refers to anything, whether human or computer, that is capable of computation.

Algorithms are used every day all around us to do all sorts of things, such as finding a route from A to B, searching the internet for the most appropriate pages, or recommending movies based on your past viewing.

Whilst we could substitute the words instructions for algorithm, there are some important characteristics of algorithms that make them distinct. An algorithm:

• Must be sufficiently precise and unambigious: every step must be clear to the actor implementing the algorithm.
• Must be repeatable, producing predictable outcomes based on the same inputs.
• Can be represented in a range of forms, including natural language, pseudocode, flowcharts, or symbols.

## Algorithms and programs

The term algorithm is often confused with program. Whilst the two are related, they have different meanings. An algorithm describes how to complete a specific task for any actor to carry out, whereas a program is the same algorithm implemented for a computer to carry out and written following the rules of a given programming language.

For example, the square-drawing algorithm below could be implemented using the pen tool in Scratch, the turtle module in Python, or even by a floor robot.

## One problem, multiple solutions

One of the really interesting aspects of algorithms is that, for any given problem, there are numerous solutions. Whether they be simple, elegant, speedy, or inefficient, they are all valid, as long as they produce the desired outputs.

In a group of learners, it’s entirely possible to get a different, but correct, algorithm created by each of them. At this point it’s important to acknowledge their correctness, but also to encourage conversations and compare the merits of each approach. It’s this higher-order thinking that programmers need, in order to evaluate the ‘best’ approach whilst being aware of alternatives.

## Why are algorithms so important?

Depending on where they are used, the performance of your algorithm can have a significant impact on how useful it is. Different aspects of algorithm performance are important in different cases:

• The algorithm that produces the route a Sat-Nav system will follow can choose from many different routes from A to B. It’s important to the user experience that they get recommended the optimum route: the user will not be pleased with a route that takes an hour longer than necessary. However, the user is unlikely to care too much if their journey takes a minute longer, as long as they get to their destination.

• In contrast, the algorithms governing the behaviour of a self-driving car are taking split-second decisions based on the available sensor data. A bad decision by the algorithm could have life-and-death consequences, so these algorithms need to be robust and trustworthy.

• Every time you do a web search, the search engine uses an algorithm to rank the most relevant pages for that search. In this case the user cares about the accuracy of the search results, but also the speed of the search. The quality of the search experience may impact upon the popularity of that search engine and ultimately the money a company is able to make from it.

## Problem solved?

Algorithms are everywhere, as we’ve seen, but that doesn’t mean that they are fully understood. Far from it! Whilst there are many problems which have been solved using algorithms and plenty of ‘standard’ algorithms, there is still work to be done. As a society we’re finding new problems we can solve with algorithms in science, medicine, arts, politics, and AI. New algorithms are constantly being invented and exisitng ones improved. To keep doing that, the world needs more computer scientists.

## Over to you

Here we’ve mentioned some examples of algorithms, but there are many more. Using the comments, can you:

• Write an algorithm to draw another 2D shape (eg a pentagon or star)?
• Comment on other learners’ suggestions, and guess the shape their algorithm draws?