Problem Solving
To be an IT developer means to be a problem solver. The good news is, that it actually doesnt matter too much what kind of problems in life you want to solve. If you follow a structured process you will eventually succeed provided you do not give up in between which is probably the main root cause why many problems remain unsolved. So lets discuss some of the techniques I have used to solve a seemingly easy problem.
The Problem
This problem illustrates how interesting my life is ;-):
Take a random sentence and capitalize the first letter of every word!
So provided the following sentence:
Capitalize the first letter of every word!,
the code should return:
Capitalize The First Letter Of Every Word!
Sounds simple right?
Problem solving techniques I have used
- Understand the problem:
"If I had an hour to solve a problem I'd spend 55 minutes thinking about the problem and five minutes thinking about solutions" (Einstein)
Although, the given problem was seemingly easy to solve at a first glance, having a closer look, there was a lot to be understood about the problem before I even wanted to jump into the exploration of potential solution.- How many words does the sentence have? Answer: infinite. More than one.
- Are some of the words capitalized already? Answer: Yes, could be.
- Is the input sentence provided as a string or as an array of words? Answer: String. This one actually took me a while to realise that this is even a relevant question to ask.
- What is the lenght of the words? Answer: Everything from 1 character to infinite.
- How are the words seperated? Answer: Spaces " ".
- Pseudo code:
I started to write down in simple words what steps might be required to solve the problem. I came up with:
// 1. Loop through each word
// 2. for each word access the first letter of the Word
// 3. turn it into upper Case
// 4. done! That's an easy one!
Little did I know ;-) - Try something:
I started to try out and experiment with different elements of the solution in isolation:- Create a loop through the sentence using for-loop
- Access the first letter of a word using word(1).
- Turn a string or a letter into upperCase using toUpperCase().
- Combine 2. and 3. in one line
- Reading error messages:
Working through the previous steps, I have encountered all sorts of errors which gave me various hints:- Wrong syntax
- on which line of code I had to look at resp. where it breaks
- What my actual output is vs. Expected output
- Where I have got something right!
- Console.logging
Console logging logs the current output of a function. It helps a lot to understand whether the current step does what it is supposed to do rather than having this big blackbox of nested functions which is impossible to untangle. At least this way we can simulate where things work and where things break. - Googling
Unfortunately none of my attempts so far worked to solve the problem entirely. So I was starting to google some functions that might be helpful. In the student.handbook there was a hint that map() might be useful. I have also realised at this point that some of the methods only work for arrays and others only for strings and that this was an important distinction to make. - Asking coaches / experts for help
Still. I couldnt make it work. So I had a sneek peak at a sample solution. You could consider that as asking an expert who has solved the problem before. That's when I have realised that there are many more methods to be used here in combination:- Split() to make an array out of the string
- Map() to loop through the elements in the array
- toUpperCase() to capitalize the letter
- Substr()to retrieve only a part from a string
- Join() to make a string out of the elements of an array
I eventually got there! Uff!
How I have felt throughout this process
I have experienced all kinds of emotions. At first I was stoic and focused but pretty soon became angry and frustrated. Why is this simple thing so hard?!Some pauses helped to calm down and think of what else I could do to get closer to the solution. The problem solving process above was a helpful guidance. Then there were, moments of relief when I found out WHY something is not working at least this helped to narrow down the possible solutions and reconfirm that I am not insane. And finally when I got that Woop, Woop message I celebrated myself with a beer. It's so rewarding to finally get something work!
What did I learn?
Heaps! Including:
- Trust the process. You will eventually get there.
- There are methods specific to arrays and others specific to strings and there are methods to turn strings into arrays and vice versa.
- Methods / functions have a scope within which they work. Assembling and calling them in the right way whith the right data parsed is crucial.