Last active
April 9, 2016 05:03
-
-
Save marythought/09a0f73145d18e2b3c56579d4524f600 to your computer and use it in GitHub Desktop.
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 array_sum(array) | |
array.length.times do | |
temp = array.shift | |
return [[temp], array] if temp == array.reduce(&:+) | |
array << temp | |
end | |
[] | |
end | |
# it passes test cases | |
describe 'the array sum function' do | |
it "returns two arrays when one element is the sum of the others" do | |
assert_equal(array_sum([1, 2, 3, 6]), [[6], [1, 2, 3]]) | |
assert_equal(array_sum([-4, 5, 8, -3, 10]), [[8], [-3, 10, -4, 5]], "with negative nums") | |
end | |
it "returns an empty array when an array does not fit the pattern" do | |
assert_equal(array_sum([1, 2, 3, 7]), []) | |
assert_equal(array_sum([]), [], "an empty array") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment