2
Given the following list of numbers, find the group of sequential positive numbers with the highest sum.
Example input:
12 2 35 -1 20 9 76 5 4 -3 4 -5 19 80 32 -1
Example output:
131
The most succinct code wins.
My approach via Perl is 87 chars (with input as command-line args):
print((sort { $b <=> $a } do { push @s, $_ > 0 ? pop(@s) + $_ : 0 for @ARGV; @s })[0]);
2This lacks an objective winning criterion. Also, as the rules are, I could just
print 179
and have a valid solution. – cemper93 – 2011-12-31T21:06:59.4672Wouldn't it give 131? 20+9+76+5+4=114 and 19+80+32=131 – Joanis – 2011-12-31T21:17:27.963
3
possible duplicate of Find largest sum of subsequence
– Peter Taylor – 2012-01-01T02:44:01.800not duplicate, since the other question is not restricted to positive ints. – user unknown – 2012-01-02T03:34:05.530
@userunknown right idea, wrong words. The other problem uses an UNSIGNED INT generator, this uses standard INTs. – arrdem – 2012-01-02T04:42:46.187
@rmckenzie: I guess you're wrong. See example:
printf "1\n2\n-1\n4\n" | ./sum \\ \n 6
. He shifts the values in the end. You may jump over negative values, if you gain more points with the value on the other side, than it costs, to pick the negative value, while here, it is an absolute barrier, which you can't cross. – user unknown – 2012-01-02T05:32:34.003Sorry @cemper93, what could I have said to make it more clear? I figured it was self-explanatory. – Al Newkirk – 2012-01-02T07:46:35.527
@M.Joanis yes, you're right, the example input line got pasted incorrectly, .... sorry about that. – Al Newkirk – 2012-01-02T08:01:50.923
@userunknown, you're right that it's not a perfect duplicate, but it's so close that it's not worth having as a separate challenge. – Peter Taylor – 2012-01-02T08:54:31.557
1@Al Newkirk: You should specify which solution will be the winner. If this shall be the solution with the least characters (standard code-golf), please add a code golf tag. Also, don't say "use the following input", but specify something like "the input will be on standard input, and use the following input to test your submission". – cemper93 – 2012-01-02T13:58:52.730
1@userunknown I stand corrected. – arrdem – 2012-01-02T19:57:12.170
1@AlNewkirk: I copyedited your post a bit, and removed the second copy of the example input. Could you please check that I didn't accidentally change the meaning of what you wrote. Also, as cemper93 notes, you really should clarify the winning criterion and tag your post accordingly (as [tag:code-golf] or [tag:code-challenge] for example). – Ilmari Karonen – 2012-01-04T16:11:07.450
1Why did you accept M42's solution which isn't the shortest? – user unknown – 2012-01-08T15:18:35.397