Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

sum of continues array

by sanku (Beadle)
on Sep 10, 2008 at 05:01 UTC ( [id://710268]=perlquestion: print w/replies, xml ) Need Help??

sanku has asked for the wisdom of the Perl Monks concerning the following question:

hi monks, Can any one give suggestion on the following one. I am having an array like this @array=(1,2,3,4,-1,-2,-3,11,12,13); i want to take the sum of only positive continues number and i have 2 display the maximum sum of that array. Thanks in advance.

Replies are listed 'Best First'.
Re: sum of continues array
by ikegami (Patriarch) on Sep 10, 2008 at 05:12 UTC

    Did you just spell "to" as "2"? And what's a "maximum sum"? Anyway,

    use List::Util qw( sum ); my $sum_of_pos = sum grep { $_ > 0 } @array;
    or
    my $sum_of_pos = 0; for (@array) { $sum_of_pos += $_ if $_ > 0; }

    Update: Oops, missed the "continues". Maybe that's a misspelling of "contiguous", and maybe you mean "contiguous and increasing"???

    my $prev; my $sum; my $max = -1; for (@array) { if (defined($prev) && $_ == $prev+1) { $prev = $_; $sum += $_; next; } if (defined($prev)) { $max = $sum if $sum > $max; undef $prev; undef $sum; } #if ($_ > 0) { # zero isn't considered positive if ($_ >= 0) { # zero is considered positive $prev = $_; $sum = $_; } }
Re: sum of continues array
by GrandFather (Saint) on Sep 10, 2008 at 05:38 UTC

    What have you tried?

    You could:

    use strict; use warnings; my @array = (1, 2, 3, 4, -1, -2, -3, 11, 12, 13); my $max = 0; my $sum; ($a = ($_ > 0 ... $b + 1 != $_)), ($a and $a == 1 and $sum = 0), ($a and $a !~ /E0/ and $sum += $b = $_), ($a and $sum > $max and $max = $sum) for @array; print $max;

    which prints 36, but your teacher may not like that.


    Perl reduces RSI - it saves typing
Re: sum of continues array
by BrowserUk (Patriarch) on Sep 10, 2008 at 06:50 UTC

    See what teach makes of this :)

    Update: Corrected logic from 'add if larger than last value' to 'add if larger than last value added.

    #! perl -slw use strict; use List::Util qw[ reduce ];; my @array=(1,2,3,4,-1,-2,-3,11,12,13);; my $sum = @{ reduce{ @{ $a } = ( $a->[0]+$b, $b ) if $b > $a->[1]; $a; } [0, -9e99 ], @array }[0]; print $sum;

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      Thank you. it's working fine.
        I'm sure it is working fine, until your teacher realizes how advanced the technique is for your level and googles it. Your academic career worth it?
Re: sum of continues array
by themage (Friar) on Sep 10, 2008 at 09:27 UTC
    hi sanku, what about this:
    my @array=(1,2,3,4,-1,-2,-3,11,12,13); my @sums=(); my $last=0; map { push @sums, $last+$_ if $last>0 and $_>0; $last=$_; } @array; my $max=0; map {$max=$_ if $_>$max } @sums; print "Max sum: $max\n";

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://710268]
Approved by lamp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found