Find a perl golf solution that returns a sorted binary tree containing those items. The tree need not be balanced if this improves the golf. The tree is represented by a hash; the key is the value from the array above, and the value is an anonymous array ref of 2 elements, the first is a reference to a similar hash for the left branch, while the second is a hash ref for the right branch. If neither branch exists, the value should be 0. See the example solution for more details.
The comparison function that is given works like the spaceship operator or cmp, in that it takes two items, and returns -1 if the first item is less than the second, 0 if they are equal, and 1 if the first item is greater than the second.
As an example:
For extra fun, try to avoid the recursive solution.my @array = ( 1, 2, 3, 4, 5, 6 ); my $func = sub { $_[0] <=> $_[1] }; my %tree = buildtree( $func, @array ); # the golf function # one possible output : %tree => { 2 => [ { 1 => [ 0, 0 ] } , { 4 => [ { 3 => [ 0, 0 ] }, { 5 => [ 0, { 6 => [ 0, 0 ] } ] } ] } ] }
-----------------------------------------------------
Dr. Michael K. Neylon - mneylon-pm@masemware.com
||
"You've left the lens cap of your mind on again, Pinky" - The Brain
It's not what you know, but knowing how to find it if you don't know that's important
|
|---|