Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Golf Challenge: FizzBuzz

by Sidhekin (Priest)
on Mar 02, 2007 at 20:01 UTC ( #602949=note: print w/ replies, xml ) Need Help??


in reply to Re: Golf Challenge: FizzBuzz
in thread Golf Challenge: FizzBuzz

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!


Comment on Re^2: Golf Challenge: FizzBuzz
Select or Download Code
Re^3: Golf Challenge: FizzBuzz
by liverpole (Monsignor) on Mar 02, 2007 at 20:15 UTC
    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://602949]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2014-07-24 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (157 votes), past polls