Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: andye - a different tack Re: "Countdown" (golf)

by blakem (Monsignor)
on Dec 01, 2001 at 16:42 UTC ( [id://128860]=note: print w/replies, xml ) Need Help??


in reply to andye - a different tack Re: "Countdown" (golf)
in thread "Countdown" (golf)

Nice, you managed to complete all 18 holes in the same number of strokes as it took me to do the back 9 with my first entry.... ;-)

Although, I can trim a couple chars, it's no longer warnings-safe:

# 1 2 3 4 5 6 7 #234567890123456789012345678901234567890123456789012345678901234567890 +12 for(<D>){$n=$o=$_;for$c(@_){$n+=s/$c//};/^$/&&push@{$w[$n]},$o}@{$w[-1 +]}

-Blake

Replies are listed 'Best First'.
Re:**3 a different tack: "Countdown" (golf)
by jynx (Priest) on Dec 02, 2001 at 04:34 UTC

    This is untested, but the following can probably happen:

    Change the outer for loop to a map (1 character)
    Change the /^$/ to just $_ (2 characters)
    Change the @{$w[-1]} to @{pop@w} (1 character)
    Drop the semi-colon after the inner for loop (1 character)

    Which gives us:

    #234567890123456789012345678901234567890123456789012345678901234567890 +12 map{$n=$o=$_;for$c(@_){$n+=s/$c//}$_&&push@{$w[$n]},$o}<D>;@{pop@w}
    67 characters?
    jynx
      The map and pop suggestions work (very nice)... The $_ trick wont though, because (get this) $_ isn't actually empty at that point, it contains a single newline. Interestingly enough /^$/ will match a single newline.
      perl -le '$_ = "\n"; print /^$/' 1
      So, a tested version of your code comes in at 69 chars....
      # 1 2 3 4 5 6 #23456789012345678901234567890123456789012345678901234567890123456789 map{$n=$o=$_;for$c(@_){$n+=s/$c//}/^$/&&push@{$w[$n]},$o}<D>;@{pop@w}
      Update: The following code illustrates the issue a bit better...
      Notice how /^$/ && is not identical to $_ || and in this example the difference is critical:
      #!/usr/bin/perl -wT use strict; my @arr = ("\n","dog\n","cat\n"); my $patternmatch = 0; my $underscorematch = 0; /^$/ && $patternmatch++ for @arr; $_ || $underscorematch++ for @arr; print "Patternmatch = $patternmatch\n"; # <== prints 1 print "Underscorematch = $underscorematch\n"; # <== prints 0

      -Blake

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2024-04-16 09:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found