# [Tutor] overlapping tuples

Mats Wichmann mats at wichmann.us
Fri Feb 28 20:03:13 EST 2020

```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.
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")
...
>>>