livepython.net
CHAPTER 03Programmer Mindset
3.2  ALGORITHMS

What Is an Algorithm? Explained with Everyday Examples

What Is an Algorithm?

The word algorithm sounds fancy, but it's simple: an algorithm is a clear list of steps that solves a problem.

You already know lots of algorithms:

  • a recipe for tea,
  • directions from your home to school,
  • the steps to tie your shoelaces.

Each one has a goal, and steps that reach it.

Algorithm vs program

What's the difference? An algorithm is the plan. It can be written in plain words, on paper or in your head. A program is the same plan written in a programming language, so a computer can follow it.

AlgorithmProgram
What it isThe steps to solve a problemThose steps in code
Written inAny language, even plain EnglishA programming language like Python
Who follows itA person or a computerA computer

That's why programmers often write the algorithm first. Once the steps are clear, writing the code is just translating them, like translating a sentence from English to another language.

Every algorithm has

  1. A start: what you have at the beginning (water, tea leaves, a cup).
  2. Steps: clear actions, in order.
  3. An end: the result you wanted (a cup of tea), after which the steps stop.

An algorithm is the plan. A program is that plan written in code.

your turnPick the right idea

Which of these is an algorithm?

Think about it, then pick one.

Algorithms All Around You

Algorithms run your day, even when you don't notice them. Here are three that computers follow millions of times a day.

Taking money from an ATM

  1. Insert your card.
  2. Enter your PIN.
  3. Choose "Withdraw".
  4. Enter the amount.
  5. Take your cash.
  6. Take your card.

Each step must happen in this order. The machine won't give cash before it checks your PIN.

Sorting students by marks

  1. Get the list of students and their marks.
  2. Compare the marks.
  3. Arrange the students from highest to lowest.

Your school's report software, and every "sort by" button on a shopping website, follows a sorting algorithm like this.

Calling an elevator

  1. Someone presses the button on a floor.
  2. The elevator checks where it is now.
  3. It moves up or down to that floor.
  4. It opens the door.

Notice step 3: "up or down". The elevator makes a decision based on where it is. Many algorithms make decisions like this. In Python, you'll make decisions with if, in chapter 8.

Your turn to spot them

Look around: a washing machine program, a traffic light, a game's rules. Each one is an algorithm. Learning to spot the steps in everyday things is great practice for thinking like a programmer.

Algorithms are everywhere: any time steps in order reach a goal.

your turnOrder the lines

Put the ATM steps in order

  1. Choose Withdraw
  2. Take your cash
  3. Insert your card
  4. Enter your PIN
  5. Enter the amount
These lines are mixed up. Put them back in order, then press Check.

What Makes a Good Algorithm

Not every list of steps is a good algorithm. A good one has four things:

A good algorithm isBecause
Cleareach step means one exact thing
Completeno step is missing
In the right ordereach step can only use what came before it
Finiteit ends after a number of steps

What goes wrong

Look at these broken algorithms for making tea:

  • Not clear: "Make it hot." Make what hot? How hot?
  • Not complete: "Add tea leaves. Pour into a cup." The water was never boiled.
  • Wrong order: "Pour into a cup. Boil water." You can't pour tea that isn't made yet.
  • Never ends: "Stir. Then go back and stir again." When do you stop?

That last one is a real problem in programming. A program that never ends is said to be stuck in an endless loop. You'll learn how to make code repeat in the loop chapters, and how to make sure it stops.

Test your algorithm

Before writing code, "run" your algorithm in your head, or on paper, step by step. Pretend you're the computer and follow the steps exactly. If you get stuck, find a missing step or end up somewhere strange, fix the steps first. It's much easier to fix a plan than to fix code.

A good algorithm is clear, complete, in order, and it ends.

your turnPick the right idea

What's wrong with this algorithm?

1. Pick up a ball. 2. Throw it. 3. Go back to step 1.
Think about it, then pick one.

From Steps to Code

Here's a simple method you can use for every coding problem, from your first practice question to big projects.

The 4-step method

  1. Observe. Read the problem carefully. What should the program show or do?
  2. Break it down. Split the job into small pieces.
  3. Plan. Write the steps in plain words, in order. This is your algorithm.
  4. Code. Turn each step into a line of Python.

Example: a shop receipt

Observe: we need a receipt with a title, the item and its price, and a thank-you.

Plan:

  1. Show the shop name.
  2. Show a line.
  3. Show the item and its price.
  4. Show a line.
  5. Say thank you.

Code: one step, one line:

PYTHONedit me
print("Sunny's Shop")
print("-" * 16)
print("Tea\t$2.00")
print("-" * 16)
print("Thank you!")
OUTPUT
Sunny's Shop
----------------
Tea	$2.00
----------------
Thank you!

Each line of code matches a step of the plan, in the same order. That's the magic: once the plan is clear, the code almost writes itself.

When the code doesn't match

If the output looks wrong, compare it with your plan, line by line. Often a step is missing, or two steps are in the wrong order. The plan helps you find the problem fast.

Plan in words first, then turn each step into a line of code.

your turnFix the code

Fix the order so the receipt prints top to bottom

It should print: Rocky's Cafe Coffee $3 Thank you!

print("Thank you!")
print("Rocky's Cafe")
print("Coffee $3")
The lines are mixed up. Put them in the right order, then press Run.
your turnType the output

Type what this prints

print("Plan\nCode")
Read the code line by line, then type the output exactly.

Practice: Algorithms

Solve these to lock in what you learned in this topic. Each one runs in your browser.

  1. 30.Python Program to Print the Steps of an ATM WithdrawalEasySolve