Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

check if two integer ranges overlap

Tags:

python

range

I have two ranges, and I want to check if the ranges overlap at all. I've converted the ranges to lists and am checking if one value in the readRegion is in the refRegion, but this is super slow. Is there a more efficient way to do this?

readRegion=[*range(end,start,1)] #this list is always 600 in length
refRegion=[*range(600000,600500,1)] #this range will vary
p=0
for i in readRegion:
    if i in refRegion and p < 10000:
        regReads.append(filteredReads[n])
        p=10000    
    p+=1
like image 760
Sarah Avatar asked Oct 19 '25 15:10

Sarah


1 Answers

Two ranges overlap if the larger of their start values is less than the smaller of their stop values. This, of course, is if step is equal to 1.

def overlaps(x, y):
    return max(x.start,y.start) < min(x.stop,y.stop)

print(overlaps(range(10, 100), range(94, 200))
like image 177
Pranav Hosangadi Avatar answered Oct 22 '25 05:10

Pranav Hosangadi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!