Determing whether two ranges overlap

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Feb 16 12:06:30 EST 2006


[Robin Haswell]

| I was wondering if you could give me a hand with something. 
| If I have two
| tuples that define a range, eg: (10, 20), (15, 30), I need to 
| determine
| whether the ranges overlap each other. The algo needs to catch:
| 
| (10, 20) (15, 25)
| (15, 25) (10, 20)
| (10, 25) (15, 20)
| and
| (15, 20) (10, 25)
| 
| I can think of lots of ways to do this but it's in a tight 
| loop so I need
| it to be as efficient as possible. Any help welcome :-)

Don't know about efficient, but:

x = 10, 20
y = 15, 25

(x[0] <= y[1]) and (x[1] >= y[0])

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list