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

In reply to Re: sum towards nearest good neighbor at either end of numerical array by Lennotoecom
in thread sum towards nearest good neighbor at either end of numerical array by rgart

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.