If you don't know what Bizz Buzz is then you are, quite frankly, as ignorant as I was until 5 minutes ago. Until recently, I didn't know that the game Bizz Buzz and several variations of it had existed. Wikipedia to the rescue: http://en.wikipedia.org/wiki/Bizz_buzz.
The Bizz Buzz game, commonly known as the Fizz Buzz game is a game where the goal is to count numbers starting from 1. However, whenever the counter encounters a number which is divisible by 3 he says the word "fizz" in place of the number. Likewise, when the number is divisible by 5 it is replaced by the word "buzz" and when the number is divisible by both 3 and 5 the word "fizzbuzz" is said in place of the number.
By now, you're probably already 4 drinks in after finding out about this wonderfully simple game and can no longer make it past 6 without having to take yet another sip. Alas, this is a software engineering blog after all so why even mention a silly game here? Adapted for computer science, we can convert the game into a programming exercise: Print whole numbers from 1 to 100 but print "Fizz" in place of numbers divisible by 3, print "Buzz" in place of numbers divisible by 5 and print "FizzBuzz" in place of numbers divisible by both. It's easy to see how this seems like a trivial matter when solved by a computer program, but according to an article written in 2007, "the majority of comp sci graduates can't [write the fizzbuzz program]."
Here is my solution which, by the way, took me 4 minutes and 56 seconds to implement from the time I opened by editor to the time that I was satisfied it was correct. Obviously, I didn't do a lot of testing but I am still reasonably confident that the code satisfies the fizzbuzz requirements.
This program was by no means difficult to implement. In fact, I was quite shocked to hear that most computer science graduates struggled writing something to simple. Just to touch on the code briefly, I should explain the logic behind it just a bit. Often, when converting a word problem into logic, it is easier to implement the algorithm behind solving the problem by thinking backwards. The most specific case in this problem seems to be printing FizzBuzz when a number is divisible by both 3 and 5. Simple arithmetic tells us that a number being divisible by both 3 and 5 means the same thing as a number being divisible by 15, saving us from doing two comparisons instead of one. Next, it is important to realize that even though a number being divisible by 15 appears to be the most specific scenario, it is actually the most broad because it covers numbers divisible by 15, 5 and 3. Thus, in order to make sure that, when a number is divisible by 15, we ignore the fact that it is also divisible by 5 and 3, we perform this check first. The next two checks for divisibility by 5 or 3 can be interchanged and finally, if all checks fail, we know the number must not be divisible by any of the aforementioned numbers.
Personally, this logic struck me immediately, but I too can't claim that I am an expert programmer by a long shot. When first posed with this questions, I came to the realization that I had quickly forgot how to start off a Java program. It's almost embarrassing to admit but a couple simple lines of code to start a Java class and the main method in the class had slipped my mind because I had grown somewhat reliant on Eclipse filling in the "trivial" parts for me. The other mistake that I originally made in my mind was to put everything into one method without considering the need for effective testing, which is now more achievable through JUnit in its current two method form. Ultimately, the more complex logical parts of any program ought be split off from the main method so that each module can be tested individually. Fortunately, because the program was so fresh in my mind from having walked through it in my computer science class, I ended up writing this code without running into any issues beyond quick syntax errors. Unfortunately, for the exact same reason, my variables ended up being named the exact same way both times that I rewrote the program for my own edification and for that display of genuine lack of creativity, I had to frown upon myself.
No comments:
Post a Comment