A step-by-step guide to solving problems in programming

A step-by-step guide to solving problems in programming

·

3 min read

I have always wondered how experienced programmers come up with solutions to problems in no time and know what method to use. I want to attain that god-level but for someone with less experience it can be a little daunting thinking through these things, and trying to come up with a solution and that is my aim for this article.

In this article, I’m going to walk you through the steps of going from problem to problem-solving and everything in between. I also got the opportunity of refreshing my mind and knowledge on this topic too as we had a weekly workshop in the Sailscasts community.

What is PREP and why is it so important?

P.R.E.P is a method for approaching coding challenges. It helps view a problem logically by breaking it down into four steps. Let’s take a closer look at what PREP stands for…

  • P stands for Parameter: In this first step, you must consider the input(s) of the function, the type(s) of the parameter(s), and how to name them. Most problems involve writing a function anyways so just think of this step as what you’re giving to the function to get something back.

  • R stands for Return: Here, you have to think through what the function will return. Think of it as the “output”. After all, you’ve given the function something it has to reciprocate.

  • E stands for Example Next is to think of examples similar to what you want to achieve. Most coding practice websites like Codewars, give examples that serve as guidance for the problem you want to solve. You can also think of it as a placeholder value.

  • P stands for Psuedocode: Pseudocode refers to a plain language description of algorithmic steps. Think of it as “fake code”. All that matters with pseudo-code is comprehension. You do not have to bother so much about syntaxes, semi-colons, and all the programming-related conventions. It is you writing out steps or sequences of actions and instructions you would take in solving a given problem. You can read more about pseudocode here

Some benefits of P.R.E.P are:

  • PREP can be used for whiteboarding during technical interviews. This gives the interviewer the impression that you know what you are doing.

  • It also helps in breaking down coding problems and gives a better understanding to them.

In addition, with consistent practice and application of this method or approach, you can build your mind to start thinking like a programmer.

Practical example on how to apply this method in a coding challenge:

Let's assume we're given a task to convert a user's password or email address into asteriks; here's how I'd approach this problem.

I'd start by using the P.R.E.P approach to simplify things for me.

- P: type of parameter is a string and that's the input aswell
- R: expected output is a string
- E: "hello" --> "*****"
- P: First step would be to loop through the length of the string
  Second step would be to replace the character of the string with the asteriks in this case.
Here's my solution to this challenge.

let word = "hello"
console.log(  "*".repeat(word.length) );

We could take this a step further by making into a function;

Function toAsterisks(password) {

  return "*".repeat(password.length);
}
console.log(toAsterisks(password));
// Expected output: *****

Also, I understand that this might not be a go-to solution for others and that’s okay. This is my solution and had to simplify it as much as possible to make understanding easier. Please do let me know what you think about this method and how you approach programming problems by direct messaging or mentioning me on Twitter. I’d be glad to see them and happy reading!