# Problem Solving

## **Summary/Overview**

How to solve problems is a learned skill. With repeated exposure to problems and from repetition of trying to solve them problem solving will become easier. Until then it is sheer will and perseverance and not getting frustrated that will help you reach your goals.

&#x20;**Lesson Goals:**

* Create A Plan
* Breaking Down A Problem
* Base case
* Useful Strategies

## **Create A plan**

Always have a plan. This is one of the most important rules. Do not engage in random trial and error. Trial and error is fine but **random trial and error is not**. If you have not yet solved the problem in your head you cannot possibly solve it in code.  Your plan may alter or even become useless but it is an important first step to tackling any problem

#### **Tips on creating a plan**

* Read the problem multiple times
* Restate the problem - approach it by working backwards or from a different starting point
* List the constraints and if stuck loosen one
* Draw out what needs to happen
* Write pseudo code
* Focus on small approachable steps

## **Breaking Down A Problem**

I cannot stress this enough. This is a common problem students run into. They try to solve the problem in one complete step.  Problems become much easier with time and exposure to coding problems. Computers are literal and need detailed precise instruction or they fail. For example to compare a word computers cannot simply evaluate the entire word as whole but each letter is evaluated step by step one at a time. &#x20;

{% hint style="info" %}
Breakdown your algorithm into as small of steps as you can
{% endhint %}

For example Let’s solve this problem if a string is a palindrome \
( a word that is the same backwards and forwards ie: madam)

\
**Palindrome Problem**\


![](https://lh4.googleusercontent.com/4GKc8m8XN4X4peLDYcgmzMci6Zzjrf8Q8LJBPtLidZSOGzvHJnNP1xHmSSgh6EEdT3mGt3y9gW92qVQFoUla_1OQGHZrMEkkiztUCmpFFS3DieAPpbvlkaNP7qNJx_K7mCZ3vauu)

{% hint style="info" %}
Trying to compare two words as a whole seems easy but computers need a direct step by step instructions aka an **algorithm** to solve this.
{% endhint %}

![Palindrome Problem Broken Into Steps ](https://lh5.googleusercontent.com/4rBcOERfXkhT11HXFKHqaF4AV507GC8HBf-NnCjTb1fswVdr-zilM8muFMC_ZU_jsjnmT6gqdTS0yOuDutYDmgrkNq7kDsbZfvqr8Qis6zB5m2dSv8W2p-G3LjtR675JQz-GRSNf)

We broke our problem down into 4 parts now but still **Step 2** is too large. Let’s break down step 2 into its own group of problems.\


![Step 2 Further Broken Down](https://lh3.googleusercontent.com/K4fkB1s-nJnuzNwbIJNMxYZjIuoRG7qyfwD2KYb_IOrMG16pr0XXQZjFc7fWf6DajoIKmTzybwqBO3efCVBXst5fBDDKNq2FSLVxaAEUSr2zMRGfjApe_3wbpubmxTsGVf138O_4)

![Step 2 full breakdown](https://lh6.googleusercontent.com/RAMii1EvyRgQSUoWt66pzfYF2_A3glhB7ICCzhGjbgJvu1vtIlAC236ej6GBtZZhetQcaeRHdzUDnb2HSsf2KWQa9H81gAroQG3gJOixvvpukw85XeZLEo3lvh6a5lLwzpS-medU)

Now we have solved step 2  from our original plan let’s put it together

![](https://lh3.googleusercontent.com/i5aVlK8Ek2kAq62hjbssW4nTewdi_6sbRnk0HAKEbxWFgj25oc699wFVd7p_JqzABc1Ll9N0e2l18VkYUBkaO9fQ1J8rr-gHtvdI8wDZA8pDcceBHUPVaG7DubgYckBmHmgR8tQq)

## **What is a base case?**

Many times in programming a problem has a one key condition to our whole problem and the rest is just iterating over the rest of the problem repeating the solution.

What was the base case in our palindrome function?

The last step was our base case. Appending the last character of the word to our reverseWord variable and repeating this process over and over. \
&#xNAN;_**Commonly your base case will be inside a loop.**_

## **Solved?**

Once we have solved a problem it is important to test it with different inputs. In this case what happens if we inserted numbers, or capitalized letters? Our code will break if we do not handle these situations. So it is important to handle edge cases after finding a base solution. It is very common to solve a problem then refactor your code to be more robust and efficient.  \


{% hint style="info" %}
**Key Point:** You will not have perfect solution on the first attempt or even second.  Do not let perfection be the enemy of progress.
{% endhint %}

## **Recap**

We started with a large problem broke it down into much smaller steps. Then looked at those steps and restated step 2 of our problem into even smaller problems that we know how to do. This is critical to creating a plan of attack to solving our problems. Sometimes just getting started on what you know will jumpstart the task at hand.\
\
Other strategies involve reducing the constraints, making the problem a little easier, maybe you can solve a simpler version of the problem or different part of the problem.  Creating analogies to past problems you have solved, look for similarity. Lastly experiment but experiment with a purpose _**console.log**_ each part of what you expect, always confirming your beliefs. Remember we are dealing with a flow of data and the computer does each thing step by step, line by line and we can capture those steps with _**console.logs**_

All of these strategies help your mind see the problems from different angles and break through. So many times in history you hear the solution just came to someone, but it is because they had an approach and kept trying to view the problem from different angles.\


Another tip if stuck is to take a break, work on another problem so your mind can process it. Many times I have come back to the problem and it was easier to solve. My favorite trick though is when I have been stuck on something hard I make sure I am thinking about it as I fall asleep. Your mind is more open to different connections in this half sleep dream state and can view the problem from different angles much easier. Many famous inventors would fall asleep thinking about a problem while holding something in their hand. Only to drop it when they fell asleep to leverage this state of open mindedness.

## **Key Concepts:**

* Do not get frustrated
* Create a Plan and pseudo code it
* If a it isn’t solved in our mind then we cannot code it.
* Break down the problem, sometimes multiple times
* Solve what we know
* Reduce complexity, lighten constraints
* Take a Break when stuck for long periods of time
* Sleep on it

## **Reference and Optional Reading**

* [**https://www.codeproject.com/Tips/833768/The-Beginner-Programmers-guide-to-Problem-Solving**](https://www.codeproject.com/Tips/833768/The-Beginner-Programmers-guide-to-Problem-Solving)
* [**https://simpleprogrammer.com/2011/01/08/solving-problems-breaking-it-down/**](https://simpleprogrammer.com/2011/01/08/solving-problems-breaking-it-down/)\
