Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^2: Pascal-Sierpinski for golfers

by eyepopslikeamosquito (Canon)
on Nov 29, 2011 at 21:18 UTC ( #940700=note: print w/ replies, xml ) Need Help??


in reply to Re: Pascal-Sierpinski for golfers
in thread Pascal-Sierpinski for golfers

perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeguntil/560/'
Shaving 12 strokes (down to 41 bytes):
s/^|\d+/$&+$'||1/eg,print,s// /for($/)x16
(Can run this from a file or via perl -e).

Update: if you are willing to allow a trailing space on each line that can be reduced to 38 bytes:

s/^/1 /,print,s/\d+/$&+$'/egfor($/)x16

Using perl -l, five more:

#!perl -l s/^/1 /,print,s/\d+/$&+$'/eguntil/08/ s/^/1 /,print,s/\d+/$&+$'/eg^16&&do$0 1while s/^/1 /,print,s/\d+/$&+$'/eg^16 {s/^/1 /,print,s/\d+/$&+$'/eg^16&&redo} eval's/^/1 /;print;s/\d+/$&+$\'/eg;'x16


Comment on Re^2: Pascal-Sierpinski for golfers
Select or Download Code
Re^3: Pascal-Sierpinski for golfers
by hbm (Hermit) on Nov 30, 2011 at 17:39 UTC

    I really appreciate two tricks:

    • s// /
      I understand this, and have used the empty expression before with m//, but never thought to try it with s///.
       
    • for($/)x16
      A slick way of getting the newline (vis-à-vis -l print until...)!
      Changing the loop to for($_)x16 is informative.
      And, since a bare print prints both $_ and $/, I found it rather unobvious which variable holds the strings.

      for($/)x16
      A slick way of getting the newline (vis--vis -l print until...)! Changing the loop to for($_)x16 is informative. And, since a bare print prints both $_ and $/, I found it rather unobvious which variable holds the strings.
      Actually, $/ is the input record separator, so a bare print does not print it. What's being printed is just the (localized) $_ inside the for loop. Of course, a bare print will print $\, aka the output record separator, often seen in golf, as in, for example, The golf course looks great, my swing feels good, I like my chances (Part I).

      The for($/)x16 loop aliases $/ and, despite the x16, only the single $/ value is (repeatedly) changed, as you will discover by printing it after the loop has terminated. Replacing "print" with "warn" like so:

      s/^|\d+/$&+$'||1/eg,warn("du='$_' ds='$/'\n"),s// /for($/)x16; warn("at end du='$_' ds='$/'\n");
      should clarify. BTW, this eccentric "for loop aliasing" trick is often seen in golf; see, for example, Drunk on golf: 99 Bottles of Beer in the "Bottle Golf Tip No 2" section (search for Cantor). Finally, the "for loop aliasing" trick is a specific example of Eugene van der Pijll's general golfing mantra, "Can't possibly work, let's try it anyway". :)

        Doh, I flubbed that ($/ vs. $\). I so often print$/,/expr/?this:that, I confused myself.

        And darn you - I had already read your lengthy linked topics, and perhaps all of their references, but I find them irresistable! This time through, I most enjoyed this gem:

        s///ge>>9?print:do$0

        The :do$0 is one *sick* loop. And the return value from s/// idea gave me the following "for 1 to 15" method:

        perl -le'$_=1;{print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeg>16||redo}'

        A couple more tricks for me!

Re^3: Pascal-Sierpinski for golfers
by brx (Pilgrim) on Nov 30, 2011 at 20:46 UTC
    Nice, thank you ! With the same idea for Sierpinski :
    #!/usr/bin/perl -l print,s/^/_/,s/./$'& z^$&^Z/eg,y/5/o/for($a=o)x80

      With the same idea for Sierpinki:
      #!/usr/bin/perl -l print,s/^/_/,s/./$'& z^$&^Z/eg,y/5/o/for($a=o)x80
      There are a variety of Sierpinski triangle solutions given in the documentation for Acme::EyeDrops. For example, this short one was composed by mtve in 2002:
      #!perl -l $x=2**pop;print$"x--$x,map$x&$_?$"x2:"/\\",0..$y++while$x
      This version takes a single command line argument; running "perl sier.pl 5", for example, produces the same result as that shown in Re: Pascal-Sierpinski for golfers.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2014-08-23 20:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (178 votes), past polls