What is Pseudocode? - Here is a Four Step Guide to Solving any Coding Problem with Pseudocode

What is Pseudocode? - Here is a Four Step Guide to Solving any Coding Problem with Pseudocode

How to design programs and solve problems with Pseudocode

Some of us just started learning how to program. Some of us have been learning for quite a while now. Either way, computer programming can demands a great deal of practice and improvement.

Software Programming is the act of creating software programs for a computer to execute. Software programs are just series of instructions telling the computer what to do or what tasks to perform.

Just as a script writer for a movie would write a detailed script of what an actor is going to do in a screenplay. Software developers write programs which a computer is going to execute.

Now, as a beginner, you may have this abstract, far-fetched notion of programming. You may believe that it's very difficult, has a lot to do with maths or is a little more complex for the human brain.

The thing is, programming is something we do every day. When we plan how we spend our time in day, we are actually doing programming.

When we decide what places to visit, what tasks we perform and so on, it's programming.

When you tell your kid what steps to take when she notices anything unusual within the environment, you are actually programming her to act.

The same concept occurs with software Programming. You are basically telling the computer what actions to take under different conditions.

You will typically write a computer program in a particular programming language. Some of them are human-readable than the others. The human readbale ones are otherwise called high-level languages and they include JavaScript, Python, Go and Rust. The less readable ones are termed low-level languages because they are closer to machine code (binaries) than to any human language. They include C, C++ and Lisp.

Code is the translation of this series of logic in computer-readable statements.

What is Pseudocode

Pseudocode is fake syntax. It is an informal, less structured and contrived language created for the sake of simplicity.

Writing in a real computer programming language demands great deal of accuracy, knowledge and strict adherence to the syntactic rules. With pseudocode, you can avoid this.

When adopting Pseudocode, it is importance to keep your programming style as consistent and clear as possible.

To give an example of a pseudocode, how it differs from a real programming language like JavaScript and how helpful using pseudocode can be in program design, I am going to write a program in simple English-based pseudocode and then translate it back into it’s JavaScript equivalent:

THE CHALLENGE

To demonstrate pseudocode usage, I am going to be using a simple coding test which made a post about in my weekly coding challenge series.

The task is simple: We are to create a likes() which takes in an array of "likers" and returns a new custom message depending on the number of people who liked the post.


likes[]   // "No one likes this"
likes["Jack"]     // "Jack likes this"
likes["Jack", "Jacob"]      // "Jack and Jacob likes this"
likes["Jack", "Jacob", "Jill"]      // "Jack, Jacob and Jill likes this"
likes["Jack", "Jacob", "Jill", "John"]      // "Jack, Jacob and 2 others liked this"

MAPPING OUT THE PROGRAM WITH PSEUDOCODE SYNTAX

Before getting into the real coding, the us solve this problem using a language we are more familiar with.

?? begin program

// Define the `likes` function. 

likes (array of likers) 

// In Case Array is empty and no one likes this post

if (array_is_empty) return no one likes this post!

// In Case Array not empty and contains likers, proceed below

if (number_of_likers_in_array ===1 ) return "sole_liker likes this post"

else if (number_of_likers_in_array === 2) return "liker_1 and liker_2 likes this post"

else if (number_of_likers_in_array === 3) return "liker_1 and liker_2 and liker_3 likes this post"

otherwise {
  remaining_likers = Total_no_of_likers - 2
  return "liker_1, liker_2 and (remaining_likers) liked this post!"
}

?? end program

One important thing to note about this code is that it doesn't adhere to any strict syntax rules and is instead based on my rules.

You can structure your pseudocode however you want. What matters the most is that you understand your code very well.

JAVASCRIPT SYNTAX

Now that pseudocode has been defined and distinguished from an actual Programming language syntax, it's time to implement that program in a valid JavaScript code


function likes(...names) {
  if(!names.length) {
    return "No one likes this";
  }

  let count = 0;
  names.forEach(name => count++);

  if(count == 1) {
    const firstName = names[0];
    return `${firstName} likes this post`
  } else if (count == 2) {
    const firstName = names[0]
    const secondName = names[1]
    return `${firstName} and ${secondName} likes this post`
  }
  else if (count == 3) {
    const firstName = names[0]
    const secondName = names[1]
    const thirdName = names[2]
    return `${firstName}, ${secondName} and ${thirdName} likes this post`
  } else {
    const remainder = count - 2;
    const firstName = names[0]
    const secondName = names[1]
    return `${firstName}, ${secondName} and ${remainder} others likes this post`
  }

}

const likers = ["Jack", "Jill"]

console.log(likes(...likers));

HOW TO SOLVE CODING PROBLEMS WITH PSEUDOCODE

Pseudocode is created to simplify the process of program design.

That way, we as programmers focus on the logical part and not have to contend with technical details like coding syntax and arrangements.

If you are looking to solve a programming challenge either in your project or in a coding challenge platform like codewars, using psedocode is a very useful tactic.

Below are the five steps to solving any programming problem with Pseudocode:

First, it is very important to have an understanding what a function is and how it works.

A function is a block of code which solves a particular task. Let me give you a simple example of this: When you take in food (input), the only output you want is nourishment.

Lets assume that the body system is a function which takes in food as input and gives out energy and nourishment as output. There many processes involved in transforming that food into nourishment.

After each step, the body gets closer to its end goal of giving nourishment to the body. Each part of the body (function) is charged with solving a mini-problem such as breaking down the food, mixing it with gastric juice, extracting nutrients etc.

A function also works the same way. It takes in input, works on the input little by little until it arrives at the final solution.

Understand the question asked.

If you don’t understand the question, you will not be able to lay out the steps needed to solve the problem and arrive at a solution.

Whether you are solving a problem in your project or on a test platform like Codewars, clearly understanding what is required to be solved gets you halfway to actually solving the problem.

Break the problem down into chunks

Breaking the problem into series of smaller problems to be solved. Use a note or paper to list out the micro steps in a logical and sequential form. For example, if you want to validate an email of a user, you’ll need to first access the email address value, then you need to store it in a variable (container of data), then you need to test it with an email expression and so on.

Each of these micro tasks will lead up to the solution. After you have done this,

Now bring in real code and tools.

This is when the Mozilla Developer Network, W3Schools, Stack Overflow and other coding sites come into play. For every micro task you want to solve, find a language construct which will help you do it.

Don’t be afraid of asking questions.

WRAPPING UP

Pseudocode is a helpful method of designing protoype programs.

This 4 step formula has proven to be very effective when it comes to writing programs. As you learn and improve with time, writing in real code may even become a second nature to you.

YOU MAY ALSO LIKE:

Thank you for reading and see you soon.

P/S: If you like articles like this follow this blog to never miss an update. If you are learning JavaScript, you’ll definitely want to check out my JavaScript Notes.

 
Share this