merge two ranges

A

anoweb

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?
 
M

Marc 'BlackJack' Rintsch

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
 
G

George Sakkis

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top