Learn Python With Dhawal - 14 - Learning about Pseudocode and Algorithms.

Before we dive further into the programming challenges and solve them one by one. I want to pause here a little and explain the concept of Pseudocode and Algorithms. For any programmer, it is very important to know about algorithms and its kind of twin brother pseudocode.

What is an Algorithm?

Before we begin with other things, we need to know about what an algorithm or 'algo' in a slang/jargon language actually means here. Before we begin to actually code, we need to mentally prepare a plan or a blueprint on how we are going to execute a particular program. An Algorithm is basically a blueprint for the actual program that you are going to write.

An Algorithm is made of very simple statements like IF, DO, THIS, JUMP, BREAK. You can write a more complex algorithm but then it would be of a pseudocode than an algorithm. Coming back to the topic, Algorithm consists of a very basic step by step instructions that we need to perform to execute a particular function or a program.

Algorithm is a set of step by step logical instructions to the machine to get the work done.

Algorithm is said to be a blueprint of the program as

A Program is a set of step by step logical instructions to the machine to get the work done.

But the only difference here is that algorithm is most basic and doesn't have concepts of loops, unless you want to add it in there which makes it close to the pseudocode.  Algorithm also doesn't have any complex elements like data structures or 'Arrays' to be specific. Your pseudocode might have arrays and  data structures.

You can write code in any language studying its algorithm. Hence it becomes quite handy when there are multiple languages, platforms, and people working on a same set of project.

How to Write an Algorithm?

Consider writing an algorithm for a program where there are two numbers and if the number one is greater than another, lesser one is subtracted from the greater one and the result is shown.

Rules for Writing an Algorithm.

1. Always write simple steps, basically one computation or instruction at a time.
2. Use functions like IF, DO, THEN, JUMP, BREAK, GO, STOP, START, etc...
3. Always begin an algorithm with START and end with STOP.
4. You can write algorithm in lowercase but for the sake of simplicity, here we are using uppercase for the main actions like START, STOP, DO THIS, PRINT, etc..
5. No need to specify the data types of any of the variables.
6. Always number the steps of the algorithm
So, if you're ready, let's begin.

Write an Algorithm to find smallest of the two numbers and then subtract smaller one from the larger one and show the result.

1. START
2. A=10
3. B= 5
4. C=0
5. IF A>B THEN GO TO : 7
6. IF A<B THEN GO TO : 9
7. IF A=B THEN GO TO : 12
8. C=A-B
9. GO TO: 14
10. C=B-A
11. GO TO: 14
12. PRINT "No difference"
13. GO TO 15
14. PRINT C
15. STOP
Here are you can see, we simply wrote what we had to do, if you have to write it in C or JAVA or Python the same code might have different number of lines and maybe different way of execution as well. This was the very bare bones and basic example of an algorithm.

Remember we had previously worked upon a Calculator program? Try making an algorithm for the same program. You can email it to me or just comment the google docs link in the comment incase you want me to check and comment on it.

What is pseudocode?

Pseudocode is basically a form of advance algorithm where we can use complex things like functions, parameters, loops, if else etc, but there won't be any syntax in it. Since it is syntax free but looks almost like a code, it can be adapted into any coding language way more easily than the algorithm. It is easy to understand and resembles the actual coding formats very much.

Python at times almost looks like Pseudocode here and there, as there's almost no syntax and its very minimal. That is why it is very important to understand the importance of pseudocode and how it is different from python. In that manner you will understand how python is very easy to write compared to other languages.

Here's a simple example of a pseudocode for the same algorithm we wrote above followed by It's code in JAVA and It's Code in Python

Pseudocode

1. a=10
2. b=5
3. if ( a>b) then
4. c=a-b
5. print c
6. elseif (a<b) then
7. c=b-a
8. print c
9. else
10. print "No difference"

JAVA Code

2.     {
3.
4.         public static void main(String args[])
5.         {
6.             a=10,b=5;

7.             if (a>b)
8.             {
9.                 c=a-b;
10.                 System.out.println(c);
11.             }
12.             elseif(a<b)
13.             {
14.                 c=b-a;
15.                 System.out.println(c);
16.             }
17.             else
18.             {
19.                 System.out.println("No difference");
20.             }
21.         }// end of main
22.     }//end of class

Python Code

1. a=10,b=5

2. if(a>b):
3.     c=a-b
4.     print (c)
5. elif(b>a):
6.     c=b-a
7.     print (c)
8. else:
9.     print ("No difference")
As you can observe from the above examples, the length of the pseudocode and python is same for this example, at times in complex ones it might go even less than the pseudocode itself as you don't use data structures which are complex like lists and queues but you can implement the simpler ones like arrays.

Well the actual scope and limit of pseudocode might differ from where you are learning since some believe it should be kept simple as it can be easily adapted into other languages as well while some feel complexities should be used if you are going to do it only for a specific language.

You can say that pseudocode is a lazy programmer's code.

With this I hope the concepts about both algorithms and pseudocode are clear. We always write the algorithm first, followed by pseudocode if needed and then we start working on the actual program. So in future if you get a problem which you aren't able to solve directly, then instead of jumping into coding directly, you can play around with algorithms and then move up the ladder and make a complex code out of it.

<< PREVIOUS CHAPTER || NEXT CHAPTER >>

~*~*~*~

This series is totally authored by me (Dhawal Joshi). Any similarities found on the text, or codes or anything is purely accidental. All the sources of reference will be mentioned, linked and will be given the proper credits. If I miss anything or there's anything wrong, feel free to comment or send me an email and I'll try to edit it out. I am not a Python expert, I am sharing whatever I have learnt on my own and with a few sources around to refer from which will be mentioned. Also feel free to share this series with others so most can benefit out of it.

~*~*~*~

Do comment and share your thoughts about it! I'd love to know what do you think. Also, I'd keep updating it quite often so do follow the Website to get all the updates by clicking here.

Also, a minor headsup.... Obsessed is free to read on Kindle Unlimited! Do check it out. I'd be glad to read your reviews!
Share :

Are you getting regular fresh content updates? If not click on the button below.