> -----Original Message-----
> From: Janek Schleicher [mailto:[EMAIL PROTECTED]] 
> Sent: 18 September 2002 16:34
> To: [EMAIL PROTECTED]
> Subject: Re: lots of numbers...
> 
> 
> I think, it's better to weight the sorted numbers
> with their sum rank, in the above example it would be:

> 
> Of course, you can also solve the real optimization problem,
> but for that you really should ask a mathematization.
> (But these algorithms are also slow :-))
> 

Your analysis is interesting. I am no mathemagician either, but it
occurs to me that it would be easy to code your analysis more
efficiently than using the sum rank, something like this: (not
debuggered)

use strict;

# Note that this array must be sorted ascending
my @numbers = (1,4,9,16,25,36,49,64,81);       

# How many groups will we create?
# What do we do if there are not exactly three numbers
# per group??
my $iterations = integer (scalar @numbers / 3);

my @groups;
foreach my $i ( 1..$iterations ) {
  my @group;
  # put the smallest and biggest value into groups
  push @group, pop( @numbers ), shift( @numbers );
  push @groups, \@group;
}

# Now we have to decide which group can best use the 
# remaining numbers. Process the groups from biggest
# to smallest, adding the smallest available value
foreach my $group ( sort { $b[0]+$b[1] <=> $a[0]+$a[1] } @groups ) {
  push @$group, pop @numbers;
}

# @groups now is an array of balanced tuplets! hoorah.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to