-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(arrays): intersection of two arrays
- Loading branch information
1 parent
fcc5584
commit 9cb1302
Showing
10 changed files
with
164 additions
and
98 deletions.
There are no files selected for viewing
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
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
21 changes: 12 additions & 9 deletions
21
...section_of_two_arrays/intersection_one.py → ...s/arrays/intersection/intersection_one.py
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from typing import List, TypeVar | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def intersect(list_one: List[T], list_two: List[T], include_duplicates: bool = True) -> List[T]: | ||
""" | ||
Given two arrays, find their intersection. This assumes that the lists are not sorted. First sorting takes place | ||
on both lists which will incur a cost of O(nlog(n)) + O(mlog(m)) depending on the algorithm used. Time will also | ||
include iterating over both lists which will be O(n+m) where n is the size of list one and m is the size of list two | ||
Next 2 pointers are set to be used to keep track of which element we are on in the iteration of both lists. | ||
As long as both pointers are less than their respective lists, we compare each element to each other. If the element | ||
from the first collection/list is less than an element from the second collection, we move the first pointer a step | ||
forward, otherwise we move the second pointer. | ||
In the event, they are equal to each other, it means there is an intersection. At this point we check if the | ||
include_duplicates argument is true and include the first element. If it is set to false, we check if the preceding | ||
element from the first list is not equal to the current element and include it, this removes duplicates. | ||
Afterwards, we move the pointers forward. | ||
Space Complexity results in O(n+m) were n is the size of list one and m is the size of list two. This is because | ||
the elements are stored in a resulting list which is returned as the result of this function. | ||
""" | ||
sorted_list_one, sorted_list_two, result = sorted(list_one), sorted(list_two), [] | ||
pointer_one, pointer_two = 0, 0 | ||
|
||
while pointer_one < len(sorted_list_one) and pointer_two < len(sorted_list_two): | ||
first_element = sorted_list_one[pointer_one] | ||
second_element = sorted_list_two[pointer_two] | ||
|
||
if first_element < second_element: | ||
pointer_one += 1 | ||
elif second_element < first_element: | ||
pointer_two += 1 | ||
else: | ||
if include_duplicates: | ||
result.append(first_element) | ||
else: | ||
if pointer_one == 0 or first_element != sorted_list_one[pointer_one - 1]: | ||
result.append(first_element) | ||
pointer_one += 1 | ||
pointer_two += 1 | ||
return result |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import unittest | ||
|
||
from algorithms.arrays.intersection.intersection_two import intersect | ||
|
||
|
||
class IntersectionTwoTestCase(unittest.TestCase): | ||
def test_1_2_2_1_and_2_2_returns_2_2(self): | ||
"""Should return [2, 2] for nums1=[1,2,2,1] and nums2=[2,2]""" | ||
nums1 = [1, 2, 2, 1] | ||
nums2 = [2, 2] | ||
expected = [2, 2] | ||
self.assertEqual(intersect(nums1, nums2), expected) | ||
|
||
def test_4_9_5_and_9_4_9_8_4_returns_4_9(self): | ||
"""Should return [4, 9] for nums1=[4,9,5] and nums2=[9,4,9,8,4]""" | ||
nums1 = [4, 9, 5] | ||
nums2 = [9, 4, 9, 8, 4] | ||
expected = [4, 9] | ||
self.assertEqual(intersect(nums1, nums2), expected) | ||
|
||
def test_2(self): | ||
"""should return [3,3,7] for a = [2,3,3,5,7,11], b = [3,3,7,15,31]""" | ||
a = [2, 3, 3, 5, 7, 11] | ||
b = [3, 3, 7, 15, 31] | ||
expected = [3, 3, 7] | ||
actual = intersect(a, b) | ||
self.assertEqual(expected, actual) | ||
|
||
def test_2_remove_duplicates(self): | ||
"""should return [3,7] for a = [2,3,3,5,7,11], b = [3,3,7,15,31] without including duplicates""" | ||
a = [2, 3, 3, 5, 7, 11] | ||
b = [3, 3, 7, 15, 31] | ||
expected = [3, 7] | ||
actual = intersect(a, b, include_duplicates=False) | ||
self.assertEqual(expected, actual) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
40 changes: 0 additions & 40 deletions
40
datastructures/arrays/intersection_of_two_arrays/README.md
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
datastructures/arrays/intersection_of_two_arrays/intersection_two.py
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.