Last active
November 7, 2022 15:29
-
-
Save omokehinde/4017877206b1d2b9969d173ec5c70844 to your computer and use it in GitHub Desktop.
function that takes 2 lists as arguments and returns True if the list are rotated version of each other or False otherwise.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A function that takes 2 lists as arguments and returns True if the list are rotated version of each other or False otherwise. | |
def rotated(list1, list2): | |
if len(list1) != len(list2) or list1[0] == list2[0]: | |
return False | |
first_elem_list1 = list1[0] | |
if first_elem_list1 in list2: | |
index_first_elem_list2 = list2.index(first_elem_list1) | |
else: return False | |
for i in range(len(list2)-1): | |
index_list2 = (index_first_elem_list2+i) % len(list1) | |
if list1[i] != list2[index_list2]: | |
return False | |
return True | |
print(rotated([1,2,3,4,5,6,7], [6,7,1,2,3,4,5])) | |
print(rotated([1,2,3,4,5,6,7], [6,7,1,2,3,5,4])) | |
print(rotated([1,2,3,4,5,6,7], [6,7,1,2,3,4,5])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment