-2

Hello I want to create a function that will take input as a list of lists and check if each list in that list is overlapping or entangling with each other. for e.g.

a = [[0.65, 0.71], [0.55, 0.65], [0.6, 0.66]]

here each of the lists in a are overlapping with each other. Since they are overlapping I want to return only the max and min from a. So, therefore, the function should return [[0.55, 0.71]] where 0.55 is the min in a and 0.71 is the max in a

case-2:

Say if there is another list b = [[0.65, 0.71], [0.55, 0.65], [0.1, 0.2]]. But now since only the first two combinations are overlapping i.e. [[0.65, 0.71], [0.55, 0.65], [0.6, 0.66]] now the function should return [[0.55, 0.71], [0.1, 0.2]]

mubas007
  • 129
  • 5
  • 3
    What have you tried so far? – jmkjaer Jun 19 '20 at 09:23
  • Does this answer your question? [Merging Overlapping Intervals](https://stackoverflow.com/questions/43600878/merging-overlapping-intervals) – Adam.Er8 Jun 19 '20 at 09:26
  • This seems to be a kind of competitive programming problem. Just posting the problem here and looking for solutions is not fair – Vivek Jun 19 '20 at 09:35

1 Answers1

1

It happens that I have written such a function recently:

def remove_intersections(a):
    for i, (s1, e1) in enumerate(a):
        for j, (s2, e2) in enumerate(a):
            if i == j:
                continue
            if s2 <= s1 <= e2:
                del a[i]
                if j < i:
                    del a[j]
                else:
                    del a[j - 1]
                a.append([s2, max(e1, e2)])
                remove_intersections(a)
                return
            if s2 <= e1 <= e2:
                del a[i]
                if j < i:
                    del a[j]
                else:
                    del a[j - 1]
                a.append([min(s1, s2), e2])
                remove_intersections(a)
                return
Thomas Schillaci
  • 2,348
  • 1
  • 7
  • 19