CMU Coding Bootcamp
1from typing import List
2
3
4def 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
9def 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
24def main():
25 testHasNoDuplicates()
26
27
28main()