Any one with any good idea how to write an elegant way of fizz buzz? mind sharing?
http://golf.shinh.org/p.rb?FizzBuzz
Fizz Buzz, any languages
Fizz Buzz, any languages
|
May 17 2007, 12:18 PM, updated 17y ago
Show posts by this member only | Post
#1
|
Junior Member
91 posts Joined: May 2007 |
Any one with any good idea how to write an elegant way of fizz buzz? mind sharing?
http://golf.shinh.org/p.rb?FizzBuzz |
|
|
|
May 17 2007, 12:24 PM
Show posts by this member only | Post
#2
|
VIP
9,270 posts Joined: Jan 2003 From: Somewhere out there |
OMG! there's actually lunatics out there who went and wrote the thing in Whitespace and Brainf***??
|
|
May 17 2007, 12:54 PM
Show posts by this member only | Post
#3
|
Senior Member
603 posts Joined: Jan 2003 |
|
|
May 17 2007, 02:17 PM
Show posts by this member only | Post
#4
|
VIP
6,008 posts Joined: Jan 2003 |
Oh man, this thing took a good part of my afternoon away T_T I made it to 152 chars in C, using two table lookups (one for the words, and another to select which word to use).
|
|
May 18 2007, 09:30 AM
Show posts by this member only | Post
#5
|
Senior Member
749 posts Joined: Jan 2007 |
FizzBuzz in javascript
CODE <html> <body> <script> for (var i=1;i<=100;i++) { document.write( ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>' ) } </script> </body> </html> |
|
May 18 2007, 09:55 AM
Show posts by this member only | Post
#6
|
Junior Member
33 posts Joined: Jul 2006 From: Petaling Jaya, Malaysia |
Well, since you specifically said interesting here's mine:
CODE <?foreach(range(1,100)as$i)echo$i%15?$i%5?$i%3?$i:"Fizz":"Buzz":"FizzBuzz","\n";?> I actually posted about it here (and included a couple of solutions my colleagues came up with) This post has been edited by seraphiel: May 18 2007, 10:54 AM |
|
May 18 2007, 08:23 PM
Show posts by this member only | Post
#7
|
Junior Member
404 posts Joined: Jan 2003 |
QUOTE(seraphiel @ May 18 2007, 09:55 AM) Well, since you specifically said interesting here's mine: Nice try, here's 67 chars one :CODE <?foreach(range(1,100)as$i)echo$i%15?$i%5?$i%3?$i:"Fizz":"Buzz":"FizzBuzz","\n";?> I actually posted about it here (and included a couple of solutions my colleagues came up with) CODE <?while($i++<100)echo$i%3?$i%5?$i:'':'Fizz',$i%5?'':'Buzz',"<br>"?> This post has been edited by zybler: May 18 2007, 08:26 PM |
|
May 18 2007, 09:33 PM
Show posts by this member only | Post
#8
|
Junior Member
33 posts Joined: Jul 2006 From: Petaling Jaya, Malaysia |
I've been coding with error_reporting(E_ALL) too long to even attempt that, zybler
I would say that your solution is definitely not what I'd accept as the shortest simply because it throws a warning |
|
|
|
May 18 2007, 09:51 PM
Show posts by this member only | Post
#9
|
Junior Member
404 posts Joined: Jan 2003 |
QUOTE(seraphiel @ May 18 2007, 09:33 PM) I've been coding with error_reporting(E_ALL) too long to even attempt that, zybler If you so insists on using it on error_reporting(E_ALL) mode, then here it is:I would say that your solution is definitely not what I'd accept as the shortest simply because it throws a warning CODE <?$i=0;while($i++<100)echo$i%3?$i%5?$i:'':'Fizz',$i%5?'':'Buzz',"\n"?> Still shorter than yours by 12 bytes, . |
|
May 18 2007, 10:50 PM
|
Junior Member
33 posts Joined: Jul 2006 From: Petaling Jaya, Malaysia |
Well done
|
|
May 18 2007, 11:08 PM
|
Junior Member
404 posts Joined: Jan 2003 |
Tried it in C. With suggestion by a close friend, managed to push it down to 109 chars.
CODE main(){char n[4];int i;for(;i++<100;){sprintf(n,"%d",i);printf("%s%s\n",i%3?i%5?n:"":"Fizz",i%5?"":"Buzz");}} Kinda feel like cheating: no #include<stdio.h> no return type for main and no return in main. The strange thing though, it runs! This post has been edited by zybler: May 18 2007, 11:12 PM |
|
May 19 2007, 01:38 AM
|
Senior Member
1,334 posts Joined: Nov 2004 |
I guess, if your main intention was to get the shortest, then it is not cheating. I have a few C collections not long time ago, exam preparation for a job interview just in case. Hehe.
CODE main(){char i=0,n[3];while(i++<100)printf("%s%s%s\n",i%3?"":"Fizz",i%5?"":"Buzz",(i%3&&i%5&&sprintf(n,"%d",i))?n:"");} CODE main(){int i=0;while(i++<100)(i%3||!printf("Fizz"))*(i%5||!printf("Buzz"))&&printf("%d",i),printf("\n");} CODE #include <stdio.h> static const char *t[] = {"%d\n", "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; } |
|
May 19 2007, 01:48 AM
|
Senior Member
749 posts Joined: Jan 2007 |
QUOTE(nyem @ May 18 2007, 09:30 AM) FizzBuzz in javascript just realised that I can execute the javascript by pasting it into the browser address bar, no need to embed in html.CODE <html> <body> � � <script> � � for (var i=1;i<=100;i++) � � { � � � � �document.write( � � � � � � � ((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>' � � � � �) � � } � � </script> </body> </html> CODE javascript:for(var i=1;i<=100;i++)document.write(((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)+'<br>');void(0); |
|
May 22 2007, 11:33 PM
|
Junior Member
91 posts Joined: May 2007 |
i forgot i left the thread over here, i thought it was erased!
Thx, really appreciate it! you all are wonderful! those codes are .. hehe but cool! browser add bar.. thx for e tips, learn one new stuff again! But i cant depict this one?? for(i = 1; i <= 100; i++) printf(t[3&19142723>>2*i%30], i); "19142723" why that number?? was it some shifting binary or hexa?? i really can't depict that... This post has been edited by bluefox: May 22 2007, 11:39 PM |
|
May 23 2007, 01:52 AM
|
Senior Member
1,334 posts Joined: Nov 2004 |
Glad u ask. Here are what everybody think about that kind of solution:
This post has been edited by sunsuron: May 23 2007, 01:55 AM |
|
May 23 2007, 02:53 AM
|
Senior Member
749 posts Joined: Jan 2007 |
QUOTE(bluefox @ May 22 2007, 11:33 PM) I'm also at a loss. The magic number also work in javascript CODE var T = [0,'Fizz','Buzz','FizzBuzz']; for (var i=1;i<=100;i++) { T[0] = i; document.write( T[ 3&19142723>>2*i%30 ] + '<br>' ); } and perl CODE perl -le "print+($_,Fizz,Buzz,FizzBuzz)[3&19142723>>2*$_%30]for 1..100" The only explanation from Samuel Tardieu QUOTE Olive: I would only give the recruiter this kind of answer if I knew that she would appreciate it (being an �gergeek for example). The trick is to represent characteristics of every number (modulo 15) by an integer between 0 and 3 (plain, fizz, buzz, fizzbuzz). You need two bits for each number between 0 and 15, and can generate a decimal number with those 30 bits with: (thanks to J) 4#.|.(-.*3|i.15)++:-.*5|i.15 19142723 (no, I don’t feel like explaining J today) Then it’s simply a matter of choosing between the four alternatives after right shifting this large number by twice i (modulo 15) and using the two rightmost bits only. |
Change to: | 0.0167sec
0.29
5 queries
GZIP Disabled
Time is now: 28th March 2024 - 09:27 PM |