Overlap in python

Marcus Wanner marcusw at cox.net
Thu Aug 6 15:24:33 CEST 2009


On 8/5/2009 10:56 AM, nn wrote:
> On Aug 5, 7:13 am, Marcus Wanner <marc... at cox.net> wrote:
>> On 8/4/2009 6:09 PM, MRAB wrote:
>>
>>>  >>> parts = [(5, 9, "a"), (7, 10, "b"), (3, 6, "c"), (15, 20, "d"),
>>> (18, 23, "e")]
>>>  >>> parts.sort()
>>>  >>> parts
>>> [(3, 6, 'c'), (5, 9, 'a'), (7, 10, 'b'), (15, 20, 'd'), (18, 23, 'e')]
>>>  >>> # Merge overlapping intervals.
>>>  >>> pos = 1
>>>  >>> while pos < len(parts):
>>>         # Merge the pair in parts[pos - 1 : pos + 1] if they overlap.
>>>         p, q = parts[pos - 1 : pos + 1]
>>>         if p[1] >= q[0]:
>>>                 parts[pos - 1 : pos + 1] = [(p[0], max(p[1], q[1]), p[2]
>>> + "." + q[2])]
>>>         else:
>>>                 # They don't overlap, so try the next pair.
>>>                 pos += 1
>>>  >>> parts
>>> [(3, 10, 'c.a.b'), (15, 23, 'd.e')]
>> That's the best solution I've seen so far. It even has input/output
>> formatted as close as is reasonably possible to the format specified.
>>
>> As we would say in googlecode, +1.
>>
>> Marcus
> 
> How does it compare to this one?
> 
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/1a1d2ed9d05d11d0/56684b795fc527cc#56684b795fc527cc
That is a different problem, and the solution is more complex.
I am not going to try to judge which is better.

Marcus

-- 
print ''.join([chr(((ord(z)+(ord("I'M/THE"[3])+sum(
     [ord(x)for x in 'CRYPTOR'])))%(4*ord('8')+ord(
     ' ')))) for z in ''.join(([(('\xca\x10\x03\t'+
     '\x01\xff\xe6\xbe\x0c\r\x06\x12\x17\xee\xbe'+
     '\x10\x03\x06\x12\r\x0c\xdf\xbe\x12\x11\x13'+
     '\xe8')[13*2-y]) for y in range(int(6.5*4)+1)]
     ))])




More information about the Python-list mailing list