473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 4706
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.

Similar topics

1
by: Adam Teasdale Hartshorne | last post by:
Using the code below I get a very stange result. I assume that I must be doing something wrong, but I can't figure out what it is. getTriangleEdges(t,t1e) ; for (int i = 0 ; i <...
2
by: Ben O'Steen | last post by:
Scenario: ========= Using PyGame in particular, I am trying to write an application that will run a scripted timeline of events, eg at 5.5 seconds, play xxx.mp3 and put the image of a ball on...
8
by: Squirrel | last post by:
Hi everyone, I've created a mail merge Word doc. (using Office XP) , the data source is an Access query. Functionality I'm attempting to set up is: User sets a boolean field to true for...
3
by: Andy Davis | last post by:
I have set up a mail merge document in Word 2003 which gets its data from my Access 2000 database. I want to set up a button on a form that: 1. runs the query to provide the dat for the merge...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
3
by: Bas | last post by:
Hi, stupid question, but would it be possible to somehow merge xrange (which is supposed to replace range in py3k) and slice? Both have very similar start, stop and step arguments and both are...
0
by: anoweb | last post by:
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...
3
by: Snedker | last post by:
Hi there, I really need some input of how to approach my little assignment. A customer wants to exclude all US IP-ranges from accessing part of his website. From...
7
by: guido | last post by:
Hi, I'm looking for a container class that can map whole ranges of keys to objects - something like std::map, but not only for individual values for the key, but for whole ranges. Example: I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.