Page 1 of 1

Competition: FizzBuzz

Posted: Wed Mar 07, 2007 4:54 am
by Xoligy
A developer's community I was part of used to have these little competitions. They were fun and encouraged people to think and learn. Basically, the idea is to make a function/snippet that perform the functionality specific in the competition in the shortest amount of code. Whitespace, function names, variable names etc. do not matter. It's done on the following (I can't remember what it was exactly, so these can be changed):

Loops = 4
Built-in Functions = 2
Conditionals = 2
Variables = 1

The lowest score wins - creativity is also awarded. The first competition is to create a FizzBuzz application.
Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.
The language is up to you, there will be an overall winner and a language specific winner. The winner gets nothing but it's fun :D

Here's my entry:

Code: Select all

array_walk(range(1, 100),'fizzbuzz');

function fizzbuzz($num){
	print (($num % 3 == 0) ? 'Fizz'. (($num % 5 == 0) ? 'Buzz' : '') : (($num % 5 == 0) ? 'Buzz' : $num)) .' ';
}
I know a loop would be easier, but I wanted to be a bit unique :P

Posted: Wed Mar 07, 2007 5:23 am
by volka

Code: Select all

$m = array(0=>'FizzBuzz');
$m[3]=$m[6]=$m[9]=$m[12]='Fizz';
$m[5]=$m[10]='Buzz';
for($i=1; $i<101; $i++) {
	echo isset($m[$i%15]) ? $m[$i%15] : $i, ' ';
}

Posted: Wed Mar 07, 2007 5:43 am
by Xoligy
Nice approach, very creative.

Posted: Wed Mar 07, 2007 7:24 am
by Mordred
I think your scoring mechanism is flawed. Source size should be somehow added to the equation, unless you want to give me the first place for this 1-point baby:

Code: Select all

echo '1 ', '2 ', 'Fizz ', '4 ', 'Buzz ', 'Fizz ', '7 ', '8 ',
'Fizz ', 'Buzz ', '11 ', 'Fizz ', '13 ', '14 ', 'FizzBuzz ',
'16 ', '17 ', 'Fizz ', '19 ', 'Buzz ', 'Fizz ', '22 ', '23 ',
'Fizz ', 'Buzz ', '26 ', 'Fizz ', '28 ', '29 ', 'FizzBuzz ',
'31 ', '32 ', 'Fizz ', '34 ', 'Buzz ', 'Fizz ', '37 ', '38 ',
'Fizz ', 'Buzz ', '41 ', 'Fizz ', '43 ', '44 ', 'FizzBuzz ',
'46 ', '47 ', 'Fizz ', '49 ', 'Buzz ', 'Fizz ', '52 ', '53 ',
'Fizz ', 'Buzz ', '56 ', 'Fizz ', '58 ', '59 ', 'FizzBuzz ',
'61 ', '62 ', 'Fizz ', '64 ', 'Buzz ', 'Fizz ', '67 ', '68 ',
'Fizz ', 'Buzz ', '71 ', 'Fizz ', '73 ', '74 ', 'FizzBuzz ',
'76 ', '77 ', 'Fizz ', '79 ', 'Buzz ', 'Fizz ', '82 ', '83 ',
'Fizz ', 'Buzz ', '86 ', 'Fizz ', '88 ', '89 ', 'FizzBuzz ',
'91 ', '92 ', 'Fizz ', '94 ', 'Buzz ', 'Fizz ', '97 ', '98 ',
'Fizz ', 'Buzz';
Or even:

Code: Select all

echo '1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14
FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28
29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz
43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56
Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz
71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz
Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98
Fizz Buzz';
Or even:
(no "echo", does it count for 0 points?)

Code: Select all

<?= '1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14
FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28
29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz
43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56
Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz
71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz
Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98
Fizz Buzz';
(lines intentionally broken for easier readability)

Posted: Wed Mar 07, 2007 7:48 am
by mikeq
Mordred wrote:I think your scoring mechanism is flawed. Source size should be somehow added to the equation, unless you want to give me the first place for this 1-point baby:

Code: Select all

echo '1 ', '2 ', 'Fizz ', '4 ', 'Buzz ', 'Fizz ', '7 ', '8 ',
'Fizz ', 'Buzz ', '11 ', 'Fizz ', '13 ', '14 ', 'FizzBuzz ',
'16 ', '17 ', 'Fizz ', '19 ', 'Buzz ', 'Fizz ', '22 ', '23 ',
'Fizz ', 'Buzz ', '26 ', 'Fizz ', '28 ', '29 ', 'FizzBuzz ',
'31 ', '32 ', 'Fizz ', '34 ', 'Buzz ', 'Fizz ', '37 ', '38 ',
'Fizz ', 'Buzz ', '41 ', 'Fizz ', '43 ', '44 ', 'FizzBuzz ',
'46 ', '47 ', 'Fizz ', '49 ', 'Buzz ', 'Fizz ', '52 ', '53 ',
'Fizz ', 'Buzz ', '56 ', 'Fizz ', '58 ', '59 ', 'FizzBuzz ',
'61 ', '62 ', 'Fizz ', '64 ', 'Buzz ', 'Fizz ', '67 ', '68 ',
'Fizz ', 'Buzz ', '71 ', 'Fizz ', '73 ', '74 ', 'FizzBuzz ',
'76 ', '77 ', 'Fizz ', '79 ', 'Buzz ', 'Fizz ', '82 ', '83 ',
'Fizz ', 'Buzz ', '86 ', 'Fizz ', '88 ', '89 ', 'FizzBuzz ',
'91 ', '92 ', 'Fizz ', '94 ', 'Buzz ', 'Fizz ', '97 ', '98 ',
'Fizz ', 'Buzz';
Or even:

Code: Select all

echo '1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14
FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28
29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz
43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56
Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz
71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz
Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98
Fizz Buzz';
Or even:
(no "echo", does it count for 0 points?)

Code: Select all

<?= '1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14
FizzBuzz 16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28
29 FizzBuzz 31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz
43 44 FizzBuzz 46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56
Fizz 58 59 FizzBuzz 61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz
71 Fizz 73 74 FizzBuzz 76 77 Fizz 79 Buzz Fizz 82 83 Fizz
Buzz 86 Fizz 88 89 FizzBuzz 91 92 Fizz 94 Buzz Fizz 97 98
Fizz Buzz';
(lines intentionally broken for easier readability)
Ha ha, I like it

Posted: Wed Mar 07, 2007 8:29 am
by onion2k

Code: Select all

array_map(create_function('$v','echo (($v%3==0&&$v%5==0)?"FizzBuzz":(($v%3==0)?"Fizz":(($v%5==0)?"Buzz":$v)))."<br>";'),range(1, 100));

Posted: Wed Mar 07, 2007 9:19 am
by Xoligy
Mordred wrote:I think your scoring mechanism is flawed. Source size should be somehow added to the equation, unless you want to give me the first place for this 1-point baby:

<snip>
haha, well the scoring isn't strict, it's just a rough guide as to how they're looked upon. The problem with source sizing is that people then remove all the whitespace and put it on one line (I could always run them through something like )CodeBeautifier and compare them. It will be human judged (not points judged) so things that try and bend the rules won't count.

So I suppose size is also counted, but not as much as creativity and efficiency.

Onion, you could have got away with $v%15==0 instead of $v%3==0&&$v%5==0.

Posted: Wed Mar 07, 2007 12:25 pm
by RobertGonzalez
I like mordred's. According to the scoring table, his is only one point.

Posted: Wed Mar 07, 2007 12:56 pm
by superdezign
I never encountered this little FizzBuzz thing until I read a digg article about how a lot of people applying for programming jobs haven't programmed anything. When I was first hired as a PHP programmer, I'd never programmed in PHP before, but I faked it from C++. :-p

But yeah, the post had tons of people trying to program it in their native programming languages, many with errors and typos. It was interesting.

Posted: Wed Mar 07, 2007 2:44 pm
by Kieran Huggins

Code: Select all

include('http://forums.devnetwork.net/viewtopic.php?p=363893#363893');
So sad.. only down to 2 points :-(

Mordred: you win!

Posted: Wed Mar 07, 2007 3:25 pm
by Xoligy
As said earlier, the scoring system is just a rough guide.

Posted: Thu Mar 08, 2007 3:55 am
by dude81
I vote for onion2k's code which looks quiet short.

Code: Select all

array_map(create_function('$v','echo (($v%3==0&&$v%5==0)?"FizzBuzz":(($v%3==0)?"Fizz":(($v%5==0)?"Buzz":$v)))."<br>";'),range(1, 100));
but , Modred logically you win.

Posted: Thu Mar 08, 2007 4:29 am
by Mordred
No, I don't "win", I just wanted to demonstrate the flaw in the scoring system ;)
I admit, my code is best in terms of readability and speed, but it's not configurable and extendable (well, not quite correct, as I actually generated the code with a script ;). If the task was to output entries from 1 to a given N, such breaking of the rules would not be as easy ;)

If you want to see a solution which I really like (my geek part at least, I know it's bad to code like this for a job), feast your eyes on this:

Code: Select all

#include <stdio.h>
static const char *t[] = {"%d", "Fizz\n", "Buzz\n", "FizzBuzz\n"};
int main()
{
  unsigned int i;
  for(i = 1; i <= 100; i++) printf(t[3&19142723>>2*i%30], i);
  return 0;
}
(http://www.rfc1149.net/blog/2007/01/26/ ... ogrammers/)

Posted: Thu Mar 08, 2007 10:55 am
by RobertGonzalez
The original blog post seemed to get people in the mood for heated conversation... :D