This may be completely off-base (and might not work precisely on odd numbered lists) but is an in-place transform within the scope of the solution? Seems like it might be quite fast, especially if you only need to transform some lines and do that with random skips through the original.

use warnings; use strict; use IO::File; my ( $sum_orig, $sum_new, $lines, $lines_new ); open my $out, ">", "reorganized.txt" or die $!; while (<DATA>) { my ( $size, $file ) = split; $sum_orig += $size; if ( defined ( $_ = <DATA> ) ) { my ( $size2, $file2 ) = split; $sum_orig += $size2; my $max = $size > $size2 ? $size2 : $size; my $rejigger = int rand($max/2); my $tmp2 = $size - $rejigger; my $tmp1 = $size2 + $rejigger; $out->printf("%-7d %s\n", $tmp1, $file); $out->printf("%-7d %s\n", $tmp2, $file2); $sum_new += $tmp1 + $tmp2; $lines_new += 2; $lines++; } $lines++; } print "Original average: ", $sum_orig / $lines, $/; print " New average: ", $sum_new / $lines_new, $/; __DATA__ 4329 file1 12311 file2 657 file3 4271 file4 20 file5 10001 file6

In reply to Re: Creating a sub-list constrained by an average while maintaining maximum number of original elements by Your Mother
in thread Creating a sub-list constrained by an average while maintaining maximum number of original elements by Jack B. Nymbol

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.