Skip to content

Instantly share code, notes, and snippets.

@jogonba2
Created March 22, 2015 17:46
Show Gist options
  • Save jogonba2/01c312cf7caf9c904aa1 to your computer and use it in GitHub Desktop.
Save jogonba2/01c312cf7caf9c904aa1 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class NodeList:
def __init__(self,data,skip):
self.data = data
self.skip = skip
def intersection(a,b):
i,j,res = 0,0,[]
while i<len(a) and j<len(b):
if a[i].data == b[j].data:
res.append(a[i].data)
i,j = i+1,j+1
else:
if a[i].data < b[j].data:
if a[i].skip!=0 and a[i].skip<b[j]:
while a[i].skip!=0 and a[i].skip<b[j]: i += a[i].skip
else: i+=1
else:
if b[j].skip!=0 and b[j].skip<a[i]:
while b[j].skip!=0 and b[j].skip<a[i]: j += b[j].skip
else: j+=1
return res
pass
if __name__ == "__main__":
v1,v2,v3,v4,v5,v6 = NodeList(2,2),NodeList(3,0),NodeList(5,2),NodeList(22,0),NodeList(38,2),NodeList(56,0)
w1,w2,w3,w4,w5,w6 = NodeList(4,2),NodeList(5,0),NodeList(7,2),NodeList(12,0),NodeList(15,2),NodeList(16,0)
l1,l2 = [v1,v2,v3,v4,v5,v6],[w1,w2,w3,w4,w5,w6]
print intersection(l1,l2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment