There is a chance that you may have heard that there is no right way how to solve a problem. Every coder has a method, a step-by-step guide on how to find the solution, and this is personal, this is creative freedom.
Not everyone thinks the same, and this is good news. Allowing us to have a different approach for a complex task, and look for the most commons solutions for the repetitive ones.
We can go through some steps to the problem-solving method:
- Understand the problem
- Plan a Solution
- Break into small pieces
- Translate to code.
In the first three steps, you don't need an editor.
Let’s start it.
Every time a code-related problem appears, it seems to rush the anxiety to open your favorite editor as soon as possible. First, you need to understand the problem.
Read the problem out loud, talk to you about what the problem is asking. Clarify everything you didn't understand. Ask questions!
There is nothing wrong to don’t fully understanding the problem at first.
If someone tells you that this is easy, this person is lying.
Now, it’s time to start sketching, simulate inputs, draw arrows for the output. Take notes on what kind of instructions are happening to obtain that result. Let's plan a solution!
The magic word here is pseudocode.
Pseudocode is the bridge between the program and the algorithm.
You can read it better here: https://www.unf.edu/~broggio/cop2221/2221pseu.htm
Do you remember your sketch before?
Keep taking notes, but now give a name for your arrows.
Is it conditional? Is this repetitive? Should I loop through it?
There is no perfect plan.
- Identify all the steps.
- Identify the parts of the problem.
- Guide yourself through your logic.
There is a high chance that your solution may change. And trust me, it is not a waste of time.
trial and error is part of the process. Trust the process!
If you consider yourself a perfectionist, you may think, why? Why would I even try if there is a high chance it won't work?
Because the simple step of planning gives you the idea of the whole thing, what works, what don't.
Here you are going to use everything you have studied. And it is an opportunity to learn on-demand.
You don't know the tools or building blocks to this specific task. Search, study for this purpose, and return to your pseudocode.
There is no right or wrong here, do in a way you understand and help you.
There are many coding challenges that can be solved with one instruction. There will be times when you’re going to need the evaluation of one instruction, to go to the next step. Since most of the kinds of stuff that we do in our daily lives we do automatically, we might think that coding is the same thing.
For example, to drink water:
- Go to the kitchen
- Open the container that holds the drinking glass
- Find a drinking glass
- Pick up the drinking glass
- Walk to the source of drinking water
- Put the drinking glass in a position to be filled.
- Fill the glass with water
- When the glass is full ⇒ Drink.
Do we think in all of these steps? No.
Each of these steps is linked, you need to complete one instruction to go to the next one. All these processes are known as Algorithms.
Get your sketch again, and divide, break your problem, everything that seems complex, break it. Divide into chunks, solve all those chunks, then you can solve the whole.
Keep it short, and simple.
The next step is to solve the problem. Using your notes and your pseudocode as your guide, write out your actual code.
Now you can open your IDE and write your code. There will be times when trying and observing the results is the best way, called Experimentation. However, this works best when you have domain about what you’re doing.
Don’t expect for typing and magically working.
What if you can’t solve the entire problem?
If you got stuck, there is no problem looking for other solutions and how other people solve them. But return to the same problem a while later, and try to do it again.
As I said in the beginning, solving problems is a whole new skill set.
Only by practicing and exposing yourself, over and over, you’ll improve it.
All solutions start with a plan.