Created
July 29, 2014 00:16
-
-
Save mrdougwright/5640c2124efeb36d2c50 to your computer and use it in GitHub Desktop.
Max Slice
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def max_slice(arr) | |
arr.size.times do |x| | |
if sum(arr[x..-1]) >= sum(arr) | |
arr.shift | |
end | |
if sum(arr[x..-2]) > sum(arr) | |
arr.pop | |
end | |
end | |
p arr | |
end | |
def sum(arr) | |
arr = Array(arr) | |
arr.inject(0) { |sum, x| sum + x.to_i } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment