The last year of my life has been all about learning. I learned a lot at The Iron Yard. I continue to learn every day at the new job, not to mention this blog and some private projects I’m working on. More specifically, I’ve learned to write code in several different programming languages. I wanted to sit down and review some of the things I’ve learned this year and compare and contrast how I might solve a common problem in several different languages. The problem I chose to solve is Fizz Buzz, a classic new programmers problem. Its not a hard algorithm to write, but it is involved enough to exercise some simple core features of most languages. It is also an excellent problem to solve over an over again while you are working on software craftmanship.
For the uninitiated, a Fizz Buzz program will count up from 1 to some number, say 25, and print one of the following for each number:
Here is an example implementation in Ruby:
1.upto(25) do |x| output = nil output = output.to_s + 'Fizz' if x % 3 == 0 output = output.to_s + 'Buzz' if x % 5 == 0 puts output || x end
Stepping thru this code, we see the program does the following:
If you ran that code, you would see:
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz
Pretty cool right? Its ok I guess. You know what would be really cool? I think it would be really cool to compare this solution against another language and see what we can learn from it. Stay tuned, I’m planning on doing just that.
Finally, review other implementations in ruby. We may discuss some in the future as we progress in our investigation.
I hope that helps.