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

Re: Triangle Golf

by MeowChow (Vicar)
on Apr 24, 2001 at 22:29 UTC ( [id://75181]=note: print w/replies, xml ) Need Help??


in reply to Triangle Golf

$c=(@ARGV,$/)='ctriangle.txt';$_=<>.$";y/ / /s;print$"x(40-($c=length$&)/2),"$&\n"while/.{1,$c}.*? /g
102, including the newline ;)

update: changed two ' ' to $", saving an additional two characters - liberally aped from jeroenes node :-)

update2: shuffled the $/ assignment into the list assignment, saving another extra char.

   MeowChow                                   
               s aamecha.s a..a\u$&owag.print

Replies are listed 'Best First'.
Re: Re: Triangle Golf
by petral (Curate) on Apr 25, 2001 at 21:15 UTC
    Here's some variations on MeowChow's idea above:
    a perfectly respactable program, about 140 chars (after the #!.../):
    #!/usr/local/bin/perl -ln0777 $\ =' '; $c = 0; s/^\s+//; s/\s+/ /gs; while ( s/(.{$c,}?. |.+$)// ) { print " " x (40 - ($c = 1 + length $1) / 2), $1 }
    > triangle.pl a.txt w.txt
    a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a The same principle applies to any text file, basically splitting words only on spaces or new lines
    a one-liner, 74 chars (+6 extra flag chars):
    > perl -ln0777we '$c=0;s/\s+/ /sg;print" "x(40-($c=length$1)/2),$1 whi +le s/(.{$c,}?. |.+$)//' a.txt w.txt a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a The same principle applies to any text file, basically splitting words only on spaces or new lines
    and pretty well golfed, 66 chars (+6 extra flag chars):
    > perl -ln0777e's/\s+/ /sg;print$"x(40-($c=length$&)/2),$&while s/.{0$ +c,}?. |.+$//' a.txt w.txt a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a The same principle applies to any text file, basically splitting words only on spaces or new lines > perl -ln0777e's/\s+/ /sg;print$"x(40-($c=length$&)/2),$&while s/.{0$ +c,}?. |.+$//' w.txt a.txt The same principle applies to any text file, basically splitting words only on spaces or new lines a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
    p
      Very nice paring down, I especially like the conversion from a greedy expression to the minimal one you use, in order to avoid defining $c. Further reduction gives:
      perl -ln0e's/\s+/ /g;print$"x(40-($c=length$&)/2),$&while/.{0$c,}? |.* +/g' ...
      61 chars of code plus 3 modifier chars.
         MeowChow                                   
                     s aamecha.s a..a\u$&owag.print
        Much cleaner! Putting the increment back in and not skipping a line at the end yields a "more intresting"* result (63 chars):
        perl -ln0e's/\s+/ /g;print$"x(40-($c=2+length$&)/2),$&while/.{0$c,}? | +.+/g' a.txt w.txt a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a The same principle applies to any text file, basically splitting words only on spaces or new lines perl -ln0e's/\s+/ /g;print$"x(40-($c=2+length$&)/2),$&while/.{0$c,}? | +.+/g' w.txt a.txt The same principle applies to any text file, basically splitting words only on spaces or new lines a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a a
        (I know, I know. I'm the only one who could possibly still be interested at this point.)

        * update "more interesting" meaning more <|triangular|>  (than the original challenge).

        And, moving even deeper into 'useless use of code in void context':
        print($"x(40-($c=2+length$&)/2),$&),$c*=($c<80)while...
        will make triangles of files of any length (a step towards automated obfusification?).

        p

Log In?
Username:
Password:

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

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

    No recent polls found