in reply to Creating a sub-list constrained by an average while maintaining maximum number of original elements

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
  • Comment on Re: Creating a sub-list constrained by an average while maintaining maximum number of original elements
  • Download Code