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
- Understand the Problem.
- Explore Concrete Examples.
- Break the Problem-set down.
- Simplify - make a proposed solution.
- Implement the solution to various case study.
- Refactor - optimise the solution.
Let now take a deeper look into the highlight above
Understanding the Problem set
- You should try to restate the essence of the problem set according to your understanding.
- Take a clear look at the intended input & expected output. Keeping in mind possible edge cases.
- Determine if the expected output can be determined from the input.
- Have a final glance through and ensure you have taken note of every important detail.
Explore Concrete Examples
- 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.
- 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.
- 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
- If there is the expected outcome
- Checking out other possible approaches
- How understandably is the solution (which refers us to the criteria we spoke previously about)
- It adaption to a similar problem set
- Can the Performance of the Solution be Improved? (in terms of time & Space Complexity)
- 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
- In simple words, return the occurrence of each character in a given string.
- We can deduce a string as the intended input.
- 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
- Defining the function
- Instantiation of a counter
- Examination of every character in the input
- Looking out for keen details such as similarities and count
- 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!")
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!")
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!")
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 ππ».
Top comments (0)