sub Return_Middle
{
my @list = @_;
return @list if ( scalar(@list) == 2 or
scalar(@list) == 1 );
my @sorted = sort { $a <=> $b || $a cmp $b } @list;
my @middle = drill_down(@sorted);
return @middle;
sub drill_down
{
my @list = @_;
return @list if ( scalar(@list) == 2 );
return @list if ( scalar(@list) == 1 );
splice @list, -1, 1;
splice @list, 0, 1;
drill_down(@list);
}
}
How 'bout
sub drill_down {
my $i=@_/2;
@_[ @_%2 ? $i : ($i-1, $i) ];
}
instead?
Also, in various order:
drill_down(@sorted); # as the last statement in your subinstead of
my @middle = drill_down(@sorted); return @middle;
sub Return_Middle {
my $midpt = ($#_/2);
@_[int($midpt) .. int($midpt + .5)];
}
See also Re: Puzzle: The Ham Cheese Sandwich cut. (by robin) and Re: Puzzle: The Ham Cheese Sandwich cut. (by me) for a different way to find the median.
perlmonks.org content © perlmonks.org and ambrus, blazar, Roy Johnson, tcf03
prlmnks.org © 2006 edmund von der burg (eccles & toad)
v 0.03