Fizzbuzz is a very common game played to teach children how to perform divisions. The game is simple, The player with which the game starts has to say one, then the other player says 2 (count incrementally) and so on. The catch is if the player has got a number that is a multiple of three, it has to say – “fizz”, or if it is a multiple of 5 it has to say “buzz”. Any number which is a multiple of both 3 and 5 should be replaced by the word “fizzbuzz”. A player who takes longer time to think or the one who make any mistake would be eliminated.
For example – 1,2,”fizz”, 4, “buzz”, “fizz”, 7, 8, “fizz”, “buzz”, 11, “fizz”, 13, 14, “fizzbuzz”, 16……….
FizzBuzz in software development –
In the world of software development, fizzbuzz (also written as FizzBuzz) is a very simple yet effective task that is to be given to a candidate in a coding interview to analyze his coding skills. It can determine whether a candidate is able to write high-quality code or not. The puzzle was invented by Imran Ghory.
The word FizzBuzz doesn’t have any specific meaning. It’s just a word representing a programming challenge. It is apparently a very effective test for choosing programmers who can write code efficiently. Even if you know how to use loops and conditional statements, “FizzBuzz” could be a real challenge for you!
Write a program that prints the numbers starting from 1 to 100. The numbers that are the multiple of 3 should be replaced by the word “fizz”, the numbers which are the multiples of 5 should be replaced by the word “buzz”, while the numbers which are the multiples of both 3 and 5 should be replaced by the word “fizzbuzz”.
Different people would take different approaches to write the code to solve this problem. However, the code would be judged not only by the output but also by the number of lines written, the effectiveness, and the time of execution.
So, in this article, we will see some of the cases of different approaches and their pitfalls. The code examples are written using ‘Python’ programming language. So let’s get started.
The most common solution that almost more than half of the people would give would be something like given below. However, it is not the correct approach-
i = 1 n = 100 while i <= n: if i % 3 == 0: print("fizz") elif i % 5 == 0: print("buzz") elif i % 3 == 0 and i % 5 == 0: print("fizzbuzz") else: print(i) i += 1
The problem with the above code is that if the first condition meets up, the next conditions would be obviously ignored by the interpreter. So for instance, if the number is 15, then according to the puzzle it should print “fizzbuzz” but the above code would print “fizz” even for 15. Because in the first condition we are checking whether the number is divisible by 3 or not. As 15 is also divisible by 3, it would print fizz even for 15 ignoring all the below conditions. (As only one condition would be executed).
So technically, the above code should be like this –
i = 1 n = 100 while i <= n: if i % 3 == 0 and i % 5 == 0: print("fizzbuzz") elif i % 3 == 0: print("fizz") elif i % 5 == 0: print("buzz") else: print(i) i += 1
This piece of code would give the correct output however it is not efficient.
The only problem with this code is that the approach of DRY (Don’t Repeat Yourself) is not followed as the expression “i % 5 == 0” appeared twice and the print statement appeared four times.
Another approach –
In this approach, we shall append the words fizz and buzz to an empty string according to some conditions.
i = 1 n = 100 while i <= n: word = "" if i % 3 == 0: word += "fizz" if i % 5 == 0: word += "buzz" if word == "": word += str(i) word = int(word) print(word) i += 1
Best approach –
In all of the above approaches, we have used the modulus(%) operator. Using that operator up to a particular value of n is fine, but the complexity would increase as the number increases. Reason being is the modulus operator is a costly operator as compared to other arithmetic operators like + and -.
The complexity of modulus operator is O(n)2. The use of modulus operator sometimes causes TLE (Time Limit Exceed) as it involves division, multiplication, and subtraction.
Only a few people would be able to write the solution without using the modulus operator. In this approach, we have used two counters named ‘countthree’ and ‘countfive’. We are incrementing both of these counters 1. Once the variable countthree is set to 3, we are appending the word “fizz” to the empty string, and reset the variable countthree again to 0. The same goes with the variable countfive. Once it sets to 5 we append the word “buzz” to the string.
It should be noted that we have used separate “if” statements which could execute simultaneously if both of the conditions become true. So in any case, if the countthree is equal to 3 and the countfive is equal to 5, the generated string would be “fizzbuzz”.
It should go somehow like this –
countthree = 0 countfive = 0 i = 1 n = 100 while i <= n: countthree += 1 countfive += 1 word = "" if countthree == 3: word += "fizz" countthree = 0 if countfive == 5: word += "buzz"; countfive = 0 if word == "": print(i) else: print(word) i += 1