in reply to How to Group List Items?

Your definition is incomplete, you must assign priorities.

For instance, you want each group to have the same number of items and you only want max 6 groups. If you only have 1 group containing all items, that would satisfy the requirements, but you probably don't want that :-)

You also don't specify if you want to allow the same number to be in more than one group, though it looks like you don't, from your example.

Update: you also in other words: you don't specify how you want to score the returned groups. For instance, why don't you prefer (pseudocode):

[ [1 1 1 2 3], [4 4 4 5 5], [6 6 6 7 7], [8 8 8] ]

Replies are listed 'Best First'.
Re^2: How to Group List Items?
by Thelonious (Scribe) on Jul 26, 2007 at 20:13 UTC
    Sorry - I'm looking to have 6 groups, no fewer.

    I'd like the groups scored by how many items they contain different from the ideal (the lower, the better):

    my $ideal_avg_per_group = @list / 6; # your solution my $solution = [ [1 1 1 2 3], [4 4 4 5 5], [6 6 6 7 7], [8 8 8] ]; my $this_score = 0; for (@$solution) { my $num_items = @$_; $this_score += abs($ideal_avg_per_group - $num_items); }
    Also, it's fine to assume that there are more than 6 unique values in the list. Otherwise, it's obviously very easy.

    Thanks!

      I still do not see where you are defining the concept of $ideal_avg_per_group. You can look at Algorithm::Bucketizer to see if that gets you anywhere -- but without a better definition of the problem it is anyones guess what a solution really is.


      -Waswas
        Look for where he assigns it.