CMU Coding Bootcamp
1from typing import List
2
3
4def inBothLists(L: List[int], M: List[int]) -> List[int]:
5 """Returns a list of unique elements that are present in both L and M."""
6 return list({x for x in L if x in M})
7
8
9def testInBothLists():
10 print("Testing inBothLists()...", end="")
11 assert inBothLists([1, 2, 3], [3, 2, 4]) == [2, 3]
12 assert inBothLists([3, 2, 1], [4, 1, 2, 1]) == [1, 2]
13 assert inBothLists([3, 2, 1, 2], [2, 2, 2]) == [2]
14 assert inBothLists([1, 2, 3], [4, 5, 6, 1]) == [1]
15 assert inBothLists([3, 2, 1, 2], [4]) == []
16
17 # Verify that the function is nonmutating:
18 L = [1, 2, 3]
19 M = [3, 2, 4]
20 inBothLists(L, M)
21 assert L == [1, 2, 3] and M == [3, 2, 4]
22 print("Passed!")
23
24
25def main():
26 testInBothLists()
27
28
29main()