in reply to (Golf) Building a Better Binary Tree

54 characters, avoiding the recursive solution.
sub h { #23456789_123456789_123456789_123456789_123456789_1234 $x=0;$s=shift;$x={$_,[$x,0]}for sort{&$s($a,$b)}@_;%$x }
Those complaining about strict.pm are invited to substitute $a and $b for $x and $s respectively. (Just to demonstrate that becoming strict does not necessarily result in clearer code...)

UPDATE
Updated per dragonchild's point about the spec misunderstanding. (How did this get updated? Well let me just say that after conferring with the author of the post, a kind janitor did the favour of editing it...)

Replies are listed 'Best First'.
Re: Re (tilly) 1: (Golf) Building a Better Binary Tree
by dragonchild (Archbishop) on Oct 08, 2001 at 20:45 UTC
    Unfortunately, it doesn't work. Easily fixed by changing the last statement to %$x. 54 characters.

    I do have an aesthetic issue with it - your code produces the worst possible tree, not a nearly-optimal tree. :-)

    ------
    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.

Re (tilly) 2: (Golf) Building a Better Binary Tree
by tilly (Archbishop) on Oct 08, 2001 at 20:38 UTC
    D'oh. That was me.
Re: Re (tilly) 1: (Golf) Building a Better Binary Tree
by jynx (Priest) on Oct 09, 2001 at 21:42 UTC

    A question,

    You can eliminate calling $s as a function (and the argument passing) and it will still work for the test cases i've been using. Is there a reason not to do that? That is, is there a reason to use the full function call when passing the function ref will work?

    If you pulled that out, you would get:

    #23456789012345678901234567890123456789012345 $x=0;$s=shift;$x={$_,[$x,0]}for sort$s @_;%$x
    at 45 characters.

    jynx

    update: alas, this is a false optimization. Thank you tilly for correcting my eyesight :)

      I haven't tested under Perl 5.6, but under 5.005 your code does not work.

      Remember that the comparison function specified depends on the arguments passed to it, and not on $a and $b. Therefore the comparison function is not a valid sort sub and you need to do that transform.

      Try this to see what I mean.

      print Dumper h(sub {$_[0] <=> $_[1]}, 1..6, -3);
      With your code that fails. (BTW your other solution also seems to have used this incorrect optimization.)