Skip to content

Instantly share code, notes, and snippets.

@st0le
Last active March 23, 2016 23:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save st0le/f049fb5f2957d52064d8 to your computer and use it in GitHub Desktop.
Save st0le/f049fb5f2957d52064d8 to your computer and use it in GitHub Desktop.
from random import randint
def random_array(n):
f = lambda i : randint(10, 100)
return map(f, range(n))
A = sorted(random_array(10))
B = sorted(random_array(10))
def intersection(A, B):
common = []
i = j = 0
while i < len(A) and j < len(B):
if A[i] < B[j]:
i += 1
elif A[i] > B[j]:
j += 1
else: #common
common_element = A[i]
common.append(common_element)
while i < len(A) and A[i] == common_element:
i += 1
while j < len(B) and B[j] == common_element:
j += 1
return common
print A
print B
print intersection(A, B)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment