This content originally appeared on Level Up Coding - Medium and was authored by Thenjiwe kubheka
If I was a hot shot head chef at a world class restaurant, I would use “The chicken test” as a litmus test for hiring chefs.
I would ask them to cook me chicken! Why chicken you may ask?
If a chef cannot cook chicken and make it tasty, then they probably are not ready to cook world class meals
Same is applicable to a software engineer, if they cannot code the simple kids game of Fizz Buzz then, they are probably not ready to produce world class software!
So what is Fizz Buzz?
Fizz Buzz is a kids game played to strengthen their mathematical skills. Played by two people they each take a turn to count 1,2,3,4,5,6,…. but every time there is a multiple of 3 they say Fizz. And every time there is a multiple of 5 they say Buzz and with numbers such as 15 which is divisible to both 3 and 5 they say Fizz Buzz. Another exception is numbers which are not multiples of 3 or 5, then the kids will just call the number.
Let's write a program that does this, and we will be using Python:
def fizzbuzz(i):
if i % 3 == 0:
return "Fizz"
if i % 5 == 0:
return "Buzz"
if (i % 3 == 0) and (i % 5 == 0):
return "FizzBuzz"
return i
print(fizzbuzz())
In the code above tells our Python interpreter to check if our variable i is a multiple of 3, if it is then output Fizz.
If the statement is false move on to check if i is a multiple of 5, if it is then return Buzz.
If that statement is also false move on to check if i is a multiple of 3 and 5 and if it is return to us FizzBuzz.
If non of those are true, return to us i, which will most probably be a number. I ran this on Google colab and look at the outputs I got.
The screenshots above show i as 3,5 and 15. The first two outputs of 3 and 5 are correct, but the last one of 15 outputs Fizz and we are expecting Fizz Buzz.
This is a common problem with decision statements, it only moves to the next condition if one is False. But 15 is applicable to both statement 1 and 2, so our not so intelligent friend the Python interpreter chooses the first option and hence we get Fizz as our input.
We can also be lazy as our Python interpreter friend, and just switch our statements;
That's sorted, it is indeed a working model, can it be improvised so that it prints 1 to 100 ?
def fizz_buzz(i):
for i in range(1,101):
output = ""
if i % 3 == 0:
output = "Fizz"
if i % 5 == 0:
output += "Buzz"
print(output or i)
print(fizz_buzz(100))
The code above is telling our Python interpreter to run through the numbers 1 to 100. Every time there is a multiple of 3, add Fizz to the empty string output.
Then move on to multiples of 5, and add Buzz to the string, no need o check for multiples of 3 and 5, as it will automatically check. mI also ran this on Google Colab:
Well you are most welcome to add to my code, I have attached it!
Additional Resources
Fizz Buzz Interview Question was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Thenjiwe kubheka
Thenjiwe kubheka | Sciencx (2021-08-22T20:22:02+00:00) Fizz Buzz Interview Question. Retrieved from https://www.scien.cx/2021/08/22/fizz-buzz-interview-question/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.