Saturday, April 7, 2018

Merging overlapping lists

You are given two lists of intervals, A and B.
In A, the intervals are sorted by their starting points. None of the intervals within A overlap.
Likewise, in B, the intervals are sorted by their starting points. None of the intervals within B overlap.
Return the intervals that overlap between the two lists.
Example:
A: {[0,4], [7,12]}
B: {[1,3], [5,8], [9,11]}
Return:
{[1,3], [7,8], [9,11]}
How would you do this?

Here is a solution
#!/usr/bin/env python
def mergeLists(L1 , L2):
len1 = len(L1)
len2 = len(L2)
print len1
print len2
index1 = 0
index2 = 0
pickx = 0
picky = 0
mergeList = []
while (L1[index1][1] > L2[index2][0]):
if (L1[index1][0] < L2[index2][0]):
pickx = L2[index2][0]
else:
pickx = L1[index1][0]
if (L1[index1][1] < L2[index2][1]):
picky = L1[index1][1]
else:
picky = L2[index2][1]
print "%d , %d" %(pickx,picky)
mergeList.append([pickx,picky])
index2 = index2+1
if (index2 == len2):
print "break"
break
if (L2[index2][0] < L1[index1][1]):
continue
else:
index1 = index1 + 1
if (index1 == len1-1):
exit
return mergeList
L1 = [[0,4], [7,12]]
L2 = [[1,3], [5,8], [9,11]]
print mergeLists(L1, L2)
view raw mergeLists.py hosted with ❤ by GitHub

No comments:

Post a Comment