Created
September 3, 2018 16:41
-
-
Save kupp1/0c8e4b734f57577d5cbf5425003c4cdc to your computer and use it in GitHub Desktop.
Circular array shift
This file contains hidden or 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 reverse_array(array: list, start, stop): | |
tmp = array[start:stop] | |
array[start:stop] = reversed(tmp) | |
return array | |
def circular_array_shift(array: list, shift: int): | |
if array and shift: | |
length = len(array) | |
shift %= length | |
array = reverse_array(array, 0, length) | |
array = reverse_array(array, 0, shift) | |
array = reverse_array(array, shift, length) | |
return array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment