-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlargestsum.rb
More file actions
31 lines (29 loc) · 857 Bytes
/
largestsum.rb
File metadata and controls
31 lines (29 loc) · 857 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
=begin
SUM OF INTEGERS
CHALLENGE DESCRIPTION:
Write a program to determine the largest sum of contiguous integers in a list.
INPUT SAMPLE:
The first argument is a path to a filename containing a comma-separated list
of integers, one per line.
For example:
-10,2,3,-2,0,5,-15
2,3,-2,-1,10
OUTPUT SAMPLE:
Print to stdout the largest sum. In other words, of all the possible
contiguous subarrays for a given array, find the one with the largest sum, and print that sum.
For example:
8
12
=end
lines = File.readlines(ARGV[0])
lines.each do |line|
int_array = line.chomp.split(",").map { |el| el.to_i }
largest = int_array[0]
1.upto(int_array.length) do |sublength|
int_array.each_cons(sublength) do |subarray|
count = subarray.reduce(:+)
count > largest && largest = count
end
end
puts largest
end