# [Tutor] overlapping tuples

Narasimharao Nelluri narasimha928 at gmail.com
Fri Feb 28 15:34:15 EST 2020

```Looks like misunderstood your question, even if independent over lap we
should return all the over lap tuples in this case we should return all the
tuples in a list.
because here two sets of over laps, (0,2) overlaps with (1,3) and there is
a overlap between (5,7) and (6,8).

tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)])

Thanks
Narasimha.

On Fri, Feb 28, 2020 at 12:06 PM Narasimharao Nelluri <
narasimha928 at gmail.com> wrote:

> Hi peter,
>
> Thanks a lot for your inputs, i will work your inputs and let you know.
> For the last question if there is independent over lap for ex: in below
> case python should return [(5,7),(6,8)]
>
> tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)])
>
> I tried different combinations as of i didn't get perfect solution.
>
> Thanks
> Narasimha.
>
>
> On Fri, Feb 28, 2020 at 11:40 AM Peter Otten <__peter__ at web.de> wrote:
>
>> Narasimharao Nelluri wrote:
>>
>> > Hi David ,
>> > Thanks for your feedback. Yes here i am solving Home work from one of my
>> > assignments.
>> >
>> > NNELLURI-M-L13P:google_class nnelluri\$ cat overlap.py
>> > #!/usr/bin/env python3
>> >
>> >
>> > def tuple_overlap(old_list,overlap = None):
>> >
>> >   old_list.sort()
>> >   if overlap is None:
>> >     overlap = []
>> >   for fir,sec in zip(old_list,old_list[1:]):
>> >
>> >     if fir[1] >= sec[0] and fir[1] <=sec[1]:=======>Here i am validating
>> > 2nd element in first variablae is in-between seconds variable if it is
>> > there is a overlap
>> >       overlap.append(fir)
>> >
>> >     if sec[0] >= fir[0] and sec[1] <= fir[1]:=====> Here i am checking
>> if
>> > first element in second variable is in-between second variable , there
>> is
>> > a oberlap
>> >       overlap.append(sec)
>> >
>> >   overlap = sorted(overlap)
>> >   return overlap
>> >
>> >
>> > overlaps = tuple_overlap( [(1,10),(15,20),(101,110)] )
>> > print( "Overlap 1 =", overlaps )
>> > assert overlaps == []
>> >
>> > overlaps = tuple_overlap( [(1,20),(15,20),(101,110)])
>> > print( "Overlap 2 =", overlaps )
>> > assert overlaps ==[(1, 20), (15, 20)]
>> >
>> > overlaps = tuple_overlap( [(1,10),(15,20),(1,10),(1,10),(101,110)])
>> > print( "Overlap 3 =", overlaps )
>> > assert overlaps == [(1, 10), (1, 10),(1,10)]
>> >
>> >
>> >
>> >
>> > Please let know if you know correct solution.
>>
>> I would have to work it out myself ;) but I think I can give you a few
>> hints:
>>
>> If you add some debugging information
>>
>> >>> tuple_overlap( [(1,10, "a"),(15,20, "b"),(1,10, "c"),(1,10, "d"),
>> (101,110, "e")])
>> [(1, 10, 'a'), (1, 10, 'c'), (1, 10, 'c'), (1, 10, 'd')]
>>
>> you can see that (1, 10, "c") occurs twice. Your code checks adjacent
>> tuples, and if three tuples overlap the tuple in the middle may be added
>> twice. In this situation you need to ensure that every tuple is added
>> only
>> once.
>>
>> But not only that:
>>
>> >>> tuple_overlap([(0, 10), (1, 3)])
>> [(1, 3)]
>>
>> Hm, one overlapping tuple? with what? with itself?
>>
>> If tuple A overlaps with tuple B, tuple B overlaps with A, i. e. instead
>> of
>> two independent checks you can use a single one, and add both tuples if
>> there is an overlap.
>>
>> That single check may be easier to implement if you ask yourself the
>> opposite question: when do the tuples not overlap?
>>
>> Also consider [(0, 10), (1, 3), (5, 20)].
>>
>> Clearly you need to decide what to do if t1 overlaps with t2 and t3, but
>> t2
>> not with t2.
>>
>> what
>> should be returned by
>>
>> tuple_overlap([(0, 2), (1, 3), (5, 7), (6, 8)])
>>
>> ?
>>
>> _______________________________________________
>> Tutor maillist  -  Tutor at python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor
>>
>
```