FizzBuzz Solution Dumping Ground

You all are using the wrong language. Stare in awe at the power of Perl:

use FizzBuzz;
my $fb = new FizzBuzz;
$fb-print;

1 Like

I agree it is scary how few people can actually code. That being said, which version is better?

for (x=1;x101;x++)
{
	sprintf(temp,"%s",x%3 ? "":"fizz");
	sprintf(tem1,"%s",x%5 ? "":"buzz");
	strcat(temp,tem1);
	if (!temp[0])
		sprintf(temp,"%d",x);
	printf("%s\n",temp);
}

for (x=1;x101;x++)
{
		if (!(x%3)  !(x%5))
			printf("fizzbuzz\n");
		else if (!(x%3))
			printf("fizz\n");
		else if (!(x%5))
			printf("buzz\n");
		else
			printf("%d\n",x);
}

I actually code primarily in python for its cleanliness and maintainability plus convenient methods for handling various data types, but… For the sake of curiosity, I figured I’d go for the executable line noise version (e.g. perl)

perl -e ‘for (my $i=1; $i=100; $i++) {($i%3==0 and $i%5==0)?print “FizzBuzz\n”:(($i%3==0) ? print “Fizz\n”:(($i%5==0) ? print “Buzz\n” : print “$i\n”))}’

lua is nice!

for i=1,100 do
fizz= (i%3==0) and “Fizz” or ""
buzz= (i%5==0) and “Buzz” or ""
print(i,fizz…buzz)
end

I’ve become infected with the FizzBuzz fever, too.

main = putStr . unlines .
foldr (zipWith ($)) (map show [1…100]) $
[replace “FizzBuzz” 15, replace “Buzz” 5, replace “Fizz” 3]
replace s n = fix ((replicate (n-1) id ++ [const s]) ++)

Console.Writeline(“1”);
Console.Writeline(“2”);
Console.Writeline(“Fizz”);
Console.Writeline(“4”);
Console.Writeline(“Buzz”);
Console.Writeline(“Fizz”);
Console.Writeline(“7”);
Console.Writeline(“8”);
.
.
.

Yeah it’s not elegant (on purpose). But it shows that I can follow the directions and produce code that works. If the interviewer wants to do a code review and ask me to defend my methodology, I’m happy to do so.

For some reason,(maybe because I am now only a hobbiest) I still use qbasic for brainstorming and pseudo-code)
X=0
Do
X = X + 1
outstring$ = ""
If x mod 3 = 0 then outstring$ = "Fizz"
if x mod 5 = 0 then outstring$ = outstring$ + "Bizz"
if len(outstring$) = 0 then outstring$ = str$(x(
print outstring$
Loop Until X = 100

Here’s a “clear” Lisp version:

(defun fizz-buzz ()
(loop for i from 1 to 100
do (cond
((= 0 (mod i 3) (mod i 5)) (format t “FizzBuzz~%”))
((= 0 (mod i 5)) (format t “Buzz~%”))
((= 0 (mod i 3)) (format t “Fizz~%”))
(t (format t “~a~%” i)))))

lua still nice even doing what the problem states!

for i=1,100 do
fizz= (i%3==0) and “Fizz” or ““
buzz= (i%5==0) and “Buzz” or ““
number= not ((fizz==””) and (buzz==””)) and “” or i
print(number…fizz…buzz)
end

(python)

for i in range(1,100):
if i % 15 == 0: print(str(i) + ’ fizzbuzz’)
elif i % 5 == 0: print(str(i) + ’ buzz’)
elif i % 3 == 0: print(str(i) + ’ fizz’)

(c#)

for (int i=1; i=100; i++)
{
if(i % 15==0) Console.WriteLine(string.Concat(i, " FizzBuzz"));
else if(i % 5 == 0) Console.WriteLine(string.Concat(i, " Buzz"));
else if(i %3 == 0) Console.WriteLine(string.Concat(i, " Fizz"));
}

seq 1 100 | sed ‘0~3 s/[[:digit:]]$/Fizz/
0~5 s/[[:digit:]]
$/Buzz/’

That’s a relief. I’m like “Dang! What if I’m one of the ones who can’t do that?!” The script below is probably a little inelegant, but it works. Took about 5 minutes, but part of that was talking to a co-worker about nasal irrigation.

?php
for($i=1; $i=100; $i++) {
if ($i%3 == 0) echo “Fizz”;
if ($i%5 == 0) echo “Buzz”;
if ($i%3!=0 $i%5!=0)
echo $i;
echo “br\n”;
}
?

Or how about,

$s = ‘’;
for($i=1; $i=100; $i++) {
$s .= ($i%3==0)?“Fizz”:"";
$s .= ($i%5==0)?“Buzz”:"";
$s .= ($i%3!=0 $i%5!=0)?"$i":"";
$s .= “br\n”;
}
echo $s;
?

I prefer readability to compactness.

I dont know a lot of the languages used in here, but im guessin about one fourth of the answers to FizzBuzz are wrong… I mean:

for num in range(1, 101):
if not num % 3:
print "bizz"
elif not num % 5:
print "buzz"
else:
print num

I dont know Python but that code looks like it doesnt print the bizzbuzz, am I rigth or is Python just weird?
i hope Im wrong cause this is sad…

I can’t believe no one has put up the Perl Obfuscated version of the FizzBuzz:

map{$=(!($%3)!($%5)?“caddeidd”:(!($%3)?“cadd”:(!($%5)?“eidd”:blush:)));y/cadei/fizbu/;print"$_\n"}(1…100)

My obfuscation powers are weak this morning, I’m sure it could have been done much better.

Incidentally, if this were the interviewee’s answer, I would probably not hire him. I might invite him to become a member of Perl Mongers, though (http://www.pm.org)

/**
 * Faster for a small amount of recursions, breaks without error for larger recursions  (in php4)
 */
function fizzBuzzCounter($i=1)
{
	if($i100)return false;else $s='';
	if($i%3===0)$s = 'Fizz';
	if($i%5===0)$s .= 'Buzz';
	echo ($s===''?$i:$s)."br/";
	return fizzBuzzCounter(++$i);
}
fizzBuzzCounter();

/**
 * The obvious solution, alot faster in php5, can handle larger numbers
 */
for($i=1;$i=100;$i++)
{
	if($i%3===0 || $i%5===0)
	{
		if($i%3===0) echo 'Fizz';
		if($i%5===0) echo 'Buzz';
	}
	else
	{
		echo $i;
	}
	echo "br/";
}

Different perl version. Won’t guarantee it’s correct, not having a perl runtime on this machine. It’s more correct than other solutions I’ve seen though

for($i = 1; $i = 100; $i++)
{
$out = “”;
$out .= (($i % 3) == 0) ? “fizz” : “”;
$out .= (($i % 5) == 0) ? “buzz” : “”;
$out .= ($out == “”) ? $i : “”;
print $out;
}

select case when mod(rownum,5) = 0 and mod(rownum,3) = 0 THEN 'FIZZBUZZ’
When mod(rownum,3) = 0 then 'FIZZ’
when mod(rownum,5) = 0 then 'BUZZ’
else to_char(rownum)
end num_fizz_buzz
from all_objects
where rownum = 100

Simple C answer:

for(i=1 ; i  101 ; i++){
	printf("%d%s%s\n", i, i%3 ? "" : "fizz", i%5 ? "" : "buzz");
}

It’s disgusting how many wrong solutions there are to FizzBuzz on this page…

for(int i = 0; i 100; i++){
if(i % 3 == 0)
printf(“Fizz”);
if(i % 5 == 0)
printf(“Buzz”);
else
printf("%d", i);
printf("\n");
}

"Jon Vaughan on February 27, 2007 08:43 AM"
thats pretty easy. just iterate through the string char by char , build substrings on the fly if the previous char is the same - and the end you’ll have a bunch of substrings (maybe a multi-dim array) and just compare the sizes of these.

AABBBBBCCCCDDDEEEEEEEE

$string[0]=“AA”;
$string[1]=“BBBB”;
$string[2]=“CCCC”;
$string[3]=“DDD”;
etc…