user248237
user248237

Reputation:

interval overlap size

what is the most concise way in Python to compute interval overlap size?

overlap([a, b], [c, d]) should return 0 if intervals are identical, N when they overlap but not identical (where N is overlap) and None if they are not overlapping.

thanks.

edit: overlap is misleading i mean the size by which the intervals are non-overlapping. so 0 is they are identical.

Upvotes: 0

Views: 2990

Answers (1)

Junuxx
Junuxx

Reputation: 14251

It doesn't get much more concise than the accepted answer in the question linked to by sjr, but:

def overlap(a,b,c,d):
    r = 0 if a==c and b==d else min(b,d)-max(a,c)
    if r>=0: return r

would also return 0 for identical intervals and None for non-overlapping intervals as required.

Upvotes: 3

Related Questions