I have a fairly basic question here: I want to find if two lines on a 1-D plane intersect. I know of two simple ways to solve this, but I wanted to know if Python has a more elegant way to solve this.
x = [1, 10] # 1 = begin, 10 = end y = [15, 20] z = [5, 12] #Method 1: Works. Is quick. Lots of typing. def is_intersect_1(a, b): bool_check = False if a <= b <= a or \ a <= b <= a or \ b <= a <= b or \ b <= a <= b: bool_check = True return bool_check is_intersect_1(x,y) # False is_intersect_1(x,z) # True #Method 2: Quicker to write. Simpler to read. Uses more memory and is slower. def is_intersect_2(a, b): bool_check = False if set(range(a, a+1)).intersection(set(range(b, b)): bool_check = True return bool_check is_intersect_2(x,y) # False is_intersect_2(x,z) # True