Skip to content

Instantly share code, notes, and snippets.

@kupp1
Created September 3, 2018 16:41
Show Gist options
  • Save kupp1/0c8e4b734f57577d5cbf5425003c4cdc to your computer and use it in GitHub Desktop.
Save kupp1/0c8e4b734f57577d5cbf5425003c4cdc to your computer and use it in GitHub Desktop.
Circular array shift
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