Looking at the other responses, I think I may have misunderstood you. You say that " items of the same value stored in the same group", but nowhere do you say that only contiguous ranges of values can be stored in a group.

If the latter is not a requirement, then this may be of interest. It does an optimal job (by my understanding) on the supplied dataset. The output order coudl be sorted (by say the first value in each group) to bring it back more in line with the starting set:

#! perl -slw use strict; use Data::Dump qw[ dump ]; $Data::Dump::MAX_WIDTH = 40; use List::Util qw[ reduce ]; sub group { my $nGroups = shift; my $ave = @_ / $nGroups; my %groups; push @{ $groups{ $_ } }, $_ for @_; while( keys %groups > $nGroups ) { my %sizes; push @{ $sizes{ @{ $groups{ $_ } } } }, $_ for keys + %groups; last if keys %sizes < 3; my @bySize = sort{$b<=>$a} keys %sizes; my %bySize = map{ $_ => 1 } @bySize; my $changed = 0; SIZE: for my $size ( @bySize ) { next if $size >= $ave; my $wanted = 1; ##int( $ave - $size + 0.5 ); { if( exists $bySize{ $wanted } ) { my $iToMove = shift @{ $sizes{ $size } }; my $iToAddto = shift @{ $sizes{ $wanted } }; push @{ $groups{ $iToAddto } }, @{ $groups{ $iToMo +ve } }; delete $groups{ $iToMove }; $changed++; last SIZE; } else { last if ++$wanted >= $size; redo; } } } unless( $changed ) { my $iToMove = shift @{ $sizes{ $bySize[ 0 ] } }; my $iToAddto = shift @{ $sizes{ $bySize[ 1 ] } }; push @{ $groups{ $iToAddto } }, @{ $groups{ $iToMove } }; delete $groups{ $iToMove }; } } return values %groups; } my @list = qw(1 1 1 2 3 4 4 4 5 5 6 6 6 7 7 8 8 8); my @lol = group( 6, @list ); print dump \@lol; __END__ C:\test>628986 [ [6, 6, 6], [3, 7, 7], [2, 5, 5], [8, 8, 8], [1, 1, 1], [4, 4, 4], ]

It does not (yet) fare so well on all randomly generated sets, and I'm sure there is some fat that can be trimmed out/simplified, but I'm reluctant to spend more time on it if I've misunderstood your requirements?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

In reply to Re: How to Group List Items? by BrowserUk
in thread How to Group List Items? by Thelonious

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.