Created
February 19, 2014 02:19
-
-
Save stevenbarragan/9084913 to your computer and use it in GitHub Desktop.
Tape equilibrium (Codility exercise)
This file contains 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 solution(a) | |
minor = 1/0.0 | |
left = 0 | |
right = a.reduce(:+) | |
a[0..-2].each do |x| | |
left += x | |
right -= x | |
abs = (left - right).abs | |
minor = abs if abs < minor | |
end | |
minor | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment