···11+from typing import List
22+33+44+def inBothLists(L: List[int], M: List[int]) -> List[int]:
55+ """Returns a list of unique elements that are present in both L and M."""
66+ return list({x for x in L if x in M})
77+88+99+def testInBothLists():
1010+ print("Testing inBothLists()...", end="")
1111+ assert inBothLists([1, 2, 3], [3, 2, 4]) == [2, 3]
1212+ assert inBothLists([3, 2, 1], [4, 1, 2, 1]) == [1, 2]
1313+ assert inBothLists([3, 2, 1, 2], [2, 2, 2]) == [2]
1414+ assert inBothLists([1, 2, 3], [4, 5, 6, 1]) == [1]
1515+ assert inBothLists([3, 2, 1, 2], [4]) == []
1616+1717+ # Verify that the function is nonmutating:
1818+ L = [1, 2, 3]
1919+ M = [3, 2, 4]
2020+ inBothLists(L, M)
2121+ assert L == [1, 2, 3] and M == [3, 2, 4]
2222+ print("Passed!")
2323+2424+2525+def main():
2626+ testInBothLists()
2727+2828+2929+main()
+28
python/oct9/level1/hasNoDuplicates.py
···11+from typing import List
22+33+44+def hasNoDuplicates(L: List[int]) -> bool:
55+ """Returns True if the list has no duplicates, False otherwise."""
66+ return len(set(L)) == len(L)
77+88+99+def testHasNoDuplicates():
1010+ print("Testing hasNoDuplicates()...", end="")
1111+ assert hasNoDuplicates([1, 2, 3, 2]) == False
1212+ assert hasNoDuplicates([1, 2, 3, 4]) == True
1313+ assert hasNoDuplicates([]) == True
1414+ assert hasNoDuplicates([42]) == True
1515+ assert hasNoDuplicates([42, 42]) == False
1616+1717+ # Verify that the function is nonmutating:
1818+ L = [1, 2, 3, 2]
1919+ hasNoDuplicates(L)
2020+ assert L == [1, 2, 3, 2]
2121+ print("Passed!")
2222+2323+2424+def main():
2525+ testHasNoDuplicates()
2626+2727+2828+main()
+27
python/oct9/level1/isPermutation.py
···11+from typing import List
22+33+44+def isPermutation(L: List[int]) -> bool:
55+ """Returns True if L is a permutation of the integers from 0 to len(L)-1."""
66+ return sorted(L) == list(range(len(L)))
77+88+99+def testIsPermutation():
1010+ print("Testing isPermutation()...", end="")
1111+ assert isPermutation([0, 2, 1, 4, 3]) == True
1212+ assert isPermutation([1, 3, 0, 4, 2]) == True
1313+ assert isPermutation([1, 3, 5, 4, 2]) == False
1414+ assert isPermutation([1, 4, 0, 4, 2]) == False
1515+1616+ # Verify that the function is nonmutating:
1717+ L = [0, 2, 1, 4, 3]
1818+ isPermutation(L)
1919+ assert L == [0, 2, 1, 4, 3]
2020+ print("Passed!")
2121+2222+2323+def main():
2424+ testIsPermutation()
2525+2626+2727+main()
+89
python/oct9/level1/movieAwards.py
···11+from typing import Set, Tuple, Dict
22+33+44+def movieAwards(oscarResults: Set[Tuple[str, str]]) -> Dict[str, int]:
55+ """Returns a dictionary mapping movie titles to the number of awards they won."""
66+ awards_count = {}
77+ for _, movie in oscarResults:
88+ awards_count[movie] = awards_count.get(movie, 0) + 1
99+ return awards_count
1010+1111+1212+def testMovieAwards():
1313+ print("Testing movieAwards()...", end="")
1414+ test = {
1515+ ("Best Picture", "The Shape of Water"),
1616+ ("Best Actor", "Darkest Hour"),
1717+ ("Best Actress", "Three Billboards Outside Ebbing, Missouri"),
1818+ ("Best Director", "The Shape of Water"),
1919+ }
2020+ result = {
2121+ "Darkest Hour": 1,
2222+ "Three Billboards Outside Ebbing, Missouri": 1,
2323+ "The Shape of Water": 2,
2424+ }
2525+ assert movieAwards(test) == result
2626+2727+ test = {
2828+ ("Best Picture", "Moonlight"),
2929+ ("Best Director", "La La Land"),
3030+ ("Best Actor", "Manchester by the Sea"),
3131+ ("Best Actress", "La La Land"),
3232+ }
3333+ result = {"Moonlight": 1, "La La Land": 2, "Manchester by the Sea": 1}
3434+ assert movieAwards(test) == result
3535+3636+ test = {
3737+ ("Best Picture", "12 Years a Slave"),
3838+ ("Best Director", "Gravity"),
3939+ ("Best Actor", "Dallas Buyers Club"),
4040+ ("Best Actress", "Blue Jasmine"),
4141+ }
4242+ result = {
4343+ "12 Years a Slave": 1,
4444+ "Gravity": 1,
4545+ "Dallas Buyers Club": 1,
4646+ "Blue Jasmine": 1,
4747+ }
4848+ assert movieAwards(test) == result
4949+5050+ test = {
5151+ ("Best Picture", "The King's Speech"),
5252+ ("Best Director", "The King's Speech"),
5353+ ("Best Actor", "The King's Speech"),
5454+ }
5555+ result = {"The King's Speech": 3}
5656+ assert movieAwards(test) == result
5757+5858+ test = {
5959+ ("Best Picture", "Spotlight"),
6060+ ("Best Director", "The Revenant"),
6161+ ("Best Actor", "The Revenant"),
6262+ ("Best Actress", "Room"),
6363+ ("Best Supporting Actor", "Bridge of Spies"),
6464+ ("Best Supporting Actress", "The Danish Girl"),
6565+ ("Best Original Screenplay", "Spotlight"),
6666+ ("Best Adapted Screenplay", "The Big Short"),
6767+ ("Best Production Design", "Mad Max: Fury Road"),
6868+ ("Best Cinematography", "The Revenant"),
6969+ }
7070+ result = {
7171+ "Spotlight": 2,
7272+ "The Revenant": 3,
7373+ "Room": 1,
7474+ "Bridge of Spies": 1,
7575+ "The Danish Girl": 1,
7676+ "The Big Short": 1,
7777+ "Mad Max: Fury Road": 1,
7878+ }
7979+ assert movieAwards(test) == result
8080+8181+ assert movieAwards(set()) == dict()
8282+ print("Passed!")
8383+8484+8585+def main():
8686+ testMovieAwards()
8787+8888+8989+main()
+29
python/oct9/level1/repeats.py
···11+from typing import List
22+33+44+def repeats(L: List[int]) -> List[int]:
55+ """Returns a sorted list of unique elements that are present in L more than once."""
66+ return list(sorted({x for x in L if L.count(x) > 1}))
77+88+99+def testRepeats():
1010+ print("Testing repeats()...", end="")
1111+ assert repeats([1, 2, 3, 2, 1]) == [1, 2]
1212+ assert repeats([1, 2, 3, 2, 2, 4]) == [2]
1313+ assert repeats([1, 5, 3, 5, 2, 3, 2, 1]) == [1, 2, 3, 5]
1414+ assert repeats([7, 9, 1, 3, 7, 1]) == [1, 7]
1515+ assert repeats(list(range(100))) == []
1616+ assert repeats(list(range(100)) * 5) == list(range(100))
1717+1818+ # Verify that the function is nonmutating:
1919+ L = [1, 2, 3, 2, 1]
2020+ repeats(L)
2121+ assert L == [1, 2, 3, 2, 1]
2222+ print("Passed!")
2323+2424+2525+def main():
2626+ testRepeats()
2727+2828+2929+main()