CMU Coding Bootcamp
at main 658 B view raw
1from typing import Sequence 2 3 4def isRotation(L: Sequence, M: Sequence) -> bool: 5 """Check if M is a rotation of L.""" 6 L = list(L) 7 M = list(M) 8 if not len(L) == len(M): 9 return False 10 if not L and not M: 11 return True 12 for i in range(len(L)): 13 if L[i:] + L[:i] == M: 14 return True 15 return False 16 17 18print("Testing isRotation()...", end="") 19assert isRotation([2, 3, 4, 5, 6], [4, 5, 6, 2, 3]) == True 20assert isRotation([2, 3, 4, 5, 6], [2, 3, 4, 5, 6]) == True 21assert isRotation([2, 3, 4, 5, 6], [2, 4, 3, 5, 6]) == False 22assert isRotation([1], []) == False 23assert isRotation([], []) == True 24print("Passed!")