Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Pascal-Sierpinski for golfers

by hbm (Hermit)
on Nov 29, 2011 at 15:12 UTC ( [id://940640]=note: print w/replies, xml ) Need Help??


in reply to Pascal-Sierpinski for golfers

Golf? Here's a first pass:

perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeg until$a++>15' perl -le'$_=1;print,s/(\d+) ?/+$1 $1/g,s/\S+/$&/eeguntil/560/'

Replies are listed 'Best First'.
Re^2: Pascal-Sierpinski for golfers
by eyepopslikeamosquito (Archbishop) on Nov 29, 2011 at 21:18 UTC

    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

      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". :)

      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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://940640]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-20 00:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found