The FizzBuzzTest inspiring many people to code up quick and dirty solutions.
...many quick. And quite a few dirty. Many "dirty" to the point of not actually working correctly!!!
----
See:
* http://golf.shinh.org/p.rb?FizzBuzz [statistics]
* Really nasty Ruby examples: http://www.megasolutions.net/ruby/-126_Solution_The-varieties-of-FizzBuzz-experience_-78356.aspx
* in Pyton, with additional "Fizz-Buzz on digits" rule: http://paddy3118.blogspot.com/2007/03/fizz-buzzpy.html
----
http://tickletux.wordpress.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
contains examples in
* Python - "Alexandre Vassalotti - January 25, 2007", "Jos'h - January 25, 2007", "Stephen. - January 25, 2007", "Patrick - February 5, 2007"
* Ruby - "john cromartie - January 25, 2007", "WC - January 25, 2007", "anon for this one - January 25, 2007", "Brian Mitchell - January 26, 2007", "Brendan - January 26, 2007"
* OCaml - "sibecker - January 25, 2007"
* LISP (Scheme) - "Andrew - January 26, 2007", (Common Lisp) "macnod - January 26, 2007"
* Java - "Tomas - January 29, 2007"
* FORTH - "George - February 2, 2007"
* Haskell - "Cameron Kerr - February 24, 2007"
* QBasic - "Syarzhuk - February 27, 2007"
* C - "Cryptnotic - February 27, 2007"
* PHP - "AlxShr - February 27, 2007"
* C# - "Patrik - February 27, 2007"
* Visual Basic for Applications in Excel - "matt - February 27, 2007"
* ''...and others...''
----
My ridiculously over-generalized solution in ScalaLanguage:
def replaceMultiples(x: Int, rs: (Int, String)*) =
rs map {case (n, s) => Either cond (x % n == 0, s, x)} reduceLeft ((a, b) =>
a fold ((_ => b), (s => b fold ((_ => a), (t => Right(s + t))))))
def fizzbuzz(n: Int) =
replaceMultiples(n, 3 -> "Fizz", 5 -> "Buzz") fold ((_ toString), identity)
1 to 100 map fizzbuzz foreach println
but at least it's DRY..."Fizz" and "Buzz" appear only once each. :)
----
My not so geneneralized ScalaLanguage solution that does not use the modulo operator:
object FizzBuzz extends App {
val three = (3 to 100 by 3) toSet
val five = (5 to 100 by 5) toSet
case class M(nums: Set[Int], phrase: String)
val t = List(M(three intersect five, "FizzBuzz"), M(three, "Fizz"), M(five, "Buzz"))
1 to 100 map { n => t.find(m => m.nums contains n) match { case Some(m) => m.phrase case default => n.toString } } foreach println
}
- JanekBogucki
----
Obvious in VBA
Public Sub FizzBuzz()
a = Array("", "Fizz", "", "Buzz", "")
For n = 1 To 100
f = a(1 + Sgn(n Mod 3)) & a(3 + Sgn(n Mod 5))
Debug.Print IIf("" = f, n, f)
Next n
End Sub
And R
fzbz <- function(n){c(n,"fizz","buzz","fizzbuzz")[1+(!n%%3)+2*(!n%%5)]}
for (a in 1:100) print(fzbz(a))
-- MarcThibault
----
Here is the one in TeX:
\newcount\x
\newcount\y
\newcount\z
\loop
\advance\x by 1
\advance\y by 1
\advance\z by 1
\ifnum\y=3 Fizz\y=0 \fi
\ifnum\z=5 Buzz\z=0 \fi
\ifvmode \the\x \fi\endgraf
\ifnum\x<100 \repeat
\bye
Golf:
\let~\advance\time0\day0\loop~\time1~\day1~\mit\ifnum\time=3\time0Fizz\fi\ifnum\fam=5Buzz\rm\fi\ifvmode\the\day\fi\endgraf\ifnum\day<100\repeat\bye
----
Readable perl version
my $i = 1;
while ($i <= 100) {
if ($i % 3 == 0 && $i % 5 == 0) {
print "FizzBuzz\n";
} elsif ($i % 3 == 0) {
print "Fizz\n";
} elsif ($i % 5 == 0) {
print "Buzz\n";
} else {
print $i . "\n";
}
$i++
}
----
Lua:
for i = 1,100 do
local n = false
if i % 3 == 0 then
io.write("Fizz")
n = true
end
if i % 5 == 0 then
io.write("Buzz")
n = true
end
if not n then
io.write(tostring(i))
end
io.write("\n")
end
----
ColdFusion:
#output#
----
Python:
for i in ("fizzbuzz" if i % 15 == 0 else ("fizz" if i % 3 == 0 else ("buzz" if i % 5 == 0 else i)) for i in range(1, 101)): print i
----
Lua (another):
for i=1,100 do
-- Set fizzBuzz to "", "Fizz", "Buzz", or "FizzBuzz" based on mod 3 and mod 5 tests.
local fizzBuzz = (( i % 3 == 0 ) and "Fizz" or "") .. (( i % 5 == 0 ) and "Buzz" or "")
-- Print non-"" fizzBuzz or print i.
print( (fizzBuzz ~= "" and fizzBuzz) or i )
end
----
C++ (with one abuse of control characters to avoid excess bools or ifs):
#include
int main()
{
for(int i = 1; i <= 100; ++i)
{
std::cout << i << '\r'; // return to beginning of line for overwriting with "Fizz" or "Buzz"
if(i % 3 == 0) std::cout << "Fizz";
if(i % 5 == 0) std::cout << "Buzz";
std::cout << '\n';
}
return 0;
}
----
CategoryInManyProgrammingLanguages