472,958 Members | 1,905 Online

# merge two ranges

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?

Jul 17 '07 #1
2 4684
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
Jul 17 '07 #2
On Jul 17, 1:29 pm, anoweb <ano...@gmail.comwrote:
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

Jul 18 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.