isPermutation(L)
def isPermutation(L):
# return True if L is a permutation of [0,...,n-1]
# and False otherwise
return (set(L) == set(range(len(L))))
def testIsPermutation():
print("Testing isPermutation()...", end="")
assert(isPermutation([0,2,1,4,3]) == True)
assert(isPermutation([1,3,0,4,2]) == True)
assert(isPermutation([1,3,5,4,2]) == False)
assert(isPermutation([1,4,0,4,2]) == False)
print("Passed!")
testIsPermutation()
repeats(L)
def repeats(L):
# return a sorted list of the repeat elements in the list L
seen = set()
seenAgain = set()
for element in L:
if (element in seen):
seenAgain.add(element)
seen.add(element)
return sorted(seenAgain)
def testRepeats():
print("Testing repeats()...", end="")
assert(repeats([1,2,3,2,1]) == [1,2])
assert(repeats([1,2,3,2,2,4]) == [2])
assert(repeats(list(range(100))) == [ ])
assert(repeats(list(range(100))*5) == list(range(100)))
print("Passed!")
testRepeats()