Hi All,

one of my colleague asked me, about how the overlap of two ranges can be
calculated easily.

>>> a = (24,27) # range1 (endpoint1,endpoint2)
>>> b = (10,227) # range1 (endpoint1,endpoint2)
>>> min( max(a), max(b) ) - max( min(a), min(b) )
3

When the result is positive, then the two ranges overlap together.
When the result is negative, then the two ranges doesn't overlap together
and the distance between them is the result multiplied with -1.
As we are using max() and min(), a=(24,27) means the same as a=(27,24).

I haven't seen this calculation method before, and I think it could be
interesting and usefull for coders.


Best regards,
János

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to