#!/usr/bin/python2.7
def rotate_3n(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place
"""
for x in range(0, k):
le = nums[0]
ri = le
for y in range(1, len(nums)):
ri = nums[y]
nums[y] = le
le = ri
nums[0] = le
def rotate_n(nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: None Do not return anything, modify nums in-place
"""
# Get the length and make sure there is something to rotate
l = len(nums)
if l < 2:
return
# Make sure the rotation is within the length
k = k % l
if (k == 0):
return
init = 0
count = 0
o = nums[init]
i = k + init
r = o
while init < k and count < l:
while (i != init):
r = nums[i]
nums[i] = o
i = (i + k) % l
o = r
count += 1
nums[init] = o
init = init + 1
o = nums[init]
i = k + init
count += 1
arr = [1,2,3,4,5,6,7,8,9,10]
print("The original array is:")
print(arr)
print("")
rotate_3n(arr, 3)
print("Rotated with 3N complexity:")
print(arr)
arr = [1,2,3,4,5,6,7,8,9,10]
rotate_n(arr, 3)
print("Rotated with N complexity:")
print(arr)
print("")
print("If both answers match, it's right")
print("")