This series of problem solving exercises are based on problems found on Codewars https://www.codewars.com/ or Hackerank https://www.hackerrank.com/dashboard.
This is the first problem in the course. There is no need for any prior knowledge.
No specific tools are required, although you will need to make notes so either:
You will be presented with a problem. You will learn how to understand the problem and break the problem down into steps that help you work towards a solution.
Todays problem can be found here: https://www.codewars.com/kata/5834fec22fb0ba7d080000e8.
After the class finishes, you should submit a solution to the problem using the link above.
You are going to make toast fast, you think that you should make multiple pieces of toast at once. So, you try to make 6 pieces of toast at once using all the toasters available.
You forgot to count the number of toast you put in there, you don't know if you put exactly six pieces of toast into the toasters.
Define a function that counts how many more (or less) pieces of toast you need in the toasters. Even though you need more or less, the number will still be positive, not negative.
You must return the number of toast the you need to put in (or to take out). In case of 5
you can still put 1
toast in:
5 --> 1
And in case of 12
you need 6
toasts less (but not -6
):
12 --> 6
function sixToast(num) {
// you code goes below this line
}
The first step in solving any problem is understanding that problem.
When you are sure that you know the meaning of all the words or phrases in the problem desicription, move on to the next step.
This next step is called using abstraction. That is just a fancy word for the process of filtering out - ignoring - the characteristics of problems that are not needed in order to concentrate on those that are needed. It is also the filtering out of specific details. From this, we can create an idea of what is to be solved (see https://www.bbc.co.uk/bitesize/guides/zmhpfcw/revision/1 for more information).
Make a note of each important piece of information you have identified that is important to the problem description.
Discuss your solution to this step as a group. Does everyone have the same list of important elements or are there differences? Resolve your differences so everyone agrees what is important.
When we solve problems with code, there is often input that is required. Input is data that changes depending on the circumstances.
For each set of input, our solution should return
an output value. The output is what you might describe as the "correct answer given the input".
num
num
is bigger than, or smaller than, 6
.The problem description almost always has some examples that show various different problem inputs and the output that goes with each input.
The first example is this one:
5 --> 1
The input value that we are calling num
, is 5
and the output value is 1
. We can compute the output by calculating the result of 6 - num
which becomes 6 - 5
which gives the output value 1
.
The second example is:
12 --> 6
The input value that we are calling num
, is 12
and the output value is 6
. If we follow the same process as the first example we get 6 - num
which becomes 6 - 12
which gives us the (incorrect) result -6
. To get to the correct answer we need to turn -6
into 6
(multiply by -1
).
2 --> 4
9 --> 3
It often helps to decompose the problem into smaller problems. We call this step decomposition.
num
is less than 6
. The solution is obtained by the formula 6 - num
.num
is greater than 6
. The solution is more complicated because we either need to calculate num - 6
OR calculate 6 - num
and multiply the result by -1
.The second case is "more difficult" in that we need to do something different to the "easier" first case.
Are there any other cases? What if num
is equal to 6
?
It is acceptable (and often important) to solve the decomposed smaller problems one by one. In this first step we will develop a solution that works ONLY in the case that num
is less than 6
.
It is important to note that up until now there has been no programming code written or even mentioned. There was a bit of mathematics involved in calculating the result.
When we develop a solution we often use pseudo-code. This is a cross between program code where that code is easy to write and an English description when the step is not easy to write in one program code statement.
return __________Fill in the gap in the example above. Don't worry about getting the code correct, just fill in what the step needs to achieve using English.
Here is the pseudo-code with the gaps filled in:
return 6 - num
We could put this solution into the training step on Codewars and we may see that sometimes it produces the correct output and passes a test and other cases it produces the wrong output and fails a test. That behaviour is ok, we expected that.
To improve our solution we need to include a check in our solution to identify whether we are solving the simple case when num
is less than 6
, or not.
if
statement that checks whether num
is less than 6 and when that condition is true returns the value of num - 6
.Here is some improved pseudo-code. Note that is less than
is not valid Javascript but while we are beginners, we won't worry yet about the correct Javascript code for the statement :
if (num is less than 6) { return 6 - num }
We can use this new version in the Codewars training screen after translating is less than
into correct Javascript. Although the solution is improved, it may not pass more tests.
Our solution so far detects the easier case and returns the correct value for that case. Here we develop a solution for the alternative case.
if (num is less than 6) { return 6 - num } else { const difference = 6 - num; return ______________________ }
Here is our complete plan for a solution in pseudo-code:
if (num is less than 6) { return 6 - num } else { const difference = 6 - num; return difference * -1; }
We will stop here.
Later, in your own time:
Math.abs
and try to come up with a neater solution.Math.abs
to come up with a version that doesn't have an if
statement?There were a lot of steps into solving this problem. They were important.