Created
June 19, 2018 11:04
-
-
Save mattiasflodin/b2aa4ba57189a4e6113c5ca2f634daf8 to your computer and use it in GitHub Desktop.
Check if two ranges intersect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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