Algorithms in Python

Algorithms are a methodical and accurate description of how to solve a problem. The definition of an algorithm is a step-by-step procedure of well-defined executable instructions designed to perform a task or solve a problem.

ALGORITHMS – GIVE THE COMPUTER INSTRUCTIONS

An algorithm has many similarities to a program where the major difference is that we can describe algorithms regardless of program language. An algorithm can be seen as a job description on how to solve the problem. It describes steps the program should take. It is important to remember that there are usually many algorithms to solve a problem and that it is important trying to find the simplest and most effective solution to the problem.

An algorithm can be seen as a job description on how to solve the problem.

For simpler problem solving, you can manage by having your algorithm in your head but as the difficulty and complexity of the tasks increase, it is almost necessary to think through the algorithm carefully before you start coding. So a good tip is to have a habit of drawing / writing down the solution process for the task.

ALGORITHMS ARE A…

  • Exact description of how to solve a problem
  • Provides a higher abstraction of the problem without using code
  • Is independent of program language
  • Describes the program step by step

DESCRIBE YOUR ALGORITHM WITH PSEUDOCOD

Pseudocode is a way to describe an algorithm without using programming. Psedocode is therefore not a real program language (like for example Python), but a way of describing an algorithm with, for example, text or flow charts. We use pseudocode to get a better overview of how the program works and make it easier to explain the algorithm.

FLOWCHARTS IN Python – STRUCTURE YOUR CODE

When drawing algorithms, you usually use so-called flowcharts that help you show what kind of action the program should perform and when. The three most common symbols in a flow chart and when they are used:

Algorithms
  • Start / stop – shows where the algorithm starts and ends, that is, the boundaries

 

  • Activity – activities that are performed in the process can be, for example, “turning on the lamp”, “starting the engine”, etc.

 

  • Decision – choices and decisions in the algorithm, are usually designed as a yes / no question

EXAMPLE – FLOWCHART IN Python

To the right, we will see a simple example of an algorithm for a thermometer that will check the temperature. If the temperature is below a certain limit, the radiator must be started.

 

1. The algorithm starts by starting the thermometer.

 

2. The thermometer then checks to see what the temperature is.

 

3. Is it below 10 degrees? If yes, then the radiator should be started and the algorithm terminated.

 

4. If no, a delay of 30 minutes should be initiated and then the algorithm restarted at step 2.

Algorithm in Java Example