So this is what I believe you currently have , expressed as partitions (for ease of thinking/debugging/vocabulary)
#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; my @want = (11, 9, 10); #~ sum(0, 3, 8), #~ 9, #~ sum(5, 2, 0, 2, 1) my @bad_ari = (0, 3, 8, 9, 5, 2, 0, 2, 1); my $min = 5; # minimal "goodness" my @partition = []; for my $val ( @bad_ari ){ push @{ $partition[-1] }, $val; if( $val >= $min ){ push @partition, []; } } dd( \@partition ); ## [[0, 3, 8], [9], [5], [2, 0, 2, 1]] __END__
So compared to what you got that was wrong (11, 9, 5, 4) you can see that 2,0,2,1 doesn't add up to four it adds up to five, so you have off by one error in your c style for loop
Also, like Lennotoecom says , maybe what you want is wrong as well .... I wouldn't know :)
In reply to Re: sum towards nearest good neighbor at either end of numerical array (partition off by one )
by Anonymous Monk
in thread sum towards nearest good neighbor at either end of numerical array
by rgart
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |