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

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 :)

  • Comment on Re: sum towards nearest good neighbor at either end of numerical array (partition off by one )
  • Download Code

Replies are listed 'Best First'.
Re^2: sum towards nearest good neighbor at either end of numerical array (partition off by one )
by Lennotoecom (Pilgrim) on Jan 02, 2014 at 08:35 UTC
    awesome solution,
    *bows in respect
    update* though if the original array will be
    (1, 1, 0, 3, 8, 9, 5, 2, 0, 2, 1)
    that would give  [[1, 1, 0, 3, 8], [9], [5], [2, 0, 2, 1]]
    and if the author want's values be around 5, that's not very good
    also if the original massive will be (1, 1, 0, 3, 8, 9, 5, 2, 0, 12, 2, 1)
    that would give a completely wrong answer:
    [[1, 1, 0, 3, 8], [9], [5], [2, 0, 12], [2, 1]]