For anyone who has studied and practiced algorithm problems, you have probably seen the FizzBuzz problem. But what is FizzBuzz and where did it come from? FizzBuzz is a traditional children’s game, it is played between two people where they each take turn to count from one to one hundred, and at number 3 and multiplication of 3, they say Fizz; at number 5 and multiplication of 5, they say Buzz; at multiplication of 3 and 5 they say FizzBuzz. While it is a very simple counting question, yet when we implement it into programming to solve this question, there are so many different approaches to it. Today we will take a look at one of the approach and my perspective on this problem.
If we look at the original question, the last line says: write the solution so it has as few characters as possible. A lot of times as programmers we want to minimize our codes (because we are lazy), and if we are repeating our codes a lot, it means we can refactor it to a simpler and better code. Taking a look at the code snippet above, first thing we notice is that the results printed out on the right side is correct, which means we didn’t break our solution with new codes. Looking on the left side, we notice that there are only 8lines of code, which is better than our previous solution. Not only that, if we want to change Buzz to 5 and multiplication of 5, it is simply a change of number on line 4. With this solution, not only we are minimizing the lines of codes that we have to write, if we decide to change the problem from 5 to 7, we are eliminating some of the errors of forgetting to change our code from 5 to 7.
In conclusion, there are a lot of ways to solve a simple FizzBuzz problem, but the approach and the perspective to this problem is what matters! Thanks for reading and have a wonderful day!
Fizz Buzz Implementation - GeeksforGeeks
Fizz Buzz is a very simple programming task, asked in software developer job interviews. A typical round of Fizz Buzz…