Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created July 17, 2013 03:53
Show Gist options
  • Save TGOlson/6017561 to your computer and use it in GitHub Desktop.
Save TGOlson/6017561 to your computer and use it in GitHub Desktop.
Question 4: Intersection
#Question 4: Intersection
def intersection(range1, range2)
# range1 is range1.min, range2.min - add in 'maxes' if range1 encompasses range2
r1_only = [range1[0], range2[0]]
r1_only << [range2[1], range1[1]] if range1[1] > range2[1]
# both occurs at range1.max, range2.min if overlap - both = range2 if encompass - nil if no overlap
both = [range2[0], range1[1]] if range1[1] < range2[1]
both = range2 if range1[1] > range2[1]
# range2 is range1.max, range2.max - or nil if no distinct range2
r2_only = [range1[1], range2[1]] if range2[1] > range1[1]
all = [r1_only, both, r2_only] # return all
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment