Day 6 of 10: Ingressive For Good Data Strucures and Algorithm Challenge

Peace AzubuoguPeace Azubuogu
2 min read

On Day 4 of this challenge, we were asked to perform some simple threading operations and today, I4G took it up a notch. Did i say a notch? I mean a hundred notches.

Instruction

You have the four functions:

printFizz that prints the word "fizz" to the console,

printBuzz that prints the word "buzz" to the console,

printFizzBuzz that prints the word "fizzbuzz" to the console, and

printNumber that prints a given integer to the console.

You are given an instance of the class FizzBuzz that has four functions: fizz, buzz, fizzbuzz and number. The same instance of FizzBuzz will be passed to four different threads:

Thread A: calls fizz() that should output the word "fizz".

Thread B: calls buzz() that should output the word "buzz".

Thread C: calls fizzbuzz() that should output the word "fizzbuzz".

Thread D: calls number() that should only output the integers.

Modify the given class to output the series [1, 2, "fizz", 4, "buzz", ...] where the ith token (1-indexed) of the series is:

"fizzbuzz" if i is divisible by 3 and 5,

"fizz" if i is divisible by 3 and not 5,

"buzz" if i is divisible by 5 and not 3, or

i if i is not divisible by 3 or 5.

My attempt

For someone coming from an analytics background and trying out Data Structures and Algorithms for the first time. I had a lot of catching up to do.

After a lot of googling and youTubing(still have more of these to do) on Python multithreading, I found a very simple code(I love them simple and concise) that worked perfectly and I applied the same concept to the problem.

It involved the use of Barrier from threading module.

First, i specified the number of threads the barrier can support(in this case, four threads). Then, i defined each function thread with the conditions specified for each for a range from 1 to n+1.

All four threads executes at the same time. For each function, i applied the self.bar.wait(). This ensures that a thread will wait till all threads are executed before proceeding to the next step in the iteration.

And that was all! You can view my submission details below.

day 6 submission .jpg

It was so exciting to finally get something i just watched from a youtube video to work after a million trials of God-knows-what. And to this, i say thank you to all the amazing tutors on YouTube, Stackoverflow and other sites that make life easy for programmers like me.

After this challenge, I would love to take a full course of Data Structures and Algorithms. Kindly recommend the best platforms for this.

Thank you!

1
Subscribe to my newsletter

Read articles from Peace Azubuogu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Peace Azubuogu
Peace Azubuogu