Srinivas Iyyer wrote:
<snip>
> Coordinates of a variable G1:
> a1 : 3-8
> b2 : 10-25
> c3 : 7-18
> d4 : 10-13
> Now, I have variables G1....Gn.  Each variable may
> have individual pieces a1(n),b2(n),c3(n).....z(n).
> 
> my kind of solution, which is very crappy (i assume):
> 
> Sort the list:
> [3,8]                 [3,8]
> [10,25]     sort      [7,18]
> [7,18]     -------->  [10,13]
> [10,13]               [10,25]
> 
<snip>
> The question to the forum is, I am using range()
> function to solve this. Since the numbers in the
> example are small, it worked. in the real world
> example , it did not work, because computer slowed
> down and eventually crashed.  The range(81393417) is
> really big. 

I'm not sure I fully understand the question - do you NEED to loop over 
the possible locations, or do you only need to determine the extremes of 
the 4 ranges? If you need a loop, xrange is probably the way to go, as 
Luke mentions.
If you just need the extremes, you don't need a range(), because it can 
be done using zip:

 >>> a,b,c,d = [3,8], [10,25], [7,18], [10,13]
 >>> candidates = zip(a,b,c,d) # 'merges' the elements of the lists
 >>> print candidates # first item contains potential minimums
[(3, 10, 7, 10), (8, 25, 18, 13)] # second item potential maxes
 >>> maxrange = [ min(candidates[0]), max(candidates[1]) ]
 >>> print maxrange
[3, 25]

Now if you have a non-continuous range, this will still give you the 
overlapping area. E.g. if you'd drop c3, it would not affect the result. 
If that's a problem, you could loop over the lists directly:

def getMaxRange(locs):
     locs.sort() # sorts 'naturally'
     #print "sorted locs:", locs
     maxrange = locs[0][:] # guaranteed to start with min
     for loc in locs[1:]: # loop over rest of locs
         if loc[0] <= maxrange[1]:
             maxrange[1] = loc[1]
         else:
             print "   discontinuity found for", loc
             return None
     return maxrange

It's similar to your solution, but without the range - they're superfluous.

Yours,

Andrei

_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to