When time is tight and demands are urgent, it is easy to get lost and fail if there is no efficient method.
So I like to use the word “methodology” a lot in different fields.
Master certain methods, can be organized, down to earth efficient completion of each task.
The same is true for programming development.
During the development process, there are often a variety of requirements that can cause a variety of problems for different reasons.
When there is a problem in the system, how to solve the problem efficiently will not be assessed in the interview process, but it is closely related to daily work.
In this article, I will introduce the method of solving coding problems efficiently by summarizing the 4 steps.
First of all, the method described in this article was not invented by me, but was inspired by a book called How to Solve It by Paula Zergy.
Originally published in 1945, the book has sold more than a million copies.
The problem solving approach presented in this book has been used by many programmers and college professors to solve many of the coding problems in four steps.
In this article, we will use JavaScript as an example language to first raise the question:
Creates a function that adds two numbers and returns the value
The solution is divided into four steps:
Understand the problem
Make a plan
The execution plan
Checking back
Here is a step-by-step introduction.
Step 1: Understand the problem
In the face of requirements, the first thought of many students is to quickly code to avoid project delay.
This is inevitable, but it needs to be avoided.
Make sure you really understand the problem before you begin to solve it.
A poor understanding of the problem not only leads to ambiguity and wasted time in the development process, but also leads to misalignment of the final requirements and unsatisfactory results, and in some cases, rework.
Therefore, it is necessary to understand the problem carefully, figure out the part that you do not understand, and achieve a clear understanding of the problem.
To better understand the question, ask yourself some questions:
What is the input?
What are the inputs to the problem? In this article’s example, the input will be taken as a parameter to the function.
As you can see from the previous questions, our input will be numbers.
However, to take a more holistic view, we can ask:
Will the input always be just two numbers? What if our function receives three numbers as input?
In order to achieve a more in-depth and comprehensive understanding of the problem.
What is the output?
What does this function return? In this case, the output will be a number, which is the result of two numeric inputs.
Make sure you really understand the output.
Once you understand the problem and know the possible inputs and outputs, you can start working on some concrete examples.
Write a use case
Examples can be used as test cases for stability and health checks. Also, by writing use cases, you can improve your understanding of the code.
Start with a simple example or two possible inputs and outputs. Let’s go back to the addition function.
Let’s call this function add.
What is sample input? Examples of input might be:
//add(2, 3)
What is its output? To write sample output, we can write:
// add(2, 3)—> 5
This indicates that our function takes inputs 2 and 3 and returns 5 as its output.
You can also find extreme cases to consider by writing more complex use cases. What if our input is a string instead of a number?
- Make a plan
Next, make a plan to solve the problem.
When you design your plan, you can write it out in pseudocode.
Pseudocode is a simple linguistic description of the steps of an algorithm. In other words, pseudocode is an important part of the solution.
Write down the steps you need to take to solve the problem. For this article question, you can write:
// Create a sum variable.
Add the first input to the second input using the addition operator.
// Store the two input values in the sum variable.
// Return the sum variable as output.
Now, you have a plan to solve this problem step by step.
For more complex problems, Prof Evans says: “Think systematically about how humans solve problems.”
That is, instead of thinking about how to solve a problem in code, think about how to solve it in a manual way, which will help you see the steps more clearly.
- The execution plan
The next step in a problem-solving strategy is to execute the plan.
Use your pseudocode as a guide to write your real code.
Prof Evans suggests focusing on simple solutions.
The simpler your solution, the more likely it is to program correctly.
Taking our pseudocode, we can now write:
function add(a, b) { const sum = a + b; return sum; }
Don’t optimize too early!
That is, while implementing requirements, you might be thinking, “Wait, I’m doing this, this is inefficient code, let’s optimize it!”
First, just get a simple solution. The solution to the problem should be considered as a whole, rather than over-optimizing one part of the implementation.
Colt Steele offers good advice here: If you can’t solve part of the problem, ignore the part that tripped you up.
Instead, focus on everything else you can start writing and ignore the hard parts of the problem that you don’t know much about for a while.
After completing this section, please return to the more difficult section.
- Checking back
Once the solution is up and running, take some time to think about it and figure out how to improve it.
This is the time to refactor your solution into a more effective solution.
When you look at work, Colt Steele suggests asking questions that get you thinking about how to improve the solution:
What other approaches are available?
Can you understand it at a glance? Does it make sense?
Can the results or methods be used for other problems?
Can the performance of the solution be improved?
Can you think of other ways to refactor?
How do others solve this problem?
One way we can refactor the problem to make the code cleaner from the previous code implementation is by removing variables and using an implicit return:
function add(a, b) { return a + b; }
When I code myself, I always see solutions that are more elegant or efficient than the ones I came up with.
Therefore, by constantly reading other people’s code, or having your code reviewed by others, you can come up with more effective solutions.
In this article, we discuss a four-step problem solving strategy for solving coding problems.
Remember, problem solving skills are skills that make anyone better with time and practice.
Dry recommended
In order to facilitate everyone, I spent half a month’s time to get over the years to collect all kinds of iso technical finishing together, content including but not limited to, Python, machine learning, deep learning, computer vision, recommendation system, Linux, engineering, Java, content of up to 5 t +, I put all the resources download link to a document, The directory is as follows:
All dry goods to everyone, hope to be able to support it!
https://http://pan.baidu.com/s/1eks7CUyjbWQ3A7O9cmYljA (code: 0000)