a companion discussion area for blog.codinghorror.com

FizzBuzz Solution Dumping Ground


#101

Here’s a more elegant MSSQL version that improves on the one above by using fewer variables, fewer checks and SELECT instead of SET (SELECT is apparently a little faster).

DECLARE @i int
DECLARE @text varchar (8)

SELECT @i = 1

WHILE @i 101
BEGIN
SELECT @text = ‘’

IF (@i % 3)
    SELECT @text = 'Fizz'
IF (@i % 5)
    SELECT @text = @text + 'Buzz'
IF (@text = '')
    SELECT @text = @i

PRINT @text

SELECT @i = @i + 1

END


#102

This is so demonstrative. How many programmers does it take to read the requirements? I thought they were painfully obvious. It’s not like in real life where the user just says:

“I want something that will let me take my data out of AutoCad2000 and enter it into our custom database written in 1988 by a lame-ass who can’t program. By the way, I won’t be available for comment, and I need it done in two weeks.”

While I’m aware you could probably do something to short these if’s using bit math, this is my unoptimized solution:

// Run with cscript/E:JScript [filename]
for(i = 1; i 101; i++)
{
if(i 15)
WScript.Echo(“FizzBuzz\n”);
else if(i 3)
WScript.Echo(“Fizz\n”);
else if(i % 5)
WScript.Echo(“Buzz\n”);
else
WScript.Echo(i+"\n");
}

// compile with dmd -release -O [filename]
for(int i = 1; i 101; i++)
{
if(i 15)
printf(“FizzBuzz”);
else if(i 3)
printf(“Fizz”);
else if(i % 5)
printf(“Buzz”);
else
printf("%d",i);
printf("\n");
}

// Note that any case where you have a 2^x - 1, you can use ‘’
// instead of %, and it’ll run faster. An optimal solution would
// trim out the branching, probably with some bitmath.

// After 0x0F comes 0x10.

// Right now on my spare time I’m the project maintainer for Walnut:
// http://dsource.org/projects/walnut
// A JScript engine. It’s a mature project, and I’m implementing
// a COM client for it.

// I’m currently employed as an office assistant, because I can’t get
// a job as a programmer. You can reach me at murpsoft@hotmail.com
// I am willing to move, but prefer to telecommute.


#103
Public Function BizzFuzz(ByVal int As Integer)
    Dim i As Integer
    For i = 1 To int
        If (i Mod 3 = 0) And (i Mod 5 = 0) Then
            Console.WriteLine("FizzBuzz")
        ElseIf i Mod 3 = 0 Then
            Console.WriteLine("Fizz")
        ElseIf i Mod 5 = 0 Then
            Console.WriteLine("Buzz")
        Else
            Console.WriteLine(i)
        End If
    Next
End Function

#104

A little late; but I figured I’d throw in a CL version:

(loop for i from 1 to 100
do
(or
(some #'identity
(list
(if (zerop (mod i 3)) (progn (format t “Fizz”) t))
(if (zerop (mod i 5)) (progn (format t “Buzz”) t))))
(format t “~A” i))
(terpri))


#105

Python one-liner:
print map(lambda x: ‘FizzBuzz’ if x%15 == 0 else ‘Fizz’ if x%3 == 0 else ‘Buzz’ if x%5 == 0 else x, range(100))

This could probably be replaced with a list comprehension.


#106

Python one-liner:
print map(lambda x: ‘FizzBuzz’ if x%15 == 0 else ‘Fizz’ if x%3 == 0 else ‘Buzz’ if x%5 == 0 else x, range(1, 101))

This could probably be replaced with a list comprehension.


#107

(awk)

BEGIN {
i = 0;
while (i100)
{
i=i+1;
if (int(i/3)*3==i int(i/5)*5==i)
{
print “FizzBuzz”;
continue;
}
if (int(i/3)*3==i)
{
print “Fizz”;
continue;
}
if (int(i/5)*5==i)
{
print “Buzz”;
continue;
}
print i;
}

}


#108

bzzzt… another wrong answer Bassam! 1. You started at 0. 2. Your logic is wrong (what would 15 do?).

Okay, since I ended up posting, I’ll post the code I jotted down when I read this post. (It took me about 4 min) (Perl)

for my $num (1…100) {
print $num if ($num % 3 and $num % 5);
print “Fizz” unless $num % 3;
print “Buzz” unless $num % 5;
print “\n”;
}

I think it’s fairly readable (especially compared to the other Perl solutions :P). In a real-world scenario, though, if code maintainers were not likely to know Perl well, I’d probably avoid the trailing-ifs and “unless” because I’ve seen these things confuse people.

I like the comments about asking questions of the interviewers regarding what to optimize for (readability? execution speed? brevity?) and whether this was likely to change, etc. Also, parameterizing the input instead of hard coding would possibly be a good way to do it (unless you had to do it in as short a time as possible). These little extra things (combined with, of course, a working program) would make you stand out in an interview.


#109

I think little coding exercises like this are far more interesting that flash games and NSFW articles/pics to pass time…LETS HAVE MORE OF THEM!

~ Matt

?php
$x=1;
while ($x101) {
if ((($x%5) == 0) and (($x%3) == 0)){
print “five AND three /br”;}
else if (($x%3) == 0) {
print “threebr/”;}
else if (($x%5) == 0) {
print “fivebr /”;}
else {
print “$xbr /”;
}
$x += 1;
}
print “You truly are, the king of kings!”;
?


#110

Because we all know how high the command is for TI-83 basic. :stuck_out_tongue:

a=0
label a
a+1=a:a/3=b:int(b)=c:a/5=d:int(d)=e
if a100:then
stop:end
if b=c:then
if d=e:then
print “fizzbuzz”:goto a:end
print “fizz”:goto a:end
if d=e:then
print “buzz”:goto a:end


#111

With C:

for (int i=1; i100; i++)
{
if (!i%3) printf(“Fizz”);
if (!i%5) printf(“Buzz”);
if (i%3 i%5) printf("%d",$i);
printf("\n");
}


#112

Ok, everybody, prepare for the horror as I, who has never learned any programming language since the C64, writes fizzbuzz.

10 i = 0
20 i = i + 1
30 if i / 15 = int(i / 15)
40 print "FizzBuzz"
50 elseif i / 5 = int(i / 5)
60 print "Buzz"
70 elseif i / 3 = int(i / 3)
80 print "Fizz"
90 else
100 print i
110 endif
120 if i 101
130 goto 20
140 else
150 quit

I probably got the syntax completely wrong…


#113

Hey Jeff,

I work for Vertigo too. I’m technically a designer, though I use PHP, Python, Ruby, JavaScript, and have used Bash, Awk, Sed and Tcl for work in the past. Not sure if that defines me as a designer or programmer. Anyway, here’s how I’d do the variable swap thingie in JavaScript:

function swapVars(){
x = 10;
y = 2;
x = x ^ y;
y = x ^ y;
x = x ^ y;
alert(“x did = “+y+” and y did =”+x+", but now x = “+x+” and y = "+y);
}
window.onload=swapVars;

And here’s the FizzBuzz in JavaScript:
function fizzBuzz(){
var i = 1;
while ( i 100 ) {
alert(i%3==0i%5==0?“FizzBuzz”:i%3==0?“Fizz”:i%5==0?“Buzz”:i);
i++;
}
}
window.onload=fizzBuzz;
(By the way, because a browser window can have only one window.onload event, you’d need to try each one separately, comment out the other, unless you use a function to queu them.)


#114

Here is the way I would code it in Javascript:

for ( var i=1; i = 100; i++ )
document.write(“FizzBuzz”.slice(i%34, i%5?4:8) || i);

For readable output, add spaces or HTML to taste.


#115

Is it a matter of a solution that works or the quality of the solution? The following Perl code works (don’t laugh; I’ve met programmers who code solutions this way), but would you hire the author?

$i = 1;
$val = { 3 = ‘Fizz’,
5 = ‘Buzz’,
6 = ‘Fizz’,
9 = ‘Fizz’,
10 = ‘Buzz’,
12 = ‘Fizz’,
15 = ‘FizzBuzz’,
18 = ‘Fizz’,
20 = ‘Buzz’,
21 = ‘Fizz’,
24 = ‘Fizz’,
25 = ‘Buzz’,
27 = ‘Fizz’,
30 = ‘FizzBuzz’,
33 = ‘Fizz’,
35 = ‘Buzz’,
36 = ‘Fizz’,
39 = ‘Fizz’,
40 = ‘Buzz’,
42 = ‘Fizz’,
45 = ‘FizzBuzz’,
48 = ‘Fizz’,
50 = ‘Buzz’,
51 = ‘Fizz’,
54 = ‘Fizz’,
55 = ‘Buzz’,
57 = ‘Fizz’,
60 = ‘FizzBuzz’,
63 = ‘Fizz’,
65 = ‘Buzz’,
66 = ‘Fizz’,
69 = ‘Fizz’,
70 = ‘Buzz’,
72 = ‘Fizz’,
75 = ‘FizzBuzz’,
78 = ‘Fizz’,
80 = ‘Buzz’,
81 = ‘Fizz’,
84 = ‘Fizz’,
85 = ‘Buzz’,
87 = ‘Fizz’,
90 = ‘FizzBuzz’,
93 = ‘Fizz’,
95 = ‘Buzz’,
96 = ‘Fizz’,
99 = ‘Fizz’,
100 = ‘Buzz’};

while ( $i 101 ) {
$p = $val-{$i} ? $val-{$i} : $i;
print “$p\n”; $i++
}

As far a recursion goes, I think whether you need to use it or not depends on what problems you’re solving. I don’t think we can assume that solutions aren’t optimal just because recursion is never used. I was probably 15 years in the business before I found a need to use recursion in the real world. Of course, when I did run across the need, at least I knew what it was.


#116

Oops! Just noticed that my FizBuzz example only goes to 99
It’s late and I’m recovering from stomach flu, blahhh!

The while loop should be:
while ( i 101 ) {


#117

private void FizzBuzz()
{
for (int i = 1; i = 100; i++)
{
string Output = i.ToString();
if ((i % 3) == 0)
Output = “Fizz”;

    if ((i % 5) == 0)
        Output = "Buzz";

    if ((i % 3) == 0  (i % 5) == 0)
        Output = "FizzBuzz";

    //Convert.ToString(i % 3)
    Console.WriteLine(Output);
}

}


#118

Yikes! Forgot the blog filters out html tags. This should work:
script
function fizzBuzz(){
var i = 1;
while ( i 101 ) {
document.write(i%3==0i%5==0?“FizzBuzz”+“br”:i%3==0?“Fizz”+“br”:i%5==0?“Buzz”+“br”:i+“br”;
i++;
}
}
window.onload=fizzBuzz;
/script


#119

I couldn’t resist either…

SQL query (Oracle):

WITH A AS (SELECT * FROM DUAL UNION ALL SELECT * FROM DUAL),
B AS (SELECT A.* FROM A, A A2 UNION ALL SELECT * FROM DUAL)
SELECT CASE WHEN MOD(ROWNUM, 15) = 0 THEN 'FizzBuzz’
WHEN MOD(ROWNUM, 5) = 0 THEN 'Buzz’
WHEN MOD(ROWNUM, 3) = 0 THEN 'Fizz’
ELSE TO_CHAR(ROWNUM) END FROM A, A A2, B, B B2;


#120

In Groovy, 70 chars:

(1…100).each{println it%3it%5?it:(it%3?’’:‘Fizz’)+(it%5?’’:‘Buzz’)}

To find all ways you can swap two variables in three moves without using a temp:

def x=[4,13], y=[13,4], z=[] as Set
def c=[
[ { [it[0], it[0]it[1]] }, ‘b=ab’],
[ { [it[0], it[0]|it[1]] }, ‘b=a|b’],
[ { [it[0], it[0]^it[1]] }, ‘b=a^b’],
[ { [it[0]it[1], it[1]] }, ‘a=ab’],
[ { [it[0]|it[1], it[1]] }, ‘a=a|b’],
[ { [it[0]^it[1], it[1]] }, ‘a=a^b’],
]
c.each{f- c.each{g- c.each{h-
if( h[0](g0) == y ) z f[1] +’; ‘+ g[1] +’; '+ h[1]
}}}
assert z==[
‘a=a^b; b=a^b; a=a|b’,
‘a=a^b; b=a^b; a=a^b’,
‘b=a^b; a=a|b; b=a^b’,
‘b=a^b; a=a^b; b=a^b’,
] as Set