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