Skip to content

Instantly share code, notes, and snippets.

@yaotti
Created October 19, 2009 09:56
Show Gist options
  • Save yaotti/213241 to your computer and use it in GitHub Desktop.
Save yaotti/213241 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
def rotate_arr(arr, t):
"""programming peals
rotate array algorithm(destructive)"""
l = len(arr)
tmp = arr[0]
k = 0
while True:
j = k
k = j+t
if k > l-1: k -= l
if k == 0:
arr[j] = tmp
break
arr[j] = arr[k]
return arr
a = [1,2,3,4,5]
print rotate_arr(a, 2) # => [3,4,5,1,2]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment