Last active
March 23, 2016 23:07
-
-
Save st0le/f049fb5f2957d52064d8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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