Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: (Golf) Decorating the Christmas Tree

by runrig (Abbot)
on Dec 03, 2001 at 22:58 UTC ( [id://129145]=note: print w/replies, xml ) Need Help??


in reply to (Golf) Decorating the Christmas Tree

I don't think the rules are clearly defined, so here's my attempt to clarify them (though Masem is the final arbiter, of course :), it sounds like we assume that $h and $f are already set, and so don't count that assignment, though we do count the assignment of the 'decoration character class'. So following what I think are the rules (subject to clarification), here's my try weighing in at 96 characters:
$j=-1;print" "x(--$h),map({$_==1?"*":rand()<$f?(qw(0 @ * +))[rand 4]:" +="}1..($j+=2)),$/for 1..$h
Update: Curses! foiled again :-)

Though japhy's reply does lead to this 87 character answer:

$j=1;print$"x--$h,($_-1?map rand>$f?"=":qw(0 @ * +)[rand 4],1..($j+=2) +:"*"),$/for 1..$h

Replies are listed 'Best First'.
Re: Re: (Golf) Decorating the Christmas Tree
by japhy (Canon) on Dec 03, 2001 at 23:12 UTC
    Every line of your tree starts with a star! But I can knock the count down a bit:
    $j--;print$"x--$h,($j+=2and$_-1?map rand>$f?"=" :qw(0 @ * +)[rand 4],1..$j:"*"),$/for 1..$h
    That's an even 90. Oh, I'm using Perl 5.6, so I can write qw(...)[...] and get away with it.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Re: (Golf) Decorating the Christmas Tree
by japhy (Canon) on Dec 04, 2001 at 00:08 UTC
    I can tie Rhose's 86 by a tiny adjustment to yours:
    $j=1;print$"x--$h,$_-1?map(rand>$f?"=":qw( 0 @ * +)[rand 4],1..($j+=2)):"*",$/for 1..$h
    I moved a set of parens. And I can trim it even more:
    print$"x--$h,$_-1?map(rand> $f?"=":qw(0 @ * +)[rand 4], 1..($j+=2)):"*",$/for++$j..$h
    I moved $j's assignment to the "fore" loop. 83 characters. And then another adjustment that gets rid of $j entirely, and puts me at (an updated) 77 -- the parens around map's arguments were unneeded.
    print$"x--$h,$_-1?map rand> $f?"=":qw(0 @ * +)[rand 4], 2..$_*2:"*",$/for 1..$h

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      Independently arriving at this (after playing some more), I get:
      print$"x$h--,$_-1?map{rand>$f?'=':qw(* 0 @ +)[rand 4]}2..2*$_:'*',$/fo +r 1..$h
      For some reason, I count that at 77?? Why am I one shorter?

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

        You are not putting parens around the map arguments, saving one character. You have block delimiters around the first map argument instead of parens around both arguments, so you don't need the comma to separate the two arguments. Congratulations :)

        Update: Though it seems the parens were completely unnecessary (though a space between map and the first arg is necessary in japhy's answer, so now your answer's are tied, i.e. length('map func,@arr')==length('map{func}@array')).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-04-19 02:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found