merge two ranges

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Tue Jul 17 19:44:15 CEST 2007


On Tue, 17 Jul 2007 17:29:41 +0000, anoweb wrote:

> I have two ranges of numbers and I need to determine if they overlap
> or adjacent and if so return a new range containing the values.  The
> values are low and high for each pair, such that the first value of
> the tuple is always less than or equal to the second value in the
> tuple.
> 
> for example:
> a = (0, 5)
> b = (5, 10)
> 
> print getAdjacent(a, b)  # output: (0, 10)
> print getAdjacent(b, a)  # output: (0, 10)
> print getOverlap(a, b) # output: None
> print getOverlap(b, a) # output: None
> 
> a = (0, 7)
> b = (3, 10)
> print getAdjacent(a, b) # output: None
> print getAdjacent(b, a) # output: None
> print getOverlap(a, b)  # output: (0, 10)
> print getOverlap(b, a)  # output: (0, 10)
> 
> a = (0, 90)
> b = (5, 10)
> print getAdjacent(a, b) # output: None
> print getAdjacent(b, a) # output: None
> print getOverlap(a, b)  # output: (0, 90)
> print getOverlap(b, a)  # output: (0, 90)
> 
> a = (0, 5)
> b = (12, 20)
> print getAdjacent(a, b) # output: None
> print getAdjacent(b, a) # output: None
> print getOverlap(a, b)  # output:None
> print getOverlap(b, a)  # output: None
> 
> any easy way of doing this?

Yes.  That's quite an easy homework you really should do yourself.  ;-)

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list