Fun with FizzBuzz
If these posts are interesting to you, please subscribe:
and share!
Also, feel free to follow me on twitter: @azakmay
Besides being a fun word to say, what is FizzBuzz?
If you’re a developer, you likely already know, as it’s purportedly a common interview question. While I have no idea of the truth of this, I have seen it spoken of as such; and have seen the task mentioned in courses as well as on programming-specific podcasts. With that in mind, a colleague who started learning to program had to implement it as part of a course. So, I thought: I hope I can complete this task. Thankfully, I was able to – otherwise, as someone who has spent most of their day programming for the past few years, I’d be deep in my feels right now.
Anyway, back to what actually is FizzBuzz?
The task is simple: Print integers 1 to N, but print “Fizz” if an integer is divisible by 3, “Buzz” if an integer is divisible by 5, and “FizzBuzz” if an integer is divisible by both 3 and 5.
Basically, if I give you the number 9, you should give me back Fizz, the number 10, and you give back Buzz, 30, and you give back FizzBuzz! This is implemented using a programming language. We will demonstrate with Python, where a simple and good solution might look like the following.
Phew, we did it. FizzBuzz implemented. We are feeling good. Using this interview question around FizzBuzz, my next thought was how could one show a solid understanding of Python l or whatever language – we happen to be using Python – using this same question. Now we don’t want to go overboard, but a few things came to mind that are good practices in programming
Readability
Naming
Documentation
Standard library
Testing
Without getting too crazy, could we beef up our answer to this question and demonstrate greater competency?
Here is what I came up with.
For the divisibility check, let’s pull that into its own function with documentation.
Note: I originally named this function ‘_divisibility_check’… name is hard!
Next, we know we are checking for 3 and 5, so let’s create explicit functions using Python’s standard library module: `partial` to show a deeper knowledge of Python while enhancing readability with very understandable function names.
Hopefully, these functions are clear: `is_divisible_by_3` and `is_divisible_by_5` check to see if a number is divisible by 3 and 5.
Now we need to incorporate these back into our `fizzbuzz` function from above, so
Now, anyone reading this should understand what is going on even without needing to know what the modulus (%) operator does.
Next, we want to add tests to ensure all our functions are doing what we intend for them to do.
And finally, we run these tests to see if they all pass; otherwise, we will need to debug the code.
And just for good measure, to show our understanding of loops and maybe a final sanity check.
All in all, this did not take long to implement, is rather robust for what we are doing, but shows good coding practices and understanding of the Python programming language even with such a simple task (although, admittedly very rigid design). Even with such a simple task as the one shown above, you can demonstrate a decent amount of understanding.
Appendix
Now if I was writing this as a function that is a little more flexible, I’d use parameters and would end up here: