# [Tutor] overlapping tuples

Narasimharao Nelluri narasimha928 at gmail.com
Fri Feb 28 20:36:12 EST 2020

```Thanks Mats for your inputs.  overlap means if two numbers share across two
tuples For Ex  if we take two tuples (1,20) and (15,20)  there is a
over lap because 15,6,17,18,19,20  numbers are sharing between these
two sets. Sets may not solve the issue because sets will
eliminate duplicate elements in a given data structure. Here we are
finding all the over lapping tuples  in a given list of tuples.

Thanks
Narasimha.

On Fri, Feb 28, 2020 at 5:03 PM Mats Wichmann <mats at wichmann.us> wrote:

> On 2/28/20 1:06 PM, Narasimharao Nelluri 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.
>
> One of the problems here is we don't have the problem definition -
> unless I've missed something, I don't know what "overlap" means, this
> question not being precise enough.
>
> A tuple of (0, 2) has two values in it.
> A tuple of (1, 3) has two values in it.
> None of the values in those two tuples match, so one reading of
> "overlap" suggests that there's no overlap between those two. Python
> supports sets, and in set theory, an "overlap" is the intersection - the
> elements that appear in both.
>
> So you could do this:
>
> >>> a = set((0, 1, 2))
> >>> b = set((1, 2, 3))
> >>> print(b.intersection(a))
> set([1, 2])
> >>> print a & b
> set([1, 2])
>
>
> But this doesn't _appear_ to be what you're talking about at all.
> Instead, it _seems_ your tuples represent ranges of integers, with both
> endpoints included. Is that right? That's a different problem. But you
> can still use sets to work on this.
>
> Range gives you all the values, but range(x, y) is the values from x up
> to but not including y:
>
> >>> print(tuple(range(1, 4)))
> (1, 2, 3)
>
> so you want to add one to the end of the range.
>
> >>> a = set(range(0, 2+1))
> >>> b = set(range(1, 3+1))
> >>> c = set(range(5, 7+1))
> >>> print(a, b, c)
> (set([0, 1, 2]), set([1, 2, 3]), set([5, 6, 7]))
> >>> print(a & b)
> set([1, 2])
> >>> print(a & c)
> set([])
> >>> if a & b:
> ...     print("a and b overlap")
> ...
> a and b overlap
> >>> if a & c:
> ...     print("a and c overlap")
> ...
> >>>
>