+29
python/oct9/level1/bothLists.py
+29
python/oct9/level1/bothLists.py
···
1
+
from typing import List
2
+
3
+
4
+
def 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
+
9
+
def 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
+
25
+
def main():
26
+
testInBothLists()
27
+
28
+
29
+
main()
+28
python/oct9/level1/hasNoDuplicates.py
+28
python/oct9/level1/hasNoDuplicates.py
···
1
+
from typing import List
2
+
3
+
4
+
def hasNoDuplicates(L: List[int]) -> bool:
5
+
"""Returns True if the list has no duplicates, False otherwise."""
6
+
return len(set(L)) == len(L)
7
+
8
+
9
+
def testHasNoDuplicates():
10
+
print("Testing hasNoDuplicates()...", end="")
11
+
assert hasNoDuplicates([1, 2, 3, 2]) == False
12
+
assert hasNoDuplicates([1, 2, 3, 4]) == True
13
+
assert hasNoDuplicates([]) == True
14
+
assert hasNoDuplicates([42]) == True
15
+
assert hasNoDuplicates([42, 42]) == False
16
+
17
+
# Verify that the function is nonmutating:
18
+
L = [1, 2, 3, 2]
19
+
hasNoDuplicates(L)
20
+
assert L == [1, 2, 3, 2]
21
+
print("Passed!")
22
+
23
+
24
+
def main():
25
+
testHasNoDuplicates()
26
+
27
+
28
+
main()
+27
python/oct9/level1/isPermutation.py
+27
python/oct9/level1/isPermutation.py
···
1
+
from typing import List
2
+
3
+
4
+
def isPermutation(L: List[int]) -> bool:
5
+
"""Returns True if L is a permutation of the integers from 0 to len(L)-1."""
6
+
return sorted(L) == list(range(len(L)))
7
+
8
+
9
+
def testIsPermutation():
10
+
print("Testing isPermutation()...", end="")
11
+
assert isPermutation([0, 2, 1, 4, 3]) == True
12
+
assert isPermutation([1, 3, 0, 4, 2]) == True
13
+
assert isPermutation([1, 3, 5, 4, 2]) == False
14
+
assert isPermutation([1, 4, 0, 4, 2]) == False
15
+
16
+
# Verify that the function is nonmutating:
17
+
L = [0, 2, 1, 4, 3]
18
+
isPermutation(L)
19
+
assert L == [0, 2, 1, 4, 3]
20
+
print("Passed!")
21
+
22
+
23
+
def main():
24
+
testIsPermutation()
25
+
26
+
27
+
main()
+89
python/oct9/level1/movieAwards.py
+89
python/oct9/level1/movieAwards.py
···
1
+
from typing import Set, Tuple, Dict
2
+
3
+
4
+
def movieAwards(oscarResults: Set[Tuple[str, str]]) -> Dict[str, int]:
5
+
"""Returns a dictionary mapping movie titles to the number of awards they won."""
6
+
awards_count = {}
7
+
for _, movie in oscarResults:
8
+
awards_count[movie] = awards_count.get(movie, 0) + 1
9
+
return awards_count
10
+
11
+
12
+
def testMovieAwards():
13
+
print("Testing movieAwards()...", end="")
14
+
test = {
15
+
("Best Picture", "The Shape of Water"),
16
+
("Best Actor", "Darkest Hour"),
17
+
("Best Actress", "Three Billboards Outside Ebbing, Missouri"),
18
+
("Best Director", "The Shape of Water"),
19
+
}
20
+
result = {
21
+
"Darkest Hour": 1,
22
+
"Three Billboards Outside Ebbing, Missouri": 1,
23
+
"The Shape of Water": 2,
24
+
}
25
+
assert movieAwards(test) == result
26
+
27
+
test = {
28
+
("Best Picture", "Moonlight"),
29
+
("Best Director", "La La Land"),
30
+
("Best Actor", "Manchester by the Sea"),
31
+
("Best Actress", "La La Land"),
32
+
}
33
+
result = {"Moonlight": 1, "La La Land": 2, "Manchester by the Sea": 1}
34
+
assert movieAwards(test) == result
35
+
36
+
test = {
37
+
("Best Picture", "12 Years a Slave"),
38
+
("Best Director", "Gravity"),
39
+
("Best Actor", "Dallas Buyers Club"),
40
+
("Best Actress", "Blue Jasmine"),
41
+
}
42
+
result = {
43
+
"12 Years a Slave": 1,
44
+
"Gravity": 1,
45
+
"Dallas Buyers Club": 1,
46
+
"Blue Jasmine": 1,
47
+
}
48
+
assert movieAwards(test) == result
49
+
50
+
test = {
51
+
("Best Picture", "The King's Speech"),
52
+
("Best Director", "The King's Speech"),
53
+
("Best Actor", "The King's Speech"),
54
+
}
55
+
result = {"The King's Speech": 3}
56
+
assert movieAwards(test) == result
57
+
58
+
test = {
59
+
("Best Picture", "Spotlight"),
60
+
("Best Director", "The Revenant"),
61
+
("Best Actor", "The Revenant"),
62
+
("Best Actress", "Room"),
63
+
("Best Supporting Actor", "Bridge of Spies"),
64
+
("Best Supporting Actress", "The Danish Girl"),
65
+
("Best Original Screenplay", "Spotlight"),
66
+
("Best Adapted Screenplay", "The Big Short"),
67
+
("Best Production Design", "Mad Max: Fury Road"),
68
+
("Best Cinematography", "The Revenant"),
69
+
}
70
+
result = {
71
+
"Spotlight": 2,
72
+
"The Revenant": 3,
73
+
"Room": 1,
74
+
"Bridge of Spies": 1,
75
+
"The Danish Girl": 1,
76
+
"The Big Short": 1,
77
+
"Mad Max: Fury Road": 1,
78
+
}
79
+
assert movieAwards(test) == result
80
+
81
+
assert movieAwards(set()) == dict()
82
+
print("Passed!")
83
+
84
+
85
+
def main():
86
+
testMovieAwards()
87
+
88
+
89
+
main()
+29
python/oct9/level1/repeats.py
+29
python/oct9/level1/repeats.py
···
1
+
from typing import List
2
+
3
+
4
+
def repeats(L: List[int]) -> List[int]:
5
+
"""Returns a sorted list of unique elements that are present in L more than once."""
6
+
return list(sorted({x for x in L if L.count(x) > 1}))
7
+
8
+
9
+
def testRepeats():
10
+
print("Testing repeats()...", end="")
11
+
assert repeats([1, 2, 3, 2, 1]) == [1, 2]
12
+
assert repeats([1, 2, 3, 2, 2, 4]) == [2]
13
+
assert repeats([1, 5, 3, 5, 2, 3, 2, 1]) == [1, 2, 3, 5]
14
+
assert repeats([7, 9, 1, 3, 7, 1]) == [1, 7]
15
+
assert repeats(list(range(100))) == []
16
+
assert repeats(list(range(100)) * 5) == list(range(100))
17
+
18
+
# Verify that the function is nonmutating:
19
+
L = [1, 2, 3, 2, 1]
20
+
repeats(L)
21
+
assert L == [1, 2, 3, 2, 1]
22
+
print("Passed!")
23
+
24
+
25
+
def main():
26
+
testRepeats()
27
+
28
+
29
+
main()