Created
May 2, 2021 18:11
-
-
Save jcrubino/cdfacf031797c788a0443e73a6f7fad3 to your computer and use it in GitHub Desktop.
rotation of arrays
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
def rotate_array_right(array, n): | |
length = len(array) | |
remainder = n % length | |
if remainder == 0: | |
return array | |
temp_array = array[0:remainder] | |
return array[remainder:] + temp_array | |
def reverse_array(array): | |
start = 0 | |
end = len(array)-1 | |
reversed_array = [] | |
while end > -1: | |
reversed_array.append(array[end]) | |
end -= 1 | |
return reversed_array | |
def rotate_array_left(array, n): | |
array = reverse_array(array) | |
return reverse_array(rotate_array_right(array, n)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment