Skip to content

Instantly share code, notes, and snippets.

@bookis bookis/sums.rb
Created Mar 29, 2014

Embed
What would you like to do?
# method that takes two inputs (an array, a target sum)
# outputs an array of pairs that sum to the target sum
# ([1,2,3,4], 5) => [[1,4], [2,3]]
def sum_pairs(array, target)
matches = []
i = 0
array.each do |first|
i += 1
array[i..-1].each do |second|
matches << [first, second] if first + second == target
end
end
matches
end
puts sum_pairs([1,2,3,4,1,43,54,75,2,1,23,23,642,357,485,235,124,124,12,21,2,32,2,353,453,45,34,53,53,3,324,52,53,45,234,23,42,6,346,457,634,523,54,2,2,2,2,4,4,5,6,7], 90).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.