Easy Tutorial
❮ Python Os Chdir Python Func Int ❯

Python Array Rotation by a Specified Number of Elements

Python3 Examples

Define an integer array and rotate a specified number of elements to the end of the array.

For example: (ar[], d, n) will rotate the first d elements of the array arr of length n to the end of the array.

The following demonstrates rotating the first two elements to the end of the array.

Original Array:

Rotated Array:

Example 1

def leftRotate(arr, d, n): 
    for i in range(d): 
        leftRotatebyOne(arr, n) 
def leftRotatebyOne(arr, n): 
    temp = arr[0] 
    for i in range(n-1): 
        arr[i] = arr[i+1] 
    arr[n-1] = temp 

def printArray(arr,size): 
    for i in range(size): 
        print("%d" % arr[i], end=" ") 

arr = [1, 2, 3, 4, 5, 6, 7] 
leftRotate(arr, 2, 7) 
printArray(arr, 7)

Output of the above example:

3 4 5 6 7 1 2

Example 2

def leftRotate(arr, d, n): 
    for i in range(gcd(d,n)): 
        temp = arr[i] 
        j = i 
        while 1: 
            k = j + d 
            if k >= n: 
                k = k - n 
            if k == i: 
                break
            arr[j] = arr[k] 
            j = k 
        arr[j] = temp 

def printArray(arr, size): 
    for i in range(size): 
        print("%d" % arr[i], end=" ") 

def gcd(a, b): 
    if b == 0: 
        return a; 
    else: 
        return gcd(b, a%b) 

arr = [1, 2, 3, 4, 5, 6, 7] 
leftRotate(arr, 2, 7) 
printArray(arr, 7)

Output of the above example:

3 4 5 6 7 1 2

Example 3

def reverseArray(arr, start, end): 
    while (start < end): 
        temp = arr[start] 
        arr[start] = arr[end] 
        arr[end] = temp 
        start += 1
        end = end-1

def leftRotate(arr, d): 
    n = len(arr) 
    reverseArray(arr, 0, d-1) 
    reverseArray(arr, d, n-1) 
    reverseArray(arr, 0, n-1) 

def printArray(arr): 
    for i in range(0, len(arr)): 
        print(arr[i], end=' ') 

arr = [1, 2, 3, 4, 5, 6, 7] 
leftRotate(arr, 2) 
printArray(arr)

Output of the above example:

3 4 5 6 7 1 2

Python3 Examples

❮ Python Os Chdir Python Func Int ❯