Skip to content

Instantly share code, notes, and snippets.

@seiflotfy
Created September 27, 2016 10:18
Show Gist options
  • Save seiflotfy/b0967c763d2798e1c56a3d754a0ecb60 to your computer and use it in GitHub Desktop.
Save seiflotfy/b0967c763d2798e1c56a3d754a0ecb60 to your computer and use it in GitHub Desktop.
#Make sure your intervals are sorted
A = [(1,2), (5,6), (6,8), (8,10)]
B = [(2,3), (8,9), (11, 18)]
print "A =", A
print "B =", B
def intersects(a, b):
start = max(a[0], b[0])
end = min(a[1], b[1])
return end - start >= 0
i = 0
j = 0
res = []
while i < len(A) and j < len(B):
a = A[i]
b = B[j]
if not intersects(a, b):
if a[0] < b[0]:
i+=1
else:
j+=1
else:
res.append((i, j))
if a[0] < b[0] or a[1] < b[1]:
i+=1
else:
j+=1
print "------"
print "R =", res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment