is there a built in function to compute the overlap between two discrete intervals, e.g. the overlap between [10, 15] and [20, 38]? In that case the overlap is 0. If it’s [10, 20], [15, 20], the overlap is 5.
Advertisement
Answer
You can use max and min:
JavaScript
x
8
1
>>> def getOverlap(a, b):
2
return max(0, min(a[1], b[1]) - max(a[0], b[0]))
3
4
>>> getOverlap([10, 25], [20, 38])
5
5
6
>>> getOverlap([10, 15], [20, 38])
7
0
8