[Tutor] overlapping tuples

Narasimharao Nelluri narasimha928 at gmail.com
Wed Feb 26 11:44:44 EST 2020


Hi

I got stuck at below problem for couple of days, can you guys help tp solve
this problem.
i need find overlapping tuples for various list of tuples, my current
solution doest solve all the cases. I am failing at last input list3 .

please check below

#!/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]:
      overlap.append(fir)

    if sec[0] >= fir[0] and sec[1] <= fir[1]:
      overlap.append(sec)

  overlap = sorted(overlap)
  print(overlap)


list1 = [(1,10),(15,20),(101,110)]
tuple_overlap(list1)
list2 = [(1,20),(15,20),(101,110)]
tuple_overlap(list2)

list3 = [(1,10),(15,20),(1,10),(1,10),(101,110)]
tuple_overlap(list3)

NNELLURI-M-L13P:google_class nnelluri$ ./overlap.py
[]
[(1, 20), (15, 20)]
[(1, 10), (1, 10), (1, 10), (1, 10)]
NNELLURI-M-L13P:google_class nnelluri$


More information about the Tutor mailing list