FizzBuzz Solution Dumping Ground

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…

Matthew,

In your perl version, you could have done this, to be more perlish of course… (I prefer readability/cleanliness/maintainability, hence I use python).

instead of:

$out .= (($i % 3) == 0) ? “fizz” : “”;

you could just use:

$out .= (!($i%3)) ? “fizz” : “”;

This response is only to point out the above, I’m not criticising or ‘correcting’ anything… just pointing out that in perl as with other languages doing a simple NOT operand removes unnecessary right side evaluation operators. The thing that bothers me still about perl is that in my solution, the ratio of punctuation vs. alphanumerics is greater than 2:1 $.=(!($%))?"":""; : out3fizz

For the guy who wrote the “executable line noise” Perl version: I’m glad you program in Python.

use strict;
use warnings;

for my $i (1…100) {
my $div3 = $i % 3 == 0;
my $div5 = $i % 5 == 0;
if ( $div3 and $div5 ) {
print ‘fizzbuzz’;
}
elsif ($div3) {
print ‘fizz’;
}
elsif ($div5) {
print ‘buzz’;
}
else {
print $i;
}
print “\n”;
}

It’s not clever, but I’ll be able to read it easily 6 months or a year later.

If you insist on the ternary operator, try this:

for my $i (1…100) {
my $div3 = $i % 3 == 0;
my $div5 = $i % 5 == 0;
print +($div3 and $div5) ? ‘fizzbuzz’ :
$div3 ? ‘fizz’ :
$div5 ? ‘buzz’ :
$i;
print “\n”;
}

My MS SQL Solution…(im a newbie programmer, so its probably not the most efficent solution!)

declare @i int
declare @text varchar(50)
declare @i3 int
declare @i5 int

set @i=1

while @i = 1000
begin
set @text = ''
set @i3 = @i/3
set @i5 = @i/5

	if (@i3*3=@i) and (@i5*5@i)
		begin
		set @text = 'Fizz'
		end
	if (@i3*3@i) and (@i5*5=@i)
		begin
		set @text = 'Buzz'
		end
	if (@i3*3=@i) and (@i5*5=@i)
		begin
		set @text = 'FizzBuzz'
		end
			
	print convert(varchar(5),@i) + ' - ' + @text 

	set @i = @i + 1
end

Any comments id be grateful - MattConway@GMail.com

Yeah, I can write a Sieve of Eratosthenes, but why are you only sifting out two primes? Well, you ask a silly question, you get a silly answer. We don’t even need looping or conditional statements for this one.

print 1
print 2
print "Fizz"
print 4
print "Buzz"
print "Fizz"
print 7
print 8
…etc, etc.