Problem solving Patterns

clouded_knight

gbenga fagbola

Posted on April 5, 2022

Problem solving Patterns

Enter fullscreen mode Exit fullscreen mode
This is an introduction to common problem solving pattern

Hey Guys 😊,

So as discussed in the previous lecture, we would be looking at common problem solving patterns we can implement when faced with a problem set. So let’s jump right in.

Let me go through the intended approach before making a list of patterns I hope to go through.


How to Improve in Problem Solving

(i). Develop a strategic Plan:

I admit it can be really tempting to jump right into solving a problem set with little or no understanding, with hopes of navigating through it. But I propose you devise a sound enough plan for accomplishing such a task.

(ii). The mastery of common problem-solving patterns:

Of course, this would give you a grasp on the most common form which various problem sets might take. Thereby giving you foresight towards tackling these said problem sets.


Let Highlight the Problem-Solving Process

  1. Understand the Problem.
  2. Explore Concrete Examples.
  3. Break the Problem-set down.
  4. Simplify - make a proposed solution.
  5. Implement the solution to various case study.
  6. Refactor - optimise the solution.

Let now take a deeper look into the highlight above


Understanding the Problem set

  1. You should try to restate the essence of the problem set according to your understanding.
  2. Take a clear look at the intended input & expected output. Keeping in mind possible edge cases.
  3. Determine if the expected output can be determined from the input.
  4. Have a final glance through and ensure you have taken note of every important detail.

Explore Concrete Examples

  1. Look through every possible edge case, such as invalid input, the presence of whitespace or special characters etcetera in other to devise a solid algorithm.
  2. Make users' Stories to aid in a graphical are relatable presentation. Which can show a profound level of understanding and also help others understand the problem set and your intended solution.
  3. Make simple examples, then complex ones to show mastery.

Breaking things down

There are quite a few ways to break down the problem set (the algorithmic way) such as:

  • Sudo code

  • Flow diagram et Cetera.


Simplifying the Problem Sets

After following through the above steps, it is only expected that we get to finally simplify the problem and deduce a solution that would suffice.


Implementation of the said Solution

We should ensure our solution passes all intended test cases, both easy and challenging. While envisaging possibilities of inconsistent input that might arise and possible ways out.


Having a Look-Back.

I employ you to create the habit of having a much-optimized version of your solution. Answering the questions

  1. If there is the expected outcome
  2. Checking out other possible approaches
  3. How understandably is the solution (which refers us to the criteria we spoke previously about)
  4. It adaption to a similar problem set
  5. Can the Performance of the Solution be Improved? (in terms of time & Space Complexity)
  6. Check if it follows basic conventions and standards.

Example

Let's Go through a Simple Problem set and implement the above steps. You can try this yourself, and kindly note that we are bound to have different answers but a similar approach.

Question Write a function that takes a string and returns the frequency of each character in the given String, considering the laid down principles above.

From 1: Understanding the Problem

  1. In simple words, return the occurrence of each character in a given string.
  2. We can deduce a string as the intended input.
  3. Yes, the expected Output can be gotten from the input.

From 2: Exploring concrete examples

Ben was given the assignment to count the re-occurring characters in a list of countries. So he got fancy and employed an automated process. Via an algorithmic approach.


From 3: Breaking things down

  1. Defining the function
  2. Instantiation of a counter
  3. Examination of every character in the input
  4. Looking out for keen details such as similarities and count
  5. Return the output or statement based on expected edge cases.

From 4: Simplifying the problem set

function countCharacter(arr){

    let countResult = {};  

    for(let i = 0; i < arr.length; i++){       
        let countIndex = arr[i].toLowerCase();       
        if(countResult[countIndex] > 0){       
            countResult[countIndex]++;    
        } else {     
            countResult[countIndex] = 1;       
        }
    }
    return countResult;
}

countCharacter("hey guy!")
Enter fullscreen mode Exit fullscreen mode

result
{" ": 1, !: 1, e: 1, g: 1, h: 1, u: 1, y: 2}


from 5: Contemplate Edge-Cases

function countCharacter(arr){
    let countResult = {};  

    for(var char of arr){                
        char = char.toLowerCase();
        if(/[a-z0-9]/.test(char)){
            if(countResult[char] > 0){
                countResult[char]++
            } else {
                countResult[char] = 1;
            }
        }
    }
    return  countResult;
}

countCharacter("hey guy!")
Enter fullscreen mode Exit fullscreen mode

result
{h: 1, e: 1, y: 2, g: 1, u: 1}


from 6: Refactoring - Looking Back

function countCharacter(arr){
    let countResult = {};  

    for(var char of arr){                
        char = char.toLowerCase();
        if(/[a-z0-9]/.test(char)){
        countResult[char] = (countResult[char] || 0) +1;     
        }
    }
    return  countResult;
}

countCharacter("hey guy!")
Enter fullscreen mode Exit fullscreen mode

result
{h: 1, e: 1, y: 2, g: 1, u: 1}


Note, you can easily run the code blocks above in your chrome snippet to see the respective results yourself.

The above code snippet is a solution to the problem statement above, and do not worry if you don’t understand. I would start explaining line by line each code block I write from the forthcoming lectures as this is to help you understand the underlying concept first.

See you in the next part πŸ‘πŸ».

πŸ’– πŸ’ͺ πŸ™… 🚩
clouded_knight
gbenga fagbola

Posted on April 5, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Problem solving Patterns
javascript Problem solving Patterns

April 5, 2022