473,804 Members | 2,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4723
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.c omwrote:
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
2679
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 < (int)t1e.size() ; i++) { printf("T1 Edges Index:- %i \n", t1e) ; }
2
1727
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 screen, at 7.8 seconds move the ball up and down. At this point, I hear you say 'Oh, like Flash'. Yes, well... Like Flash, but I don't want to take this app in the same direction as Macromedia took Flash, nor do I (ever) want the two to be
8
9528
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 each person for whom a mail merge letter is desired. The query reads address info from the table for each record where is true.
3
5593
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 document in Word; 2. opens the document and runs the merge process for the new data. I have managed to write the code to perform step 1 ok, but I'm having trouble with step 2. It opens the word document fine but does not perform the mail merge of...
67
7721
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 4/30/06 I am looking for suggestions on how to find the date ranges where there were no transactions.
3
2993
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 lightweight objects to indicate a range. But you can't do a and 'for i in slice(10,20)'. The only difference is see is some behavior with infinities (e.g. object gives a slice(3,maxint) inside _getitem_ , but I think this should not be a large...
0
926
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 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)
3
1532
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 http://www.ipaddresslocation.org/ I've collected about 16,000 ranges in the format 208.202.120.0 208.203.111.255 208.203.114.0 208.203.244.127
7
10279
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 want to be able to tell the container to return object a for every given key between 0 and 10, object c for every key between 11 and 500000 and object c for every key between 500001 and 599999, without having to
0
10568
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10311
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10074
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7613
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5516
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4292
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3813
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.