Fizzbuzz!

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Prelates, Moderators General

Fizzbuzz!

Postby Zach » Sun Apr 01, 2007 3:19 am UTC

It's pretty trivial, but fun all the same.

http://golf.shinh.org/p.rb?FizzBuzz

Problem:

Print integers 1 to 100, but replace multiples of 3 with "Fizz" and multiples of 5 with "Buzz" and multiples of both with "FizzBuzz"

Challenge:

Do it in the least amount of bytes possible.

My PHP Entry(83 bytes): wrote:<?for($i=1;$i<101;$i++)echo(($i%3<1?"Fizz":'').($i%5<1?"Buzz": (($i%3?$i:''))."\n")?>


A friend's Ruby Entry(65 bytes): wrote:(1..100).map{|n|puts"FizzBuzz#{n}"[n%3<1?0:n%5<1?4:8,n%15<1?8:4]}


Brainfuck! (9951 Bytes) wrote:http://zorachus.net/brainfuck.bf


I didn't use looping in Brainfuck, just did it line-by-line, but by God, I did it!

This should be interesting.

Edit: If you go to the main site of http://zorachus.net, you'll see random XKCD comics, and then, of course, a link to this site. I'm hotlinking, but my site has less than 20 people that have even seen it, so I'm assuming it's not a problem. If it is, of course, I'll stop hotlinking immediately.
Last edited by Zach on Mon Apr 02, 2007 5:15 pm UTC, edited 3 times in total.
Zach
 
Posts: 81
Joined: Thu Feb 15, 2007 1:18 am UTC
Location: Lexington, Kentucky.

Postby adlaiff6 » Sun Apr 01, 2007 7:59 am UTC

Sounds like the Buzz competitions in NYSML.
User avatar
adlaiff6
 
Posts: 274
Joined: Fri Nov 10, 2006 6:08 am UTC
Location: Wouldn't you rather know how fast I'm going?

Postby Aglet » Sun Apr 01, 2007 8:02 am UTC

I just spent half an hour trying to figure out why my Python program wasn't working for this competition.

Turns out that in the shebang line at the beginning, I forgot the exclamation point.
Image
User avatar
Aglet
 
Posts: 364
Joined: Tue Mar 13, 2007 12:26 am UTC

Postby Torn Apart By Dingos » Sun Apr 01, 2007 1:12 pm UTC

This was my first try in Haskell. If you remove unnecessary whitespace, it's 120 bytes.
Code: Select all
main = putStr . unlines $ zipWith3 f [1..100] (cycle [0,0,4]) (cycle [0,0,0,0,4])
f x 0 0 = show x
f _ y z = take y "Jizz" ++ take z "Buzz"


After a lot of struggling, I managed another solution of 106 bytes. Now it's less readable than Perl.
Code: Select all
b s d n|mod n d==0=s|True="";m""x=show x;m y _=y;main=putStr$do n<-[1..100];m(b"Jizz"3n++b"Buzz"5n)n++"\n"


edit: An improvement of 8 bytes.
Last edited by Torn Apart By Dingos on Sun Apr 01, 2007 5:45 pm UTC, edited 2 times in total.
User avatar
Torn Apart By Dingos
 
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Postby SirNotApearingOnThisForum » Sun Apr 01, 2007 4:25 pm UTC

Chances are there's an even briefer way to do this, but this seems to be as small as I can get it in C (101 chars):
Code: Select all
main(i){while(i<101)!printf("%s%s","\0Fizz"+!(i%3),"\0Buzz"+!(i%5))&&printf("%d",i),putchar(10),i++;}
SirNotApearingOnThisForum
 
Posts: 29
Joined: Sun Apr 01, 2007 4:01 pm UTC

Postby EvanED » Sun Apr 01, 2007 5:36 pm UTC

SirNotApearingOnThisForum wrote:Chances are there's an even briefer way to do this, but this seems to be as small as I can get it in C (101 chars):
Code: Select all
main(i){while(i<101)!printf("%s%s","\0Fizz"+!(i%3),"\0Buzz"+!(i%5))&&printf("%d",i),putchar(10),i++;}


I must say I'm impressed. There are some pretty nifty space-saving tricks in there.
EvanED
 
Posts: 4031
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby Yakk » Sun Apr 01, 2007 5:38 pm UTC

Code: Select all
main(i){char*a[]={"%d\n","Fizz\n","Buzz\n","FizzBuzz\n"};while(i<101)printf(a+!(i%3)+2*!(i%5),i++);}

100 characters! (technically 101, because the standard requires a trailing newline)

If I can get the user to preload argv, I've got a ridiculously short one.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby SirNotApearingOnThisForum » Sun Apr 01, 2007 6:01 pm UTC

woot, 95 chars:
Code: Select all
main(i){while(i<101)printf("Fizz\n\0Buzz\n\0FizzBuzz\n\0%d\n"+(i%15?i%3?i%5?22:6:0:12),i),i++;}


btw Yakk your code dosn't really work. you'd have to dereference the address a+!(i%3)+2*!(i%5) (eg. a[!(i%3)+2*!(i%5)]), because printf's first parameter is a char * while you're passing it a char **. and post-incrementing i as a parameter seems to screw up your fizz-buzz calculations (I think its because of the parameter stack; i is added to the stack, incremented, then used in the fizzbuzz expression in the 'next' parameter).

edit2: make that 89 chars:
Code: Select all
main(i){while(i<101)printf("%d  \n\0Fizz\n\0Buzz\n\0FizzBuzz\n"+6*!(i%3)+12*!(i++%5),i);}
SirNotApearingOnThisForum
 
Posts: 29
Joined: Sun Apr 01, 2007 4:01 pm UTC

Postby Yakk » Sun Apr 01, 2007 9:16 pm UTC

SirNotApearingOnThisForum wrote:woot, 95 chars:
Code: Select all
main(i){while(i<101)printf("Fizz\n\0Buzz\n\0FizzBuzz\n\0%d\n"+(i%15?i%3?i%5?22:6:0:12),i),i++;}


btw Yakk your code dosn't really work. you'd have to dereference the address a+!(i%3)+2*!(i%5) (eg. a[!(i%3)+2*!(i%5)]), because printf's first parameter is a char * while you're passing it a char **. and post-incrementing i as a parameter seems to screw up your fizz-buzz calculations (I think its because of the parameter stack; i is added to the stack, incremented, then used in the fizzbuzz expression in the 'next' parameter).


Ya, order of evalutation for arguements of a function is not defined. Forgot about that.

edit2: make that 89 chars:
Code: Select all
main(i){while(i<101)printf("%d  \n\0Fizz\n\0Buzz\n\0FizzBuzz\n"+6*!(i%3)+12*!(i++%5),i);}


Corrected:
Code: Select all
main(i){while(i<101)printf("%d\n\0$$Fizz\n\0Buzz\n\0FizzBuzz\n"+6*!(i%3)+12*!(i%5),i);i++}


Now we don't print out extranious spaces. Plus we cannot increment i in the printf statement, the order of evaluation of the arguements is not defined (there are no sequence points between arguements).
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

My Entry...

Postby chrismurf » Mon Apr 02, 2007 1:08 am UTC

I'm late to the game, but here's my entry in python (actually, I did it a while ago, but just stumbled on this thread ).

print ", ".join([str((i, "Fizz", "Buzz", "FizzBuzz")[int((i % 3)==0) + int(2*((i % 5)==0))]) for i in xrange(1,101)])

I feel it's a pretty "creative" solution :-)
chrismurf
 
Posts: 2
Joined: Wed Nov 01, 2006 7:14 am UTC
Location: Cambridge, MA

Postby warriorness » Mon Apr 02, 2007 1:43 am UTC

All right, I did mine in Java because that's the only language I know well enough. Damned JVM wouldn't let me have a main method with a non-String[] parameter. Also I made p(String) so I didn't have to waste bytes typing out "System.out.println(...);" for each print call. It probably saved about five bytes.

Expanded, my code looks like this:
Code: Select all
class A
{
   public static void main(String[]a)
   {
      for(int i=1;i<101;i++)
      {
         String f="Fizz";
         String b="Buzz";
         if(i%3==0)
         {
            p(f)
            if(i%5==0) p(b);
         }
         else if(i%5==0) p(b);
         else p(i+"");
         p("\n");
      }
   }
   static void p(String a)
   {
      System.out.print(a);
   }
}

Collapsed, it becomes 219 characters.
Code: Select all
class A{public static void main(String[]a){for(int i=1;i<101;i++){String f="Fizz";String b="Buzz";if(i%3==0){p(f);if(i%5==0)p(b);}else if(i%5==0)p(b);else p(i+"");p("\n");}}static void p(String a){System.out.print(a);}}


EDIT: According to the website there have to be newlines. Without them it's 211 characters.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby SpitValve » Mon Apr 02, 2007 1:53 am UTC

Could you make that smaller using System.out.println(something with lots of ? : in it); ?
User avatar
SpitValve
Not a mod.
 
Posts: 5105
Joined: Tue Sep 26, 2006 9:51 am UTC
Location: Québec, Québec.

Postby EvanED » Mon Apr 02, 2007 2:05 am UTC

Without thinking about it, you could definitely lose 10 characters by passing "Fizz" as a literal strings to p() on line 11. Doing the same with Buzz on 12 and 14 is also profitable, but only by 5 characters.

Are you allowed to drop the argument to main? Or what about something like !(i%3) instead of i%3==0?

/Hasn't programmed Java for a while

EDIT: Wait, I'm an idiot. !(i%3) is the same length as i%3==0.
EvanED
 
Posts: 4031
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby bitwiseshiftleft » Mon Apr 02, 2007 2:46 am UTC

Oh yeah? Well how about this one (written in sh)?

wget -qO- tinyurl.com/2ofw5m

28 characters!
User avatar
bitwiseshiftleft
 
Posts: 293
Joined: Tue Jan 09, 2007 9:07 am UTC
Location: Stanford

Postby warriorness » Mon Apr 02, 2007 2:50 am UTC

SpitValve wrote:Could you make that smaller using System.out.println(something with lots of ? : in it); ?


Yep, you're right there. Unfortunately you can't use (i%3) as x in the x?y:z construction - javac demands an actual boolean, so I have to use == or !=. I cut it down to 141 bytes with this program.

Expanded:
Code: Select all
class B
{
   public static void main(String[] a)
   {
      for(int i = 1; i < 101; i++)
      {
         System.out.println(
            i%3*i%5 != 0 //i is none
            ?
               i
            : //i is 3, 5, or 15
               i%5 != 0 //i is 3
               ?
                  "Fizz"
               ://i is either 5 or 15
                  
                  i%3==0?"FizzBuzz":"Buzz"
         );
      }
   }
}


Compressed:
Code: Select all
class B{public static void main(String[]a){for(int i=1;i<101;i++){System.out.println(i%3*i%5!=0?i:i%5!=0?"Fizz":i%3==0?"FizzBuzz":"Buzz");}}}


And that puts me in 12th place for the Java section.

EDIT: No! They use the Java 1.4 compiler/library, which doesn't have a bunch of overloaded System.out.println(...) methods. So I can't use just i on line 10, I have to use i+"" which is an additional 3 bytes, giving me 144 characters and tying for 12th place (they put me on 13).
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby SpitValve » Mon Apr 02, 2007 3:59 am UTC

Does it count if it keeps on writing numbers after 100? e.g. for(int i=1;;i++){bla} ?
User avatar
SpitValve
Not a mod.
 
Posts: 5105
Joined: Tue Sep 26, 2006 9:51 am UTC
Location: Québec, Québec.

Postby warriorness » Mon Apr 02, 2007 4:17 am UTC

SpitValve wrote:Does it count if it keeps on writing numbers after 100? e.g. for(int i=1;;i++){bla} ?


Nope. When you submit, it checks the output of your code - it has to match up exactly. (And nothing printed to stderr either)
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby Drostie » Mon Apr 02, 2007 7:43 am UTC

I've done Java in 135, for 10th place (me = Chris Drost), and I'm trying to cut it even a little bit lower.
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby yy2bggggs » Mon Apr 02, 2007 7:48 am UTC

89 characters, except that it works.
Code: Select all
main(i){while(printf("%sFizzBuzz\n\0Fizz\n\0%s%d\n"+12*!!(i%5)+6*!!(i%3),"",i),++i<101);}
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby Drostie » Mon Apr 02, 2007 8:33 am UTC

Okay, I've given up a bit on trying to do it in fewer.

Here's 135, if you kill all the whitespace:

Code: Select all
class A{
    public static void main(String[] a){
        for(int i=0; ++i<101; )
            System.out.println(
                i%15<1?
                    "FizzBuzz" :
                    i%3<1?
                        "Fizz":
                        i%5<1?
                            "Buzz":
                             ""+i
            );
    }
}

This is just a straight "if, else if, else if, else" chain. As such, it needs no parentheses.

The for-loop doesn't need any curly braces in Java -- if they're omitted, it repeats the next item over again.

I wanted to use the String concatenation +, but the smallest I got that to was:

Code: Select all
i%5*i%3>0?
    ""+i:
    (i%3<1?"Fizz":"") + (i%5<1?"Buzz":"")

Unfortunately, that's just barely larger than the earlier code, and it doesn't seem like you can do away with any of the underlying components.

I was briefly wondering if the challenge would let you use a \r to overwrite previous characters on the line; but I couldn't manage to compress the code any more even with \r's, so I gave up on that.
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby warriorness » Mon Apr 02, 2007 8:35 am UTC

Drostie wrote:I've done Java in 135, for 10th place (me = Chris Drost), and I'm trying to cut it even a little bit lower.


Care to post the source? EDIT: Apparently I've been beaten to the punch.

I personally am disappointed that the website doesn't store entrants' source code - it is only around 100 bytes per entry, after all! I'd love to see the thus-far-winning Bash entry (I love Bash but suck at it).

EDIT 2: I don't know why I didn't realize "<1" is a byte less than "==0". Damn programming-by-habit. Using that idea (I didn't feel it'd be right to steal your other ones) I'm down to 139, and done, hopefully.
Iluvatar wrote:Love: Gimme the frickin' API.
yy2bggggs, on Fischer Random chess wrote:Hmmm.... I wonder how how a hypermodern approach would work
User avatar
warriorness
Huge Fucking-Lazer
 
Posts: 1610
Joined: Thu Dec 28, 2006 10:33 am UTC
Location: CMU, Pittsburgh, PA, USA

Postby SirNotApearingOnThisForum » Mon Apr 02, 2007 3:43 pm UTC

aha, 81 chars:
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+4*!!(i%3),i),i++<100);}
SirNotApearingOnThisForum
 
Posts: 29
Joined: Sun Apr 01, 2007 4:01 pm UTC

Postby Yakk » Mon Apr 02, 2007 4:16 pm UTC

btw, doesn't that PHP entry at the top have a bug?

I could unbalanced quotes, and naively it seems to print out the number regardless of the fizbuzzianness of it.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby Zach » Mon Apr 02, 2007 5:12 pm UTC

Yakk wrote:btw, doesn't that PHP entry at the top have a bug?

I could unbalanced quotes, and naively it seems to print out the number regardless of the fizbuzzianness of it.


Yep, sorry, wrong one. Check the edit. (Up to 83 bytes :()
Zach
 
Posts: 81
Joined: Thu Feb 15, 2007 1:18 am UTC
Location: Lexington, Kentucky.

Postby Yakk » Mon Apr 02, 2007 7:49 pm UTC

In a related comment:
Why Python is Better Than Perl - Reason #256
Because this is still the cleverest hello world program in Python:
#!/usr/bin/env python
print "Hello, world!"


Code: Select all
print " ".join("l".join("".join("H","e"),"".join("l","o")),"r".join("".join("w","o"),"d".join("l","!")))


or, even more fun:
Code: Select all
print reduce(lambda a,b:a[:1].join([a[1:], b]), [x for x in "oHrlWdel ol!"])
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby Aglet » Mon Apr 02, 2007 9:51 pm UTC

I will admit, I know of two ways where Perl is better than Python:

1. Regular expressions.

2. Making hilariously indecipherable programs, which is really a bad sign for heavy-duty programming languages but makes for great fun.

One of my personal favorites is this one:

Code: Select all
 not exp log srand xor s qq qx xor
 s x x length uc ord and print chr
 ord for qw q join use sub tied qx
 xor eval xor print qq q q xor int
 eval lc q m cos and print chr ord
 for qw y abs ne open tied hex exp
 ref y m xor scalar srand print qq
 q q xor int eval lc qq y sqrt cos
 and print chr ord for qw x printf
 each return local x y or print qq
 s s and eval q s undef or oct xor
 time xor ref print chr int ord lc
 foreach qw y hex alarm chdir kill
 exec return y s gt sin sort split


No literals. At all.
Last edited by Aglet on Tue Apr 03, 2007 2:07 am UTC, edited 1 time in total.
Image
User avatar
Aglet
 
Posts: 364
Joined: Tue Mar 13, 2007 12:26 am UTC

Postby Yakk » Mon Apr 02, 2007 10:07 pm UTC

Hmm?

Code: Select all
my_re = re.compile(RE)
match = my_re.search(string)
if match is None:
  print "didn't find the regular expression"
else:
  print "we matched [%s] in [%s]"%(match.group(), string)


How is perl RE support significantly better than python's?

Perl's advantage is that it comtains a lot of useful, pity constructs. Python's advantage is that it was put together with a bit more care.

Speaking of which, a python version of FizzBuzz... Hmm...
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby Aglet » Mon Apr 02, 2007 11:11 pm UTC

Python's method-based REs are readable and nice, but the lack of a global variable ($_, in Perl) makes them harder to use. If you want to print a text file on a line-by-line basis using regexes, Perl is easier.

But that might just be because the Python regex tutorial I found was less comprehensible than the Perl one.
Image
User avatar
Aglet
 
Posts: 364
Joined: Tue Mar 13, 2007 12:26 am UTC

Postby Drostie » Tue Apr 03, 2007 12:10 am UTC

7th place in Java! 134 characters, by revising the switcher script as:
Code: Select all
i%5>0?
    i%3>0 ?
        ""+i :
        "Fizz":
    i%3>0?
        "Buzz":
        "FizzBuzz"
Still working on pruning the last few characters...
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby Yakk » Tue Apr 03, 2007 12:44 am UTC

Code: Select all
re1 = re.compile(re_string)
for x in file_by_line:
  match1 = re1.search(x)
  if not( match1 is None):
    print x


Perl1 and 2:

Code: Select all
// 1:
foreach(<file_by_line>) {
  print $_ if (m/re_string/);
}
// 2
while($line = <file_by_line>) {
  print $line if (m/re_string/);
}


Perl is pithy, but that doesn't make it's RE system better.

And speaking of pithy -- Python's % operator is a rather neat trick! Will be important in my FizzBuzz solution...

Sadly, I have python 2.3 which is missing a trinary operator.

An untested python solution:
Code: Select all
print "\n".join(["%s%s%s"%("" if x%3 else "Fizz","" if x%5 else "Buzz","%d"%x if x%15 else "") for x in range(1,101)])

118 characters

A tested one that expands to 126 characters when implemented "for real":
Code: Select all
print "\n".join(map(lambda x:fi(fi("%d"%x,x%5,"Buzz"),x%3,fi("Fizz",x%5,"FizzBuzz")),range(1,101)))


where fi is a hand-coded python 2.3 equivilent to the trinary operator.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby yy2bggggs » Tue Apr 03, 2007 8:01 am UTC

SirNotApearingOnThisForum wrote:aha, 81 chars:
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+4*!!(i%3),i),i++<100);}

Order of ops to the rescue... 79 chars:
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+i%3*4%8,i),i++<100);}
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby Yakk » Tue Apr 03, 2007 1:10 pm UTC

yy2bggggs, the results of your solution are undefined.

The compiler can evaluate the second arguement to a function before the first, and thus give you a different value for i in the first arguement than you expect.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby yy2bggggs » Tue Apr 03, 2007 2:33 pm UTC

Yakk wrote:yy2bggggs, the results of your solution are undefined.

The compiler can evaluate the second arguement to a function before the first, and thus give you a different value for i in the first arguement than you expect.

I'm not sure you're parsing this right.
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby Yakk » Tue Apr 03, 2007 3:44 pm UTC

yy2bggggs wrote:
Yakk wrote:yy2bggggs, the results of your solution are undefined.

The compiler can evaluate the second arguement to a function before the first, and thus give you a different value for i in the first arguement than you expect.

I'm not sure you're parsing this right.


Gah -- you are right. I got LISPed.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10324
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Postby Cosmologicon » Tue Apr 03, 2007 6:22 pm UTC

Darn. I thought for sure I could improve on yy2bggggs's C routine by using the return value of printf. It would have worked if 100 weren't divisible by 5!
Code: Select all
main(i){for(;4^printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+i%3*4%8,i);i++);}

Then again, the rules don't say you can't print out other stuff.... :)
User avatar
Cosmologicon
 
Posts: 1806
Joined: Sat Nov 25, 2006 9:47 am UTC
Location: Cambridge MA USA

Postby themuffinking » Wed Apr 04, 2007 2:38 am UTC

In java, you don't need to add a ""+ to the i in the printout. System.out.println() knows how to handle integers, so that makes it:
Code: Select all
class A{
   public static void main(String[] a){
      for(int i=0;++i<101;)
         System.out.println(
            i%15<1 ? "FizzBuzz" :
               i%3<1 ? "Fizz" :
                  i%5<1?"Buzz": i
         );
   }
}


Eliminate the whitespace, of course:
Code: Select all
class A{public static void main(String[] a){for(int i=0;++i<101;)System.out.println(i%15<1?"FizzBuzz":i%3<1?"Fizz":i%5<1?"Buzz":i);}}
themuffinking
 
Posts: 52
Joined: Sat Mar 10, 2007 4:42 am UTC

Postby EvanED » Wed Apr 04, 2007 2:54 am UTC

themuffinking wrote:In java, you don't need to add a ""+ to the i in the printout. System.out.println() knows how to handle integers, so that makes it:...


I was all set to say that wouldn't work, but then I tried it. Javac 1.4 gives "incompatible types for ?: neither is a subtype of the other", which is what I expected, but 1.5 and 1.6 returned no error. (I was using this.)

Assuming it works right, that's a pretty spiffy piece of syntactic sugar.
EvanED
 
Posts: 4031
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby yy2bggggs » Wed Apr 04, 2007 2:55 am UTC

Cosmologicon wrote:Then again, the rules don't say you can't print out other stuff.... :)

Seeing as how qualification requires your output to exactly match the correct one, I'm sure this is implied.
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby Drostie » Wed Apr 04, 2007 4:52 am UTC

themuffinking wrote:In java, you don't need to add a ""+ to the i in the printout. System.out.println()

Unfortunately, that's not really true. For the compiler used in this competition, the code without the ""+ won't compile. It's not really clear what the error is, but it seems to be that, in Java 1.4 (or whatever their compiler uses), the syntax of A?B:C requires both B and C to return the same type of thing.

So, ""+i it is. Sadly. More modern versions of Java have no problem with ?i:"Stuff"; but old ones do.
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby EvanED » Wed Apr 04, 2007 4:59 am UTC

Drostie wrote:
themuffinking wrote:In java, you don't need to add a ""+ to the i in the printout. System.out.println()

Unfortunately, that's not really true. For the compiler used in this competition, the code without the ""+ won't compile. It's not really clear what the error is, but it seems to be that, in Java 1.4 (or whatever their compiler uses), the syntax of A?B:C requires both B and C to return the same type of thing.


This is correct. The reason is that in just about every statically typed languge I know (in fact I think the new ?: behavior may be the *only* exception to this I know), given an expression, every subexpression must have a verifiable type. So for instance, in foo( A ? B : C), A must have a type, B must have a type, and C must have a type, but so must A?B:C. But if B is a string and C is an int, what's the type of A?B:C? Well, it depends on the run time value of A, which means it doesn't have a static type.

The question then becomes, what is Java 1.5 and 1.6 doing? It could notice that B is a string and implicitly call C.toString() (making A?B:C equivalent to A?B:C.toString() ), or it could be really fancy and actually dispatch to the properly overloaded function.

I think it's experiment time...
EvanED
 
Posts: 4031
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Next

Return to Coding

Who is online

Users browsing this forum: No registered users and 10 guests