As for the interesting case of all negative numbers noted by BrowserUK++, coming from the initial "best" intervall being an empty list with sum zero, which will then serve as an upper limit not to surpass in the loop.
I think that can be dealt with by
Update: hiding original approach in readmore tags, since it is incorrect and now obsolete
- Setting the first element for the best intervall, if the @array is non-empty, prior to entering the loop
- Swapping the order of the if-elsif tests
- But then having to check again for $cur < 0 wrt setting $best_start
As in:
# initialisation as before
$cur = $best = $array[$cur_start] if @array;
for (1..$#array) {
$cur += $array[$_];
if ($best < $cur) {
$best = $cur;
$best_start = $cur < 0 ? $_ : $cur_start;
$best_end = $_;
}
elsif ($cur < 0) {
$cur = 0;
$cur_start = $_ + 1;
}
}
(start Update insert)
So instead of messing that much with tilly's code I now concentrate on the "best" initialization
before we enter the loop.
For my addendum loops through the beginning of @array as long as we are still seeing negative
numbers.
When we finally see a non-negative we proceed with tilly's code from there.
To be prepared for the case that all elements are negative, we adjust the *_start, *_end, best*, cur* variables heading for the max (= least negative number) instead.
Which leaves me with some code not so elegant anymore
Maybe someone else is able to reimplement tilly's original idea also to work for all negative numbers in a more elegant/compact/concise way
#! /usr/bin/perl -w
use strict;
my @array = @ARGV ? @ARGV : qw(3 2 8 9 -25 5 8 4 4 -3 5 3 -10);
my $cur = my $best = my $best_start = my $cur_start = 0;
my $best_end = -1;
for (0 .. $#array) {
my $elem = $array[$_];
if ($elem < 0) {
if (!$best or $best <= $elem) {
$cur = $best = $elem;
$best_start = $best_end = $_;
}
}
else {
$cur_start = $best_start = $best_end = $_;
$cur = $best = $elem;
last;
}
}
for ($best_end+1..$#array) {
$cur += $array[$_];
if ($cur < 0) {
$cur = 0;
$cur_start = $_ + 1;
}
elsif ($best < $cur) {
$best = $cur;
$best_start = $cur_start;
$best_end = $_;
}
}
print qq(Start: $best_start
End: $best_end
Sum: $best
Numbers: @array[$best_start..$best_end]
);
(end update insert)
Otherwise I think the idea behind the algorithm presented by tilly++ is correct, or so I hope.
Update: Still not correct as presented. Back to the drawing board...
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.