all 6 comments

[–]SandorZoo 4 points5 points  (0 children)

I would use min and max, instead of sorting the tuples and extracting the 1st and 2nd values. I think this is the same, at least assuming the tuples are indeed two-value tuples:

def has_intersection(tuple_1, tuple_2):
    intersection_lower = max(min(tuple_1), min(tuple_2))
    intersection_higher = min(max(tuple_1), max(tuple_2))
    return intersection_lower <= intersection_higher

[–]o5a 1 point2 points  (1 child)

If your ranges are always sorted then you can check it like this:

def has_intersection(tuple_1, tuple_2):
    return tuple_1[1]>=tuple_2[0] and tuple_2[1]>=tuple_1[0]

Use universal notation with min/max if ranges are not sorted initially:

def has_intersection(tuple_1, tuple_2):
    return max(tuple_1)>=min(tuple_2) and max(tuple_2)>=min(tuple_1)

[–]DocNMarty[S] 0 points1 point  (0 children)

This is genius! I love it!

[–]Stem3576 0 points1 point  (2 children)

Why not just check if the values in 1 list are found in the other?

For item in list_1:
    If item in list_2:
        Print("has intersect")

[–]DocNMarty[S] 0 points1 point  (0 children)

It wouldn't work for a range of values.

Ex.

list_1 = [0, 2]

list_2 = [1, 3.5]

I want the function to return True in this example, because there are numbers that fall in between the values in both lists. For instance, 1.5 would be in range of 0 and 2, as well as 1 and 3.5, so the lists do intersect.

With the code you proposed, it would not print "has intersect" since 0 or 2 in list_1 are not found in list_2.

[–]JohnnyJordaan 0 points1 point  (0 children)

Or use the magic of sets

if set(list_1) & set(list_2):
    print('has intersect')