Every programming problem involves some kind of algorithm. Algorithm is a process or set of steps to accomplish a certain tasks or a step by step way to solve a problem. There are about 700 programming languages. Here is a list. So what to learn and why to learn that’s most of the people stuck on. In my perspective You can learn any. Learning basics of some on-demand programming language is not a harm. But you must select a 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 is like a curse. So,that’s all about leaning and selecting.Now I will mention the steps which I follow to solve a problem. I have broken it into 5 steps. Here I go then.
- 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 word?
- 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 thatif(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 numberif(/[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…..(^ _ ^)