Created
October 19, 2009 09:56
-
-
Save yaotti/213241 to your computer and use it in GitHub Desktop.
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
# -*- 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