Skip to content

Instantly share code, notes, and snippets.

@bookis
Created March 29, 2014 19:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bookis/9860879 to your computer and use it in GitHub Desktop.
Save bookis/9860879 to your computer and use it in GitHub Desktop.
# 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