Thelonious has asked for the wisdom of the Perl Monks concerning the following question:
I have lists, and I need to group them with items of the same value stored in the same group, and each group having as close to the same number of items as possible, with a maximum of X groups (assuming 6 here)...
That would group these items like this:my $best_score = 9999; my $best_combo; my @list = qw(1 1 1 2 3 4 4 4 5 5 6 6 6 7 7 8 8 8); while (my $combo = get_combo(\@list)) { # hard to do well?? $score = get_score($combo); # easy if ($score < $best_score) { $best_score = $score; $best_combo = $combo; } } print $best_score.': '.Dumper($best_combo);
I know that there must be some algorithm out there to get this done, but it's perplexing me... any thoughts?$VAR1 = [ [qw(1 1 1 2)], [qw(3 4 4 4)], [qw(5 5 )], [qw(6 6 6)], [qw(7 7)], [qw(8 8 8 9)], ];
Many Thanks!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: How to Group List Items?
by Joost (Canon) on Jul 26, 2007 at 19:58 UTC | |
by Thelonious (Scribe) on Jul 26, 2007 at 20:13 UTC | |
by waswas-fng (Curate) on Jul 26, 2007 at 21:18 UTC | |
by ysth (Canon) on Jul 27, 2007 at 01:45 UTC | |
Re: How to Group List Items?
by BrowserUk (Patriarch) on Jul 27, 2007 at 03:14 UTC | |
by Thelonious (Scribe) on Jul 27, 2007 at 15:18 UTC | |
Re: How to Group List Items?
by GrandFather (Saint) on Jul 27, 2007 at 02:16 UTC | |
by Thelonious (Scribe) on Jul 27, 2007 at 15:14 UTC | |
Re: How to Group List Items?
by Ouato (Novice) on Jul 27, 2007 at 08:18 UTC |