I noticed some folks on here were sharing some coding challenges, and I thought it might be fun to share some of my own (and shamelessly promote my Sample Programs project in the process).
The Challenge
Fizz Buzz is that notorious interview question almost everyone in the industry knows. Ironically, I don't know of anyone who has every been asked it. Regardless, it's a fun problem to tackle because it involves a little bit of everything: loops, conditions, math, etc. And, there are countless ways to solve it.
For the purposes of this challenge, here are the rules:
Write a program that prints the numbers 1 to 100. However, for multiples of three, print “Fizz” instead of the number. Meanwhile, for multiples of five, print “Buzz” instead of the number. For numbers which are multiples of both three and five, print “FizzBuzz”
Here's a segment of the output:
1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
If you're feeling festive, you can swap the terms for whatever you like (i.e. EasterEgg, AprilShowers, etc.). In any case, the solution should be executable in any language of your choice. The more obscure the language, the better!
The Solution
I'll kick off the challenge with my favorite solution to the problem in Java:
public class FizzBuzz {
public static void main(String[] args) {
for (int i = 1; i < 101; i++) {
String output = "";
if (i % 3 == 0) {
output += "Fizz";
}
if (i % 5 == 0) {
output += "Buzz";
}
if (output.isEmpty()) {
output += i;
}
System.out.println(output);
}
}
}
If you're curious how this solution works, Stuart Irwin wrote an excellent article about it. Along with this solution, I've been collecting several others in a repo called Sample Programs. After you drop your solution below, you should see if its missing from the collection. We'd love your contribution!
Top comments (38)
CSS
Haha I love that you can do this in CSS.
There's several ways to do it in C++ :)
The Classic
The "GOSH, MOM, IT'S GENERIC"
The "Calculated At Compile Time (stare into madness edition)"
I raise you the compile time calculated fizzbuzz in Nim ;)
C++20 isn't available yet, but we're a bit closer to this approach. :)
Interested in adding this to the collection? 😁
That last one scares me. Haha I’m not sure I’d know where to start reading it.
Thanks for jumping into the challenge with some great additions. 😃
Take that github.com/EnterpriseQualityCoding...
number 3 is the Cthulu of FizzBuzz
These actually fail the FizzBuzz challenge because they print numbers that are divisible by 3 and/or 5. The challenge specifically states to print Fizz, Buzz, or FizzBuzz INSTEAD of the number.
Here's my implementation in Racket
The
print-list
function is a bit redundant, since(map fizzbuzz (range 1 101))
will already print the resulting list to the console.Ah come on @avalander - surely you should've written a FizzBuzz DSL in Racket? 😉
I should, but I don't know enough Racket for that yet 😅
Great stuff! I like racket a lot, but I haven't written any code in it myself. The fact that there are so many dialects of it is pretty cool to me.
Thanks! I've just started learning it myself. I can recommend the book Realm of Racket if you want to give it a shot.
Sql anybody?
`
Because using a cursor is too mainstream? :)
Since nobody has done Javascript yet, here's a crazy implementation.
Consider how easy it is to extend to print
'fazz'
for multiples of 7.I appreciate the commitment to the obscure. Haha these are great.
That's the whole point of the exercise, right? :D
Oh absolutely! Got any code golf solutions?
Hmm... the best I can come up with right now is 85 chars. Nothing really clever, just sacrificed readability for space.
Another fun one, albeit longer, is this.
Elixir
Here is another example using as a Module and using pattern matching. It looks hilarious :P
Ooh, this would make a nice addition to the repo.
Go ahead.
Didn't see a C# solution, so here's mine (with some linq love):
Here's some fun from the world of Common Lisp
Inoffensive version
Offensive FizzBuzz Builder Macro
For when you want to define your own custom fizzbuzzer. Nice and easy to extend.
Most Offensive Macrogeddon Too Hot for TV Version
For when you want to define your own custom matcher logic for each word in your custom fizzbuzzer. Still lets you put a single number in for 'divides by' logic.
Bring on the parentheses!
Here's a shot at an Erlang version, but I barely know Erlang.
Is this a Python solution using a massive list comprehension?! I like it.