in reply to sum towards nearest good neighbor at either end of numerical array

if the original array (0, 3, 8, 9, 5, 2, 0, 2, 1)
and minimal "goodness" is 5
how come you consider (11, 9, 10) good, when
in this situation it should be (11, 9, 5, 5)?
update
here is my humble solution:
@a = qw/1 1 0 3 8 9 5 2 0 12 2 1/; $m = 5; print "@a\n"; for $x (0 .. $#a){ if($a[$x] < $m){$a[$x+1] += $a[$x]; next;} if($a[$x+1] < $m and $x < $#a){$a[$x+1] += $a[$x]; next;} push @b, $a[$x]; } print "@b\n";
output
1 1 0 3 8 9 5 2 0 12 2 1 5 8 9 7 15
*update
have found a terrible problem with this
if (0, 3, 8, 9, 5, 2, 2, 0, 1, 0, 2, 0, 2, 1 continous to be the number lower
than minimum, that would creata an enormous number at the end of the resulting array
I'm sorry for confusion, working on correction
update
@a = qw/1 13 0 3 8 9 5 2 2 0 1 0 2 0 2 1 13 1/; $m = 5; grep $s += $_, @a; while ($s > $m){ $e = shift @a; if($e >= $m){ $s -= $e; push @b, $e; } else { $a[0] += $e;} } $s < $m ? $b[$#b] += $s : push @b, $s; print "@b\n";
this seems to be working in all kind of different original arrays

Replies are listed 'Best First'.
Re^2: sum towards nearest good neighbor at either end of numerical array
by rgart (Initiate) on Jan 04, 2014 at 01:02 UTC
    Thank you. It works perfectly under all conditions I've come up with; randomly generated frequency distributions for the type of test I referred to.