Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Golf Challenge: FizzBuzz

by eric256 (Parson)
on Mar 02, 2007 at 19:49 UTC ( #602948=note: print w/ replies, xml ) Need Help??


in reply to Golf Challenge: FizzBuzz

Here is my variation..got 50! ;)

#23456789 123456789 123456789 123456789 123456789 print+((Fizz)[$_%3]).((Buzz)[$_%5])||$_ for 1..100

___________
Eric Hodges


Comment on Re: Golf Challenge: FizzBuzz
Download Code
Re^2: Golf Challenge: FizzBuzz
by Sidhekin (Priest) on Mar 02, 2007 at 20:01 UTC

    Ooh, neat! You've got a few unnecessary parentheses, though. That should be 46:

    #23456789 123456789 123456789 123456789 123456 print+(Fizz)[$_%3].(Buzz)[$_%5]||$_ for 1..100

    ... which I expect will be the winning entry. Not much cruft there.

    print "Just another Perl ${\(trickster and hacker)},"
    The Sidhekin proves Sidhe did it!

      Unfortunately, the last several entries are all missing newlines, which makes the output very hard to read.

      It seems like such output is just a little too golfed!

      Update:  Otherwise, you can still make it shorter:

      die+map{(Fizz)[$_%3].(Buzz)[$_%5]||$_}1..100

      Update 2:  As Sidhekin pointed out to me, the -l switch makes the newline problems go away.

      On an unrelated note, I thought it would be fun writing a version that doesn't use the modulo operator (%).  Originally I tried golfing it, but it didn't golf well, so here's an easy-to-read version, which makes use of divisibility tests:

      use strict; use warnings; sub divisible_by_3 { my $num = shift; while (length($num) > 1) { my $sum = 0; map { $sum += $_ } split '', $num; $num = $sum; } return $num =~ /^[0369]$/; } sub divisible_by_5 { my $num = shift; return ($num =~ /[05]$/); } sub divisible_by_15 { my $num = shift; return (divisible_by_3($num) and divisible_by_5($num)); } foreach (1 .. 100) { printf "%s\n", divisible_by_15($_)? "fizzbuzz": divisible_by_3($_)? "fizz": divisible_by_5($_)? "buzz": $_; }

      s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
        Unfortunately, the last several entries are all missing newlines, which makes the output very hard to read.

        Indeed, but there is an easy fix: $\="\n";. This does add eight characters, though.

        Solving the problem without modulo seems fun. If this were Perl 6 or Haskell, one could do it with filtered streams. How about table-driven programming?

        --
        print "Just Another Perl Adept\n";

        (Note the lack of %).

        #2345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 6 2345678 7 + 2345 for('001'x33|'00002'x20){print[pos,Fizz,Buzz,FizzBuzz]->[$1],$/while/( +.)/g} for('001'x33|'00002'x20){warn+(pos,Fizz,Buzz,FizzBuzz)[$&],$/while/./g +} map{warn+(FizzBuzz,Fizz,Buzz,pos)[$&],$/while/./g}331x34&33332x20 #2345678 1 2345678 2 2345678 3 2345678 4 2345678 5 2345678 6 2345

        Update: Dropped 3 4 strokes above. Then 6 more. Then s/x33/x34/ to fix a bug where only 99 lines were printed by the 3rd version.

        - tye        

        Update: Otherwise, you can still make it shorter:
        die+map{(Fizz)[$_%3].(Buzz)[$_%5]||$_}1..100

        Generally printing to STDERR is not considered equivalent to printing to STDOUT.

        Update 2: As Sidhekin pointed out to me, the -l switch makes the newline problems go away.

        But under common golfing rules -l does count as +3 strokes. Changing

        $_ for

        to

        $_,$/for

        adds two.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://602948]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2015-07-06 00:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls