### Re: (Golf) Decorating the Christmas Tree

by jynx (Priest)
 on Dec 04, 2001 at 03:31 UTC ( #129234=note: print w/replies, xml ) Need Help??

in reply to (Golf) Decorating the Christmas Tree

Rather than checking rand for different values, i construct a heuristic and run through it for each character. It's only vaguely correct since it's difficult to extract integer values appropriately in only a few keystrokes. The first entry weighs in at 120 characters and doesn't assume that \$f and \$h are set for me. The second weighs in at 103 characters and assumes that they are set (but doesn't run under warnings). Thanks for the interesting golf Masem :)

```#1
sub f{
(\$a,\$b,@_)=(@_,split//,'0@*+'.'='x(4/(pop)-4);
\$b=2*\$_-1,print\$"x((2*\$a-1-\$b)/2),\$_>1?map\$_[rand@_],1..\$b:'*',\$/for 1
+..\$a
}

#2
sub g{
+split//,'0@*+'.'='x(4/\$f-4);
\$b=2*\$_-1,print\$"x((2*\$h-1-\$b)/2),\$_>1?map\$_[rand@_],1..\$b:'*',\$/for 1
+..\$h
}
jynx

update: i didn't notice the --\$h trick, so i've shortened those still further:
update2:oops, my heuiristic was hard-coded for 0.3. i've corrected all versions.

```#1 (at 97 characters)
sub f{
(\$a)=@_;+split//,'0@*+'.'='x(4/(pop)-4);
print\$"x--\$a,\$_>1?map\$_[rand@_],1..2*\$_-1:'*',\$/for 1..\$a
}

#2 (at 86 characters)
sub g{
+split//,'0@*+'.'='x(4/\$f-4);
print\$"x--\$h,\$_>1?map\$_[rand@_],1..2*\$_-1:'*',\$/for 1..\$h
}

Create A New User
Node Status?
node history
Node Type: note [id://129234]
help
Chatterbox?
 [davido]: Using the -i switch causes Perl to rename the input file, read from it, and write to a file using the original file's name. If there's an extension provided, as in -i.bak, it's easy to see where the input file is. Where is the input file temporarily.... [davido]: placed if there is no extension provided to the -i switch? [davido]: Nevermind, found the answer. [davido]: If no extension is supplied, and your system supports it, the original file is kept open without a name while the output is redirected to a new file with the original filename. When perl exits, cleanly or not, the original file is unlinked. [haukex]: doc says "If no extension is supplied, and your system supports it, the original file is kept open without a name ..."

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (14)
As of 2017-09-22 14:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
During the recent solar eclipse, I:

Results (264 votes). Check out past polls.

Notices?