Skip to content

Instantly share code, notes, and snippets.

@mattiasflodin
Created June 19, 2018 11:04
Show Gist options
  • Save mattiasflodin/b2aa4ba57189a4e6113c5ca2f634daf8 to your computer and use it in GitHub Desktop.
Save mattiasflodin/b2aa4ba57189a4e6113c5ca2f634daf8 to your computer and use it in GitHub Desktop.
Check if two ranges intersect
# Check overlap of range [a1, a2) and range [b1, b2), where None indicates infinity.
def ranges_intersect(a, b):
# Make sure that a starts before or at b.
if b[0] is None or (a[0] is not None and b[0] < a[0]):
a, b = b, a
# If b starts before a ends, then there is an intersection.
return b[0] is None or a[1] is None or b[0] < a[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment