Coding challenge Day-4

Coding challenge Day-4

Fourth day of I4G coding challenge

Print in order

Question

Today's challenge came with a BIG shock for me. The shock was the fact that I couldn't use JavaScript to solve it (Leetcode didn't include JavaScript in the options). At this point, I just felt like moving on from it without even looking at the question. But then I just said to myself.

There should be a way!

Now let's see how I was able to do it.

Thought process

When I read through the question and I saw that the task was to order the firing of three functions in order without knowing how the operating system will schedule them. The two steps to take were:

  1. Pause all the firing of the functions

  2. Create a condition for when they should be fired.

Implementation

The implementation was a little tricky but these are the steps that were followed:

  1. Created a list self.fire in the init since it will apply this across all the other functions in the class. This list held two items. The two items were Boolean value (False).

  2. Reset the items of the self.fire list in each of the functions, that's first, second and third.

  3. For the first function, the value of the first item self.fire[0] was set to be true

  4. A condition in the second function was set using while loop to pass the function if self.fire[0] was not true.

  5. By doing this, the second function fires only when the first function has fired.

  6. Reset the self.fire[1] to be true before moving from the second function.

  7. In the third, another while loop was set up that will pass the code if self.fire[1] = False, making the third function fire only when the second function has fired and sets the value of self.fire[1] to be true.

What I learnt

Using Python was a little bit tricky since my knowledge of it was not strong. It was fun using it though.

The major thing I learnt was that when I attempted the solution first I used an if block instead of a while loop. This led to only the first function firing. I had to go and refresh my knowledge on both and I learnt that while loop will keep firing till the condition is no longer met but if block will fire just once.

Conclusion

Answer

Looking at the output (memory usage and runtime) I wasn't impressed but the fact that I got the code to run was a big plus considering I was in an unfamiliar zone (Python zone).

I was able to use 14.5MB of memory and a runtime of 3383ms

Thank you for reading through

Day four(4) done, Six (6) more days to go!

You can follow me on Twitter