The problem is the following: I have a list intervals
which consists of tuples of the form (start, end
) [with start <= end
]. Each tuple represents an interval (of the real line). We assume that the intervals in intervals
are not overlapping each other. Given a new interval (s,e)
, I would like to write a Python function which checks if (s, e)
overlaps any of the intervals in intervals
. If (s, e)
has a non-empty intersection with at least one of the intervals in intervals
, the function should return the indices of these intervals in the list intervals
.
Say that the function is called find_intersections
. Then, given that intervals = [(1, 3.5), (5.5, 8.7), (10.2, 22.6), (22.7, 23.1)]
, expected outputs would be:
find_intersection(intervals, (3.2, 5.))
returnsarray([0])
find_intersection(intervals, (6.1, 7.3))
returnsarray([1])
find_intersection(intervals, (9.1, 10.2))
returnsNo intersection.
find_intersection(intervals, (5.8, 22.9))
returnsarray([1, 2, 3])
.
The code for find_intersection
I have written is:
import itertools
def find_intersection(intervals, new_interval):
_times = sorted(list(itertools.chain.from_iterable(intervals)))
ind = np.searchsorted(_times, np.asarray(new_interval))
parity = np.mod(ind, 2)
if (not np.any(parity)) and ind[1] == ind[0]:
print('No intersection.')
elif parity[0] == 1:
ub = ind[1] if parity[1] == 1 else ind[1] - 1
return np.arange((ind[0] - 1) / 2, (ub - 1) / 2 + 1)
elif parity[1] == 1:
lb = ind[0] if parity[0] == 1 else ind[0] + 1
return np.arange((lb - 1) / 2, (ind[1] - 1) / 2 + 1)
else:
lb = ind[0] if parity[0] == 1 else ind[0] + 1
ub = ind[1] if parity[1] == 1 else ind[1] - 1
return np.arange((lb - 1) / 2, (ub - 1) / 2 + 1)
I believe that the code does the job.
Is there an easier/smarter way to address this problem?
Edit: this question was cross-posted on stackoverflow.com.