People who can’t program still get hired as programmers.
I first heard about the Fizz Buzz code from Jeff Atwood on Coding Horror web page. His point has nothing to do with being able to actually code a solution for ‘Fizz Buzz ‘, but the ability to code for a solution. Fizz Buzz is just the example provided.
To be honest, I am not surprised that people get employed to program who can’t even program something as simple as ‘Fizz Buzz’! Frequently people don’t get hired because they can prove (and I mean beyond a benefit of a doubt) that they can perform well in the roll. For instance. Take the Spelling mistake I just provided to you. Roll. The word is ‘Role’. So, if I can’t spell the word Role, then how on earth can I be a good coder? (if you don’t know the answer, then you are not what I consider a good coder). Be honest, all too often an individual gets employed because they are good at embellishing the answers to the interview questions, and one or more of the Interviewers gets a good ‘gut feeling’ about the individual. (for more information watch Simon Sinek’s You Tube Video). Plus, lets face it, what proof do I have that YOU wrote your own Job Application?
Over the years in which I have seen these sorts of Fizz Buzz requests, I have tried several different ways to write them – just for fun. Personally, I find that Fizz Buzz holds absolutely no challenge other than the ‘Fun Factor’. I have seen several ways to create them. Ranging from almost One liners (I am sure this could be done with Power Shell), to intricate class files (all hail the wonders of abstraction and inheritance). Although, I am yet to see it in Binary 😉
I thought I would post just ONE (non nobis solum) solution, which I have not seen anyone else post using VBA (Visual Basic for Applications).
OK. Let’s define the Fizz Buzz problem:
Loop through a set of numbers (N), say up to 100 starting at 1 (n).
If n is divisible by 3, print Fizz.
If n is divisible by 5, print Buzz.
If n is divisible by 15, print
FizzBuzz Woof. Could someone please use a bit of creativity?
If n is not divisible by 3, 5 or 15, print the number (n).
Here is my VBA solution.
(NB: I most often use VBA because next to 99% of the population can, if they so desire, load it up and try it out)
Public Sub FizzBuzzWoof() 'I am using a single Data Type (4 bytes of Nominal Storage Allocation) ' I chose to do this as I want to store at least one decimal place. Dim iThree As Single Dim iFive As Single 'I decided to dim as a byte, as the question is from 1 to 100 - not infinity. 'However, if you want to go higher, use a different type. Dim i As Byte For i = 1 To 100 'or you could create a constant. iThree = i Mod 3 'a number divisible by 3 will return 0. iFive = i Mod 5 'a number divisible by 5 will return 0 '53% of outputs will be a numerical number. How do I know this? 'Because I sat down with a Spreadsheet and did the maths. 'Here is a thought: if our output is a number deal with it first?! 'While THIS code is only '4' lines long it's the principle of the matter. If iFive <> 0 And iThree <> 0 Then Debug.Print i 'If you check for both Three AND Five at the same time, you are actually checking for 15. 'While fifteen only occurs 6% of the time, it just simplifies the overall testing if we do this now. ElseIf iFive = 0 And iThree = 0 Then Debug.Print "Woof" 'Three will occur 27% of the time. So, Ill place it above the Five test. ElseIf iThree = 0 Then Debug.Print "Fizz" 'Five will occur 14% of the time. ElseIf iFive = 0 Then Debug.Print "Buzz" End If Next i End Sub
The code I have provided isn’t rocket science, it isn’t complicated. but it is somewhat thoughtful, it does work and it shows that I can think about how to solve this problem, hopefully in a way that others didn’t initially conceive of.