Python Program to Left Rotate a List by R Times

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

Left Rotation of a List:

An array’s elements are shifted to the left when it is rotated to the left, as demonstrated in the image below. Left rotation involves rotating the array’s elements clockwise to the provided number of places.

Examples:

Example1:

Input:

given list =[11, 28, 9, 7, 6, 1, 3, 4, 19]
number of positions = 3

Output:

The list before rotating 3 times = [11, 28, 9, 7, 6, 1, 3, 4, 19] 
The list after rotating 3 times : [7, 6, 1, 3, 4, 19, 11, 28, 9]

Example2:

Input:

given list = ['hello', 'this', 'is', 'the', 'btechgeeks']
number of positions = 2

Output:

The list before rotating 2 times = ['hello', 'this', 'is', 'the', 'btechgeeks']
The list after rotating 2 times : ['is', 'the', 'btechgeeks', 'hello', 'this']

Program to Left Rotate a List by r Times in Python

There are several ways to left rotate the given list by r times in Python some of them are:

Method #1:Using Indexing(Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Give the number of rotations r as static input and store it in another variable.
  • Pass the given list and number of rotations as arguments to the leftRotateList function which rotates the given list to the left by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the LeftRotateOne function inside the for loop which rotates the list to the left one time.
  • In this way, we rotate the given list to the left r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the lefy by one time


def leftRotateByOne(given_list):
    # intializing first variablee to the first element of the given list
    firstval = given_list[0]
    # traversing length of given list -1 times using for loop
    for i in range(len(given_list) - 1):
      # initializing every element with the next element
        given_list[i] = given_list[i + 1]
    # intializing last value to the first valuee of the given list
    given_list[-1] = firstval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def leftRotateList(given_list, positions):
    # Pass the given list to the LeftRotateOne function inside the
    # for loop which rotates the list to the left one time.
    for numb in range(positions):
        leftRotateByOne(given_list)


# Driver Code
# Give the list as static input and store it in a variable.
given_list = [9, 1, 3, 10, 47, 19, 43]
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as static input and store it in another variable.
positions = 4
# Pass the given list and number of rotations as arguments to the leftRotateList function
# which rotates the given list to the left by r positions.
leftRotateList(given_list, positions)
# print the list after rotating to the left r times
print('The list after rotating r times : ', given_list)
print(given_list)

Output:

The list before rotating r times =  [9, 1, 3, 10, 47, 19, 43]
The list after rotating r times :  [47, 19, 43, 9, 1, 3, 10]

The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.

Method #2:Using Indexing(User Input)

i)Integer List

Approach:

  • Give the integer list as user input using map(),split(),list() and int functions.
  • Store it in a variable.
  • Give the number of rotations r as user input using int(input()) and store it in another variable.
  • Pass the given list and number of rotations as arguments to the leftRotateList function which rotates the given list to the left by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the LeftRotateOne function inside the for loop which rotates the list to the left one time.
  • In this way, we rotate the given list to the left r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the lefy by one time


def leftRotateByOne(given_list):
    # intializing first variablee to the first element of the given list
    firstval = given_list[0]
    # traversing length of given list -1 times using for loop
    for i in range(len(given_list) - 1):
      # initializing every element with the next element
        given_list[i] = given_list[i + 1]
    # intializing last value to the first valuee of the given list
    given_list[-1] = firstval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def leftRotateList(given_list, positions):
    # Pass the given list to the LeftRotateOne function inside the
    # for loop which rotates the list to the left one time.
    for numb in range(positions):
        leftRotateByOne(given_list)


# Driver Code
# Give the list as user input using map(),split(),list() and int functions.
# Store it in a variable. 
given_list = list(
    map(int, input('Enter some random list separated by spaces = ').split()))
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as user input using int(input())
# and store it in another variable.
positions = int(input('Enter some random number of positions = '))
# Pass the given list and number of rotations as arguments to the leftRotateList function
# which rotates the given list to the left by r positions.
leftRotateList(given_list, positions)
# print the list after rotating to the left r times
print('The list after rotating r times : ', given_list)

Output:

Enter some random list separated by spaces = 11 28 9 7 6 1 3 4 19
The list before rotating r times = [11, 28, 9, 7, 6, 1, 3, 4, 19]
Enter some random number of positions = 3
The list after rotating r times : [7, 6, 1, 3, 4, 19, 11, 28, 9]

The above solution has a time complexity of, where n is the size of the input and r is the number of rotations.

ii)String List

Approach:

  • Give the string list as user input using split(),list() functions.
  • Store it in a variable.
  • Give the number of rotations r as user input using int(input()) and store it in another variable.
  • Pass the given list and number of rotations as arguments to the leftRotateList function which rotates the given list to the left by r positions.
  • Use for loop to loop r times.
  • Pass the given list to the LeftRotateOne function inside the for loop which rotates the list to the left one time.
  • In this way, we rotate the given list to the left r times.
  • The Exit of the Program.

Below is the implementation:

# function which accepts the giveen list as argument
# and rotates the given list to the lefy by one time


def leftRotateByOne(given_list):
    # intializing first variablee to the first element of the given list
    firstval = given_list[0]
    # traversing length of given list -1 times using for loop
    for i in range(len(given_list) - 1):
      # initializing every element with the next element
        given_list[i] = given_list[i + 1]
    # intializing last value to the first valuee of the given list
    given_list[-1] = firstval


# function which accepts the given list and number
# of positions as arguments and rotate the given list r times here r=positions
def leftRotateList(given_list, positions):
    # Pass the given list to the LeftRotateOne function inside the
    # for loop which rotates the list to the left one time.
    for numb in range(positions):
        leftRotateByOne(given_list)


# Driver Code
# Give the list as user input using map(),split(),list() and int functions.
# Store it in a variable. 
given_list = list(input('Enter some random list separated by spaces = ').split())
print('The list before rotating r times = ', given_list)
# Give the number of rotations r as user input using int(input())
# and store it in another variable.
positions = int(input('Enter some random number of positions = '))
# Pass the given list and number of rotations as arguments to the leftRotateList function
# which rotates the given list to the left by r positions.
leftRotateList(given_list, positions)
# print the list after rotating to the left r times
print('The list after rotating r times : ', given_list)
print(given_list)

Output:

Enter some random list separated by spaces = hello this is the btechgeeks
The list before rotating r times = ['hello', 'this', 'is', 'the', 'btechgeeks']
Enter some random number of positions = 2
The list after rotating r times : ['is', 'the', 'btechgeeks', 'hello', 'this']

Related Programs: