Competition: FizzBuzz

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Competition: FizzBuzz

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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, ' ';
}
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Post by Xoligy »

Nice approach, very creative.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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)
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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));
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

I like mordred's. According to the scoring table, his is only one point.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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!
Xoligy
Forum Commoner
Posts: 53
Joined: Sun Mar 04, 2007 5:35 am

Post by Xoligy »

As said earlier, the scoring system is just a rough guide.
User avatar
dude81
Forum Regular
Posts: 509
Joined: Mon Aug 29, 2005 6:26 am
Location: Pearls City

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post 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/)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

The original blog post seemed to get people in the mood for heated conversation... :D
Post Reply