paint-brush
5 Repeatable Steps I Follow to Solve Almost Any Coding Problemby@sujan-chhetri
1,198 reads
1,198 reads

5 Repeatable Steps I Follow to Solve Almost Any Coding Problem

by Sujan ChhetriAugust 26th, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

5 Repeatable Steps I Follow to Solve Almost Any Coding Problem: Understand the problem, Explore Examples, Solve a problem and Solve / Simplify a simpler problem. Look Back and Refactor are the steps I follow to solve almost any coding problem. I have broken my problem-solving process into 5 steps:Understand the problem. Explore examples with empty inputs. Solve the difficulty in what you are trying to do, then incorporate that difficulty into a simplified solution.
featured image - 5 Repeatable Steps I Follow to Solve Almost Any Coding Problem
Sujan Chhetri HackerNoon profile picture

Every programming problem involves some kind of algorithm. An algorithm, in this sense, is a process or set of steps to accomplish a certain tasks; or simply a step-by-step way to solve a problem. There are about 700 programming languages. (Seriously: Here's a list.) So, what to learn and why to learn it - that's what most people are stuck on. From my perspective, you can (and should) choose learn any language. Learning the basics of one on-demand programming language will do you no harm. But: you must select ONE language that becomes your magic spell, and you should always be ready to create something out of it. Learning varieties of languages and not being able to do anything really well is like a curse.

So, those are my thoughts about selecting a programming language to learn. Now I will outline the repeatable steps I follow to solve almost any coding problem.

I have broken my problem-solving process into 5 steps:

  • Understand the problem
  • Explore Examples
  • Break it Down
  • Solve / Simplify
  • Look Back and Refactor

Let's look the steps into more detail with examples. As I am deeply in love with JavaScript I will be using that. Feel free to use your own magic spell.

Example question: Count the value of only numbers and alphabets from a given string "Hello There Blogue IS A BEauTIFUL %^$##@123451112231144 !!!!!$%#"

1. Understand the problem

Can I restate the problem in own words? What are the inputs that go into the problem? What are the outputs that should come from the solution to the problem? Can the outputs be determined from the input? Do I have enough information to solve the problem? How should I label the important pieces of data that are a part of the problem?

Input:
integer?
float?
string?
str("Hello There Blogue IS A BEauTIFUL %^$##@123451112231144 !!!!!$%#")
Output:
integer?
float?
string?
result: { h: 2, e: 3, l: 2, o: 2, t: 1, r: 1, f: 1, u: 2, c: 1, k: 1, y: 1 }

2. Explore Examples

Which can help understand the problem better. Provides sanity checks that your eventual solution works how it should. Start with simple examples. Move to complex example. Explore examples with empty inputs. Explore examples with Invalid inputs.

//example1
let str = '';


for (let i = 0; i < 9; i++) {
  str = str + i;
}
console.log(str);

//example2
const sentence = 'The quick brown fox jumps over the lazy dog.';

console.log(sentence.toLowerCase());

//example3
function testNum(a) {
  let result;
  if (a > 0) {
    result = 'positive';
  } else {
    result = 'NOT positive';
  }
  return result;
}

3. Break it Down

  • Explicitly write out the steps you need to take.
  • Comment out steps to be used
  • function countValues(str){
                                       var result = {};
                                      for(var i=0;i<str.length;i++)
                                     {
                                       var chara = str[i].toLowerCase();
         //conversion of string to lowercase as the same letter is in small and in capital,we don' want that
                                             if(result[chara]>0){   
                                                result[chara]++;
         //increments the count if condition is grater than 0;
                                               }else{
         //else makes it 1
                                                      result[chara] = 1;
                                               }
                                               }
            }
                                               console.log(result) ;
            }
    countValues("Hello There Blogue IS A BEauTIFUL %^$##@123451112231144 !!!!!$%#") 

We are near to the solution but its not totally correct. We still need to eliminate the spaces and the symbols.

4. Solve / Simplify

Solve a simpler problem.

Find the core difficulty in what you are trying to do.

  • Temporarily skip the difficulty.
  • Write a simplified solution.
  • Then incorporate that difficulty back in the code.
  • function countValues(str){
                                       var result = {};
                                       //for(var i=0;i<str.length;i++)
                                       for(var chara of str)
                                       {
                                       //var chara = str[i].toLowerCase();
                                       chara = chara.toLowerCase();
                                       
                                       //R.E to check lowercase and number
                                       if(/[a-z0-9]/.test(chara)){
                                       result[chara] = ++result[chara] || 1;
                                       
                                       // if(result[chara]>0){
                                       
                                       // result[chara]++;
                                       // }else{
                                       //                   result[chara] = 1;
                                       // }
                                       }
    }
                                       console.log(result) ;
    }
    countValues("Hello There Blogue IS A BEauTIFUL %^$##@123451112231144 !!!!!$%#") 

Now our solution is complete. But In real life application we check the speed of the approach,how fast the code executes to give output.So, we follow step 5.

5. Look Back and Refactor

Can you derive the result differently?

  • Can you understand it at a glance?
  • Can you use the result or method for other problem?
  • Can you improve the performance of your solution?
  • Can you think of other ways to refactor the code?
  • How have other people solved the same problem?
  • function countValues(str){
                                       var result = {};
                                       for(var chara of str){
                                       
                                       if(isAlphaNumeric(chara)){
                                       chara = chara.toLowerCase();
                                       result[chara] = ++result[chara] || 1;
    }
    }
    console.log(result) ;
    }
    
    
    function isAlphaNumeric(chara){
                                       var code = chara.charCodeAt(0);
    if(!(code > 47 && code < 58)&& !(code > 64 && code < 91)&& !(code > 96 && code < 123))
    {
    return false; 
    }
    return true;
    }
    countValues("Hello There Blogue IS A BEauTIFUL %^$##@123451112231144 !!!!!$%#")

When we compare the speed of the code in step 4 (55% slower) . This code is pretty much faster in execution.

That's all I do for solving problems .Different people have their own perspective. You can try these steps and see the result.

Learn, Code , Create repeat.....(^ _ ^)

Previously published at here.