Fizzbuzz!

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

Moderators: phlip, Prelates, Moderators General

Postby yy2bggggs » Wed Apr 04, 2007 5:32 am UTC

Code: Select all
@echo off
for /l %%x in (1,1,100)do call :a %%x
goto :eof
:a
set /a f=%1%%3
set /a b=%1%%5
set r=
if %f%==0 set r=Fizz
if %b%==0 set r=%r%Buzz
if %r%.==. (echo %1) else (echo %r%)

179 char batch file (using single char line feeds, omit the final one)
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

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

EvanED, here's the trick:

In Java, nearly everything is an Object -- which includes integer primitives at some times. While you might not think of the primitives as objects, a recent feature called autoboxing allows primitives (byte, char, int, etc.) to be cast to their corresponding objects (Byte, Character, Integer, etc.)

As such, the following code compiles validly in Java:
Code: Select all
class B {
    public static void print(Object obj){
        System.out.println(obj);
    }
    public static void main(String[] args){
        Object o = 1;
        print(1);
        print( (Object) 1); // redundant.
        print(true?o:2);
    }
}

Now, autoboxing is not universal -- that is, a primitive is not always considered equivalent to its object. An example is the code (1).reverse(); if you try it, you'll get an error that the int cannot be dereferenced.

But autoboxing does work on things like class-casts and function calls.

So, to answer your question, it's not an overloading issue; the type of (A ? i : "stuff") is simply Object, since that's the lowest class which has both of those as subclasses; and it works because 1 can be autoboxed to new Integer(1).

So, the code works because PrintStream.println(Object o) is defined. As mentioned in the API, that particular function just calls String.valueOf(o) and prints it out the same way it prints out anything else.

If you change the above code to say print(String s) instead of print(Object o), you'll see some pretty angry compiler messages.
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby EvanED » Wed Apr 04, 2007 6:37 am UTC

Ahhhhhh, gotcha. Makes sense. Thanks.

And that explains why it started working in 1.5, 'cause I knew that they added autoboxing then. ;-)
EvanED
 
Posts: 4160
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI

Postby warriorness » Wed Apr 04, 2007 7:02 am UTC

Drostie wrote:If you change the above code to say print(String s) instead of print(Object o), you'll see some pretty angry compiler messages.


I tried it, and it's not that bad. But it's absolutely hideous if you conflict generics.

Code: Select all
import java.util.*;

public class Test
{
   public static void main(String[] args)
   {
      boolean x = false;
      
      LinkedList<String> y = new LinkedList<String>();
      LinkedList<Integer> z = new LinkedList<Integer>();
      
      y.add("Hello!");
      z.add(1337);
      
      print( x?y:z );
   }
   private static void print(LinkedList<String> o)
   {
      System.out.println(o.toString());
   }
}


Try compiling it, and you get:
Code: Select all
Test.java:15: print(java.util.LinkedList<java.lang.String>) in Test cannot be applied to (java.util.LinkedList<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.AbstractSequentialList<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.AbstractList<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.List<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.AbstractCollection<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.Queue<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.util.Collection<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>&java.lang.Iterable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<? extends java.lang.Object&java.io.Serializable&java.lang.Comparable<?>>>)
                print( x?y:z );
                ^
1 error


However, simply remove the generic type in the parameter of print(...) so it is now "print(LinkedList o)", it compiles fine! (and the output is "[1337]").
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 » Wed Apr 04, 2007 9:41 am UTC

Yeah, that's nasty, but none of that is caused by the actual autoboxing. (You get the same error if you replace 1337 with new Integer(1337), where you do the boxing yourself.)

Two things are happening in your code, both of them related to this "cast-up" effect of the ternary operator (the question-mark (?) operator thing).

I earlier said that if you try to do A? B : C, then it looks for the lowest mutual superclass it can do. That's only a part of the truth -- it then *extends* this superclass, with a derived one that implements any mutual interfaces that both subclasses have, but the superclass does not.

When you try to use a ternary operator on an Integer and a String, instead of just getting an Object, you get a:
Code: Select all
? extends java.lang.Object
    &java.io.Serializable
    &java.lang.Comparable<
        ? extends java.lang.Object
            &java.io.Serializable
            &java.lang.Comparable<?>>

Now, one of the things that's killing your code is just the redundancy of the generic in the Comparable class. The object that the ternary operator creates is, as far as I can tell, only Comparable to itself -- you cannot, as far as I know, create *any* another object of the same anonymous class to compare it with. And so the above specification states that outright. It just means many more characters to deal with.

Okay, so let's replace that whole long "? extends Object" thing in the code block, above, with the letter A. If we call this anonymous class A, what's happening in your particular compiler error?

Well, first off, the LinkedList doesn't get cast to a different class. It doesn't need to be; both objects are LinkedLists. So their superclass is a LinkedList, too.

But aha, these aren't *quite* the same class. One is a subclass called LinkedList<Integer> and the other is a subclass called LinkedList<String>. And, though you don't need to send the LinkedList to a superclass, you do need to send the generics there. So we're casting to a LinkedList<A>.

And that's not quite enough, either. LinkedList<String> also implemented some of its interfaces with generics -- for example, it implements Iterable<String>. Any genericized interfaces *also* need to be respecified.

If I just rewrite your error message with my A convention, and with some whitespace tossed in, it says:
Code: Select all
print(java.util.LinkedList<java.lang.String>) in Test cannot be applied to (
java.util.LinkedList<A>
    &java.util.AbstractSequentialList<A>
    &java.util.AbstractList<A>
    &java.util.List<A>
    &java.util.AbstractCollection<A>
    &java.util.Queue<A>
    &java.util.Collection<A>
    &java.lang.Iterable<A>)

So, all of the genericized LinkedList interfaces also got respecified.

And that's how your compiler message from hell was created -- generics + ternary operators + two classes which were both Comparable and Serializable, but could not be traced to a superclass.
User avatar
Drostie
 
Posts: 262
Joined: Fri Nov 03, 2006 6:17 am UTC

Postby rulerofutumno » Sun Apr 15, 2007 6:24 pm UTC

Torn Apart By Dingos wrote: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"


You could always replace "True" with "1>0" or something along those lines and save one byte.

Haskell (105 bytes):
Code: Select all
main=putStr$unlines$map h [1..100];h a|mod a 15<1="FizzBuzz"|mod a 3<1="Fizz"|mod a 5<1="Buzz"|3>2=show a


C# (124 bytes):
Code: Select all
class P{static void Main(){for(int i=0;i++<100;)System.Console.WriteLine(i%5<1?i%3<1?"FizzBuzz":"Buzz":i%3<1?"Fizz":""+i);}}


</first post>
rulerofutumno
 
Posts: 3
Joined: Sun Apr 15, 2007 5:14 am UTC
Location: Massachusetts

Postby william » Sun Apr 15, 2007 6:48 pm UTC

Aglet wrote: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.

Perl and Python use the same "guts" in their regex libraries, IIRC. It's just that Perl has built-in syntax for it, which helps if you use regeces but just adds to the bloat of the language if you don't. (This wasn't always the case though. Once python had Emacs-style regeces which aren't as good as Perl-style regeces)
SecondTalon wrote:A pile of shit can call itself a delicious pie, but that doesn't make it true.
User avatar
william
Not a Raptor. Honest.
 
Posts: 2418
Joined: Sat Oct 14, 2006 5:02 pm UTC
Location: Chapel Hill, NC

Postby Torn Apart By Dingos » Sun Apr 15, 2007 7:50 pm UTC

rulerofutumno wrote:
Torn Apart By Dingos wrote: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"


You could always replace "True" with "1>0" or something along those lines and save one byte.

Haskell (105 bytes):
Code: Select all
main=putStr$unlines$map h [1..100];h a|mod a 15<1="FizzBuzz"|mod a 3<1="Fizz"|mod a 5<1="Buzz"|3>2=show a

Right. And using the trick that ==0 is the same as <1, I can trim my code to 104 bytes. I could improve your code:
Haskell (98 bytes):
Code: Select all
main=putStr$unlines$map h[1..100];h a|a%15<1=h 3++h 5|a%3<1="Fizz"|a%5<1="Buzz"|3>2=show a;(%)=mod


I'd like to see the 85 byte solution for Haskell.

EDIT:
An improvement! Down to 95 bytes.
Code: Select all
main=mapM_(putStrLn.h)[1..100];h a|a%15<1=h 3++h 5|a%3<1="Fizz"|a%5<1="Buzz"|3>2=show a;(%)=mod
User avatar
Torn Apart By Dingos
 
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Postby musicinmybrain » Mon Apr 16, 2007 1:15 am UTC

At least for Python, it will let you shave off a byte by submitting a file without a newline at the end. (Your text editor may add the newline automatically, as text files are really supposed to end with newlines.) I don't know if it will throw errors when you do that for other languages.

Here's my best effort so far in Python (76 bytes):
Code: Select all
for x in range(1,101):print str(x)*(x%3*x%5>0)+"Fizz"*(x%3<1)+"Buzz"*(x%5<1)

I tried weaseling the size down by using urllib to retrieve the answer from a remote location, but the server didn't like that. Can't say I'm surprised.

I'm amazed people have done it in 58 bytes.
+ + + + + + + + [ > + + + + [ > + + > + + + > + + + > + < < < < - ] > + > - > + > > + [ < ] < - ] > > . > > - - - . + + + + + + + . . + + + . > . < < - . > . + + + . - - - - - - . - - - - - - - - . > + . > + + .
User avatar
musicinmybrain
 
Posts: 96
Joined: Sun Dec 31, 2006 2:50 am UTC
Location: Greensboro, NC

Postby hotaru » Mon Apr 16, 2007 3:21 am UTC

i've managed to get it down to 50 bytes in perl, a few different ways:

Code: Select all
print"Fizz"x!($_%3).Buzz x!($_%5)||$_,$/for 1..100

Code: Select all
print"Fizz"x!($_%3).Buzz x/[05]$/||$_,$/for 1..100

Code: Select all
print+Fizz x!($_%3).Buzz x!($_%5)||$_,$/for 1..100

Code: Select all
print+Fizz x!($_%3).Buzz x/[05]$/||$_,$/for 1..100
User avatar
hotaru
 
Posts: 955
Joined: Fri Apr 13, 2007 6:54 pm UTC

Postby rulerofutumno » Mon Apr 16, 2007 3:23 am UTC

Torn Apart By Dingos wrote:I could improve your code:
Haskell (98 bytes):
Code: Select all
main=putStr$unlines$map h[1..100];h a|a%15<1=h 3++h 5|a%3<1="Fizz"|a%5<1="Buzz"|3>2=show a;(%)=mod


I'd like to see the 85 byte solution for Haskell.

EDIT:
An improvement! Down to 95 bytes.
Code: Select all
main=mapM_(putStrLn.h)[1..100];h a|a%15<1=h 3++h 5|a%3<1="Fizz"|a%5<1="Buzz"|3>2=show a;(%)=mod


Not bad, not bad... some things there that I don't know about yet - I only started Haskelling a few days ago.

Seeing that 85-byte program would be pretty awesome, but more than that I want to see the winning C# entry - it's one byte smaller than mine and I have no idea where that byte could come from.
rulerofutumno
 
Posts: 3
Joined: Sun Apr 15, 2007 5:14 am UTC
Location: Massachusetts

Postby Torn Apart By Dingos » Mon Apr 16, 2007 2:34 pm UTC

Better still in Haskell (91 bytes):
Code: Select all
main=mapM_(\a->putStrLn$[show a,"Buzz","Jizz","JizzBuzz"]!!(0^mod a 5+2*0^mod a 3))[1..100]


Here's another 95 byte solution:
Code: Select all
main=putStr$[1..100]>>=(++"\n").(zipWith(++)(cycle["Jizz","",""])(cycle["Buzz","","","",""])!!)


It's really frustrating that I can't reach 85 bytes.
User avatar
Torn Apart By Dingos
 
Posts: 817
Joined: Thu Aug 03, 2006 2:27 am UTC

Postby rulerofutumno » Mon Apr 16, 2007 5:36 pm UTC

Torn Apart By Dingos wrote:It's really frustrating that I can't reach 85 bytes.


Getting closer every day though.. :)
rulerofutumno
 
Posts: 3
Joined: Sun Apr 15, 2007 5:14 am UTC
Location: Massachusetts

Postby zybler » Fri Sep 21, 2007 5:29 pm UTC

I got 81 characters in C. The previous poster who got 79 characters had some error. :D
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+(i%3?4:0),i),i++<100);}
Last edited by zybler on Tue Sep 25, 2007 10:53 am UTC, edited 1 time in total.
zybler
 
Posts: 9
Joined: Fri Sep 21, 2007 4:57 pm UTC

Postby hotaru » Fri Sep 21, 2007 7:35 pm UTC

121 bytes in factor:
Code: Select all
: d dupd mod 0 = ; 100 [ 1+ 15 d [ drop "FizzBuzz" ] [ 3 d [ drop "Fizz" ] [ 5 d [ drop "Buzz" ] when ] if ] if . ] each
User avatar
hotaru
 
Posts: 955
Joined: Fri Apr 13, 2007 6:54 pm UTC

Postby Earlz » Sun Sep 23, 2007 2:49 am UTC

there should really be a show-off type forum where you are limited to code of like 200 bytes or so and just so people can make random stuff...maybe make it a competition...

edit:
what about in assembly? because it is basically not abstract, could it be that the actual compiled code count for the size?
My new blag(WIP, so yes it's still ugly..)
DEFIANCE!
Image
This is microtext. Zooming in digitally makes it worse. Get a magnifying glass.. works only on LCD
User avatar
Earlz
Gets Obvious Implications
 
Posts: 786
Joined: Sat Jun 09, 2007 8:38 am UTC
Location: USA

Postby torne » Mon Sep 24, 2007 12:10 pm UTC

Earlz wrote:what about in assembly? because it is basically not abstract, could it be that the actual compiled code count for the size?

I'd do that now if I wasn't at work :) x86 DOS COM executables can be written entirely using printable ASCII characters, too, for maximum amusement (thus meaning you could still trivially paste it as a forum quote).
User avatar
torne
 
Posts: 98
Joined: Wed Nov 01, 2006 11:58 am UTC
Location: London, UK

Postby mrkite » Mon Sep 24, 2007 10:30 pm UTC

Earlz wrote:what about in assembly? because it is basically not abstract, could it be that the actual compiled code count for the size?


Great, now I feel old.

Reading that reminded me that I had once participated in a 256 byte fire competition. I did a search to see if I could find it.

It turns out the competition was a #coders competition in 1996.
mrkite
 
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Postby yy2bggggs » Mon Sep 24, 2007 11:30 pm UTC

zybler wrote:I got 81 characters. The previous poster who got 79 characters had some error. :D
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+(i%3?4:0),i),i++<100);}

Which was?
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby zybler » Tue Sep 25, 2007 10:43 am UTC

yy2bggggs wrote:
zybler wrote:I got 81 characters. The previous poster who got 79 characters had some error. :D
Code: Select all
main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+(i%3?4:0),i),i++<100);}

Which was?

Which was:

main(i){while(printf(i%5?i%3?"%d\n":"Fizz\n":"FizzBuzz\n"+i%3*4%8,i),i++<100);}
The bolded was not mathematically equivalent to (i%3?4:0). If you go and submit it to http://golf.shinh.org/p.rb?FizzBuzz. It would failed.
zybler
 
Posts: 9
Joined: Fri Sep 21, 2007 4:57 pm UTC

Postby yy2bggggs » Tue Sep 25, 2007 11:30 am UTC

zybler wrote:The bolded was not mathematically equivalent to (i%3?4:0).

Good catch.
If you go and submit it to

...but why would you do that?
User avatar
yy2bggggs
 
Posts: 1261
Joined: Tue Oct 17, 2006 6:42 am UTC

Postby Victorkm » Tue Sep 25, 2007 12:34 pm UTC

I only know Visual basic for Excel but without the workbook attached its only 489 bytes. With the workbook its 22k :(

Code: Select all
Sub count()
For butt = 1 To 100
Worksheets("Sheet1").Cells(butt, 1).Value = butt
check = 0
check2 = 0
For butt2 = 1 To 33
If butt / butt2 = 5 Then
Worksheets("Sheet1").Cells(butt, 1).Value = "Buzz"
check = 1
End If
If butt / butt2 = 3 Then
    Worksheets("Sheet1").Cells(butt, 1).Value = "Fizz"
    check1 = 1
    If check1 + check = 2 Then
        Worksheets("Sheet1").Cells(butt, 1).Value = "FizzBuzz"
        Exit For
    End If
End If
Next butt2
Next butt
End Sub
User avatar
Victorkm
 
Posts: 317
Joined: Tue Jul 17, 2007 1:51 pm UTC
Location: Florida

Postby hotaru » Wed Sep 26, 2007 3:26 pm UTC

74 bytes in false.
Code: Select all
1[$101\>][$$[$2>][3-]#\[$4>][5-]#$@$@*[@$.@@]?~["Fizz"]?~["Buzz"]?"
"1+]#
User avatar
hotaru
 
Posts: 955
Joined: Fri Apr 13, 2007 6:54 pm UTC

Postby Defenestrator » Thu Sep 27, 2007 11:35 am UTC

56 bytes in Python:
Code: Select all
i=1;exec"print'FizzBuzz'[i%-3&4:12&8-i%5]or i;i+=1;"*100
Multiplying a string by an integer will return that string repeated that many times, so "foo" * 3 == "foofoofoo". Using this together with exec is shorter than any other type of loop in this case. The loop body prints a cleverly calculated substring (or the value of i, if the substring is empty).

For the first substring index, i%-3 returns 0, -2 or -1. Bitwise and-ing with 4 returns 0, 4 or 4 respectively.
For the second substring index, i%5 returns 0, 1, 2, 3 or 4, then 8-i%5 returns 8, 7, 6, 5 or 4, and 12&8-i%5 returns 8, 4, 4, 4 or 4 respectively.
Defenestrator
 
Posts: 8
Joined: Thu Sep 13, 2007 8:58 am UTC

Re: Fizzbuzz!

Postby Hench » Mon Oct 01, 2007 4:25 pm UTC

I've got 15th place in Java at 134 bytes for
Code: Select all
class a {
    public static void main(String[]b) {
        for(int i=0; ++i<101; System.out.println(i%3<1 ? i%5<1?"FizzBuzz":"Fizz" : i%5<1?"Buzz":""+i));
    }
}

without the un-required whitespaces and line returns, of course.
Spoiler:
Your perceptions will not change reality, but simply color it.
User avatar
Hench
Porn, hence, Man
 
Posts: 498
Joined: Wed Mar 28, 2007 4:16 pm UTC
Location: Right between my eyes

Re: Fizzbuzz!

Postby Nexuapex » Tue Oct 02, 2007 1:50 am UTC

As small as I can figure out how to get it in Lua:
Code: Select all
for i=1,100 do s=(i%3<1 and"Fizz"or"")..(i%5<1 and"Buzz"or"")print(#s<1 and i or s)end
“It’s my estimation that every man ever got a statue made of him was one kind of a son of a bitch or another.”
User avatar
Nexuapex
 
Posts: 22
Joined: Thu Sep 06, 2007 3:01 am UTC
Location: Northwest Iowa

Re:

Postby adlaiff6 » Tue Oct 02, 2007 4:59 am UTC

Defenestrator wrote:56 bytes in Python:
Code: Select all
i=1;exec"print'FizzBuzz'[i%-3&4:12&8-i%5]or i;i+=1;"*100

Very congratulations!
3.14159265... wrote:What about quantization? we DO live in a integer world?

crp wrote:oh, i thought you meant the entire funtion was f(n) = (-1)^n
i's like girls u crazy
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?

Re:

Postby Porges » Wed Oct 10, 2007 6:17 am UTC

Drostie wrote:the type of (A ? i : "stuff") is simply Object, since that's the lowest class which has both of those as subclasses


If you want to get technical, it's the join, or least upper bound. Since Java doesn't have a particularly rich type system this is always Object :p
Porges
 
Posts: 46
Joined: Mon Aug 06, 2007 3:01 am UTC
Location: Wellington, New Zealand

Re: Fizzbuzz!

Postby EndangeredMassa » Thu Oct 11, 2007 6:40 pm UTC

How about Javascript?
Code: Select all
for(i=1;i<101;i++)document.writeln(i%3*i%5?i:i%5?"Fizz":!i%3?"FizzBuzz":"Buzz")

That's a mere 80 characters.
User avatar
EndangeredMassa
 
Posts: 46
Joined: Wed Apr 25, 2007 7:16 pm UTC

Re: Fizzbuzz!

Postby else » Sun Oct 14, 2007 6:19 am UTC

My solution. 80 characters in C.

Code: Select all
main(i){char*a="FizzBuzz";while(i++<=99)printf(i%3?i%5?"":&a[4]:i%5?"Fizz":a);}


Edit:

Actually that char array does nothing for me. 72.
Code: Select all
main(i){while(i++<=99)printf(i%3?i%5?"":"Buzz":i%5?"Fizz":"FizzBuzz");}
User avatar
else
 
Posts: 8
Joined: Sun Oct 07, 2007 5:28 am UTC

Re: Fizzbuzz!

Postby zybler » Sun Oct 14, 2007 7:21 am UTC

else wrote:My solution. 80 characters in C.

Code: Select all
main(i){char*a="FizzBuzz";while(i++<=99)printf(i%3?i%5?"":&a[4]:i%5?"Fizz":a);}


Edit:

Actually that char array does nothing for me. 72.
Code: Select all
main(i){while(i++<=99)printf(i%3?i%5?"":"Buzz":i%5?"Fizz":"FizzBuzz");}

I don't think your solution is correct. You're supposed to print the number out as well if the number is not a factor of 3 or 5 or both. Not to mention the questions requires a newline after each number.
zybler
 
Posts: 9
Joined: Fri Sep 21, 2007 4:57 pm UTC

Re:

Postby Random832 » Fri Oct 19, 2007 6:35 pm UTC

Aglet wrote: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.


I'm pretty sure the use of the 'q' and 'qq' operators count as literals. What i'm not sure is whether the space can act as a delimiter, or if the next letter after the space is what does it.
Random832
 
Posts: 2525
Joined: Wed Oct 10, 2007 4:38 pm UTC

Re: Fizzbuzz!

Postby aldimond » Sun Oct 21, 2007 9:19 am UTC

I don't know how some dude got down to 43 in bash... I'm hovering around 101 with

Code: Select all
a()(o=
((i%3))||o+=Fizz
((i%5))||o+=Buzz
(((i%3)*(i%5)))&&o+=$i
echo $o
((i++))
((i-101))&&a)
i=1
a


I'm definitely not an expert on crazy bash syntax... so if there are cheaper ways to do math and conditionals I may be missing out.

EDIT: Down to 94 by consolidating i++ with i-101. Obviously there is something fundamentally better to be done. Also for some reason early on I thought that the recursive call was smaller than a normal "for i in {1..100}" loop, but it winds up being exactly the same.
One of these days my desk is going to collapse in the middle and all its weight will come down on my knee and tear my new fake ACL. It could be tomorrow. This is my concern.
User avatar
aldimond
Otter-duck
 
Posts: 2665
Joined: Fri Nov 03, 2006 8:52 am UTC
Location: Uptown, Chicago

Re: Fizzbuzz!

Postby mrkite » Sun Oct 21, 2007 10:56 am UTC

I currently hold the record for TCL.. at 85 bytes.

Code: Select all
set i 0;while {[incr i]<101} {puts [expr $i%3?$i%5?$i:"Buzz":$i%5?"Fizz":"FizzBuzz"]}
mrkite
 
Posts: 336
Joined: Tue Sep 04, 2007 8:48 pm UTC

Re: Fizzbuzz!

Postby aldimond » Sun Oct 21, 2007 4:01 pm UTC

Woo, have come up with a much better way in bash, currently at 51 bytes. I'll spoilerize this, 'cause it doesn't suck as much as my last attempt.

Spoiler:
Code: Select all
seq 100|sed -e15~15cFizzBuzz -e3~3cFizz -e5~5cBuzz


Yes, this is not so much bash as sed. Sue me. Maybe I'll do one fully in sed.


Edit: It's at 46 like so:

Spoiler:
Code: Select all
seq 100|sed '0~15cFizzBuzz
0~3cFizz
0~5cBuzz'

Note that sed is really strange... after you "c" a line it's no longer available to future "c" commands.
One of these days my desk is going to collapse in the middle and all its weight will come down on my knee and tear my new fake ACL. It could be tomorrow. This is my concern.
User avatar
aldimond
Otter-duck
 
Posts: 2665
Joined: Fri Nov 03, 2006 8:52 am UTC
Location: Uptown, Chicago

Re: Re:

Postby Yakk » Mon Oct 22, 2007 10:48 pm UTC

Random832 wrote:I'm pretty sure the use of the 'q' and 'qq' operators count as literals. What i'm not sure is whether the space can act as a delimiter, or if the next letter after the space is what does it.


It depends on the version of perl. Whitespace being used as a delimiter was nerfed in perl5, I think.
One of the painful things about our time is that those who feel certainty are stupid, and those with any imagination and understanding are filled with doubt and indecision - BR

Last edited by JHVH on Fri Oct 23, 4004 BCE 6:17 pm, edited 6 times in total.
User avatar
Yakk
Poster with most posts but no title.
 
Posts: 10493
Joined: Sat Jan 27, 2007 7:27 pm UTC
Location: E pur si muove

Re: Fizzbuzz!

Postby aldimond » Tue Oct 23, 2007 4:03 am UTC

I decided to get addicted to code golf for a while, and I did the little-endian calculator one in C. And I'm currently the low man for C, with these 184 characters of noise:

Spoiler:
Code: Select all
main(i){char*f,a[99],b,*c;while(gets(a)){b=*(strchr(a,32)+4)-'d';i=strtoul(a,&f,b?10:16);for(f-=c=a;f;i>>=8,f=b?i:f-2,c+=2)sprintf(c,"%02hhx\n",i);printf(b?a:"%u\n",strtol(a,0,16));}}


For correctness, the last strtol should be a strtoul. None of the test cases on the site excercise it (they will break it if the first strtoul is a strtol), so I guess I'm cheating :evil: .
One of these days my desk is going to collapse in the middle and all its weight will come down on my knee and tear my new fake ACL. It could be tomorrow. This is my concern.
User avatar
aldimond
Otter-duck
 
Posts: 2665
Joined: Fri Nov 03, 2006 8:52 am UTC
Location: Uptown, Chicago

Re: Fizzbuzz!

Postby julesh » Tue Oct 23, 2007 6:44 pm UTC

EndangeredMassa wrote:How about Javascript?
Code: Select all
for(i=1;i<101;i++)document.writeln(i%3*i%5?i:i%5?"Fizz":!i%3?"FizzBuzz":"Buzz")

That's a mere 80 characters.


Three incremental improvements:

Code: Select all
for(i=0;++i<101;)document.write(i%3*i%5?i:i%5?"Fizz":!i%3?"FizzBuzz":"Buzz")

Just trimming unnecessary characters from your version, down to 76, excluding newline.

Code: Select all
s="";for(i=0;++i<101;)s+=i%15?i%5?i%3?i:"Fizz":"Buzz":"FizzBuzz";alert(s)

Rewriting the conditional expression. Plus, just for a laugh, nobody specified where it has to be output to, so put it in a dialog box instead.

Code: Select all
for(i=0;++i<101;)document.write(i%15?i%5?i%3?i:"Fizz":"Buzz":"FizzBuzz")

The best I can do. 72 chars, excluding newline.
julesh
 
Posts: 22
Joined: Wed Jul 04, 2007 9:20 am UTC

Re: Fizzbuzz!

Postby Notch » Thu Oct 25, 2007 2:44 pm UTC

It's a shame the java upload won't run static initializers

Code: Select all
class a{public static void main(String[]a){}}
// Is much larger than
class a{static{System.exit(0);}}


On my regular jvm, that loads the class, runs the static initializer, then exits the jvm before looking for the main method, and I think it's even JVM specification compliant.
Notch
 
Posts: 318
Joined: Tue Dec 12, 2006 5:52 pm UTC
Location: Stockholm, Sweden

Re: Fizzbuzz!

Postby Robin S » Thu Oct 25, 2007 2:54 pm UTC

Earlz wrote:there should really be a show-off type forum where you are limited to code of like 200 bytes or so and just so people can make random stuff...maybe make it a competition...
Late reply, but you may be interested in .kkrieger. That article also mentions

a fly-through of the first level of Descent in a mere 4,095 bytes.
That can be found here.
This is a placeholder until I think of something more creative to put here.
Robin S
 
Posts: 3579
Joined: Wed Jun 27, 2007 7:02 pm UTC
Location: London, UK

PreviousNext

Return to Coding

Who is online

Users browsing this forum: No registered users and 5 guests