Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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