Skip to content

Instantly share code, notes, and snippets.

@bswinnerton
Last active December 20, 2015 10:19
Show Gist options
  • Save bswinnerton/6114740 to your computer and use it in GitHub Desktop.
Save bswinnerton/6114740 to your computer and use it in GitHub Desktop.
# You’re given two integer ranges (x1, x2) and (y1, y2). For example, the ranges (1, 10) and (5, 20). Sum the
# numbers that exist in both ranges. In our example, we would add up 5, 6, 7, 8, 9 and 10 (for a total of 45) since
# 5, 6, 7, 8, 9 and 10 exist in both ranges.
#The function should look like this (in Ruby):
def sum_of_intersection(x1, x2, y1, y2)
intersection = (x1..x2).to_a & (y1..y2).to_a
intersection.inject { |sum, i| sum + i }
end
puts sum_of_intersection(1,10,5,20)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment