Print in order
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:
Pause all the firing of the functions
Create a condition for when they should be fired.
Implementation
The implementation was a little tricky but these are the steps that were followed:
Created a list
self.fire
in theinit
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
).Reset the items of the
self.fire
list in each of the functions, that'sfirst
,second
andthird
.For the
first
function, the value of the first itemself.fire[0]
was set to be trueA condition in the second function was set using
while
loop to pass the function ifself.fire[0]
was not true.By doing this, the
second
function fires only when thefirst
function has fired.Reset the
self.fire[1]
to be true before moving from the second function.In the third, another
while
loop was set up that will pass the code ifself.fire[1] = False
, making thethird
function fire only when thesecond
function has fired and sets the value ofself.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
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