http://qs1969.pair.com?node_id=79865

mr.nick has asked for the wisdom of the Perl Monks concerning the following question:

All the questions of "Here is a problem, write a solution" bouncing around here recently has reminded me of the first programming competition I competed at, at Duke University way-back in '87.

I've tried to remember some of the problems we were presented and so far have only remembered one:

Write a program that outputs:

* *** ***** ******* ***** *** *
Now, granted this isn't of the same level as Masem's (Golf) Shortest Graph Distance by any stretch, but it's brings back memories.

So, what's the shortest you can get the solution in Perl? Mine is at 51 currently (strict & -w safe):

for(0..3,2,1,0){print" "x(3-$_),"*"x($_*2+1),"\n"}

Replies are listed 'Best First'.
Re: Programming Contest (High School '87)
by no_slogan (Deacon) on May 12, 2001 at 03:37 UTC
    39 characters

    map{print$"x abs,"*"x(7-2*abs),$/}-3..3

    and 37

    print$"x abs,"*"x(7-2*abs),$/for-3..3

      That is the best organization of the problem. All that I can add is a slimy trick. The spec said output, but did not say where that data had to go:
      warn$"x abs,"*"x(7-2*abs),$/for-3..3
      I am not sure whether saving this character would be legal in a real golf competition...
Re: Programming Contest (High School '87)
by MrNobo1024 (Hermit) on May 12, 2001 at 03:21 UTC
    43 characters:
    print$"x(4-$_/2),"*"x$_,$/for 1,3,5,7,5,3,1
Re: Programming Contest (High School '87)
by Kanji (Parson) on May 12, 2001 at 03:31 UTC

    I can get it down to 41 by juggling your code around and making use of the -l switch ... or 43 if you count the -l. :)

    print$"x(3-$_),"*"x($_*2+1)for 0..3,2,1,0

        --k.


Re: Programming Contest (High School '87)
by MeowChow (Vicar) on May 12, 2001 at 06:00 UTC
    Not particularly short, but it sure was fun to learn something about the byzantine format builtin:
    format= @|||||| $_ . write for map'*'x(7-2*abs),-3..3
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
(Ovid) Re: Programming Contest (High School '87)
by Ovid (Cardinal) on May 12, 2001 at 03:32 UTC
    I ain't even in the same ballpark at 88, but it was fun to write :)
    $,="\n";$a[6]='';for(1..3){$a[$_-1]=$a[-$_]=" "x(4-$_)."*"x(2*$_-1)}$a +[3]="*"x7;print@a

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Programming Contest (High School '87)
by perlmonkey (Hermit) on May 12, 2001 at 05:15 UTC
    print<<A; * *** ***** ******* ***** *** * A
    55 characters :)
      Why the here-script?
      print' * *** ***** ******* ***** *** *'
      print <<; * *** ***** ******* ***** *** *
      Two shorter, but I still count 55. Gives a "deprecated" warning with -w.

Re: Programming Contest (High School '87)
by damian1301 (Curate) on May 12, 2001 at 09:29 UTC
    In the process of trying to make one shorter than the rest, decided to take a slight turn off the path and came up with this. It took me a damn long time to figure out how to get them both the right way and it still isn't up to my standards (and it is long), but I hope you like it anyway. :)

    #!/usr/bin/perl -w use strict; for(2,3,4,7,4,3,2){unless($_ == 7){print "\t"," "x($_-3/4),"*"x$_, "\n";}else{print "\n","="x($_+$_+2),"\n";}}print"\n\t\n";for(5,4,3, 2,3,4,5){unless($_ == 2){print "\t"," "x($_-5/4),"*"x$_,"\n";}else{ print "\n"," "x($_+6),"="x($_+14),"\n";}}


    Tiptoeing up to a Perl hacker.
    Dave AKA damian