merge two ranges
George Sakkis
george.sakkis at gmail.com
Wed Jul 18 23:17:08 CEST 2007
On Jul 17, 1:29 pm, anoweb <ano... at gmail.com> 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?
It's not really hard to come up with a quick and dirty solution;
however if this isn't a homework or a toy program, take a look at the
interval module (http://cheeseshop.python.org/pypi/interval/1.0.0) for
a more thought-out design.
HTH,
George
More information about the Python-list
mailing list