Python: Array/List Rotation – Right

Python program to rotate an array towards its right

Inplace Method

# Rotate Array
def roateRight(arr, size, rotate_count):
    array_length = len(arr)
    if array_length <= 0:
        return arr
 
    if size > array_length:
        size = array_length
 
    # Figure out effective roation count
    # eg: size = 10 and rotation_count = 15
    # effective roation_count is 5
    if rotate_count > size:
        rotate_count = rotate_count - size
 
    print ("Array Length: %d" %array_length)
    print ("Array Size to consider: %d" %size)
    print ("Effective Rotation Count: %d" %rotate_count)
 
    while rotate_count > 0:
        rotate_count -= 1
        shiftRight(arr, size)
 
# Shift One Element
def shiftRight(arr, size):
    array_length = len(arr)
 
    # return if array is empty
    if array_length <= 0:
        return
 
    # Check and use the size passed in
    if size < array_length:
        array_length = size
 
    lastItem = arr[array_length-1]
 
    for index in range (array_length - 1, 0, -1):
        arr[index] = arr[index - 1]
 
    arr[0] = lastItem
 
# Tests
input_array = [1,2,3,4,5,6,7,8,9]
print ("Input", input_array)
roateRight(input_array, 5, 6)
print ("Output", input_array)
print ("")
input_array = [1,2,3,4,5,6,7,8,9]
print ("Input", input_array)
roateRight(input_array, 12, 14)
print ("Output", input_array)
print ("")
input_array = []
print ("Input", input_array)
roateRight(input_array, 12, 14)
print ("Output", input_array)
 
'''
Output:
Input [1, 2, 3, 4, 5, 6, 7, 8, 9]                                                                                                                                     
Array Length: 9                                                                                                                                                       
Array Size to consider: 5                                                                                                                                             
Effective Rotation Count: 1                                                                                                                                           
Output [5, 1, 2, 3, 4, 6, 7, 8, 9]                                                                                                                                    
 
Input [1, 2, 3, 4, 5, 6, 7, 8, 9]                                                                                                                                     
Array Length: 9                                                                                                                                                       
Array Size to consider: 9                                                                                                                                             
Effective Rotation Count: 5                                                                                                                                           
Output [5, 6, 7, 8, 9, 1, 2, 3, 4]                                                                                                                                    
 
Input []                                                                                                                                                              
Output []
'''

Split and Merge Method

# Rotate Array
def roateRight(arr, size, rotate_count):
    array_length = len(arr)
    if array_length <= 0:
        return arr
 
    if size > array_length:
        size = array_length
 
    # Figure out effective roation count
    # eg: size = 10 and rotation_count = 15
    # effective roation_count is 5
    if rotate_count > size:
        rotate_count = rotate_count - size
 
    print ("Array Length: %d" %array_length)
    print ("Array Size to consider: %d" %size)
    print ("Effective Rotation Count: %d" %rotate_count)
    return arr[size-rotate_count:size] + arr[:size-rotate_count] + arr[size:array_length]
 
 
# Tests
input_array = [1,2,3,4,5,6,7,8,9]
print ("Input", input_array)
output_array = roateRight(input_array, 5, 6)
print ("Output", output_array)
print ("")
input_array = [1,2,3,4,5,6,7,8,9]
print ("Input", input_array)
output_array = roateRight(input_array, 12, 14)
print ("Output", output_array)
print ("")
input_array = []
print ("Input", input_array)
output_array = roateRight(input_array, 12, 14)
print ("Output", output_array)
 
'''
Output:
Input [1, 2, 3, 4, 5, 6, 7, 8, 9]                                                                                                                                     
Array Length: 9                                                                                                                                                       
Array Size to consider: 5                                                                                                                                             
Effective Rotation Count: 1                                                                                                                                           
Output [5, 1, 2, 3, 4, 6, 7, 8, 9]                                                                                                                                    
 
Input [1, 2, 3, 4, 5, 6, 7, 8, 9]                                                                                                                                     
Array Length: 9                                                                                                                                                       
Array Size to consider: 9                                                                                                                                             
Effective Rotation Count: 5                                                                                                                                           
Output [5, 6, 7, 8, 9, 1, 2, 3, 4]                                                                                                                                    
 
Input []                                                                                                                                                              
Output []
'''

Hope this helps.

Leave a comment

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.