Computer Science, asked by megha20052002agrawal, 11 months ago

Write a program rotates the element of a list so that the element at the first index moves to the second index, the element in the second index moves to the third index, etc., and the element in the last index moves to the first index.

Answers

Answered by Anonymous
19

Hey mate....

here's the answer....

For rotation.

Input : n = 2

List_1 = [1, 2, 3, 4, 5, 6]

Output : List_1 = [5, 6, 1, 2, 3, 4]

We get output list after right rotating

clockwise given list by 2.

Input : n = 3

List_1 = [3, 0, 1, 4, 2, 3]

Output : List_1 = [4, 2, 3, 3, 0, 1]

For moving...

Python program to right rotate a list by n

# Returns the rotated list

def rightRotate(lists, num):

output_list = []

# Will add values from n to the new list

for item in range(len(lists) - num, len(lists)):

output_list.append(lists[item]) # Will add the values before

# n to the end of new list

for item in range(0, len(lists) - num):

output_list.append(lists[item]) return output_list

# Driver Code

rotate_num = 3

list_1 = [1, 2, 3, 4, 5, 6]

print(rightRotate(list_1, rotate_num

Hope it helps you ❤️

Answered by Brainly9b78
2
Python program to right rotate a list by n

Given a list, right rotate the list by n position.

Examples :

Input : n = 2 List_1 = [1, 2, 3, 4, 5, 6] Output : List_1 = [5, 6, 1, 2, 3, 4] We get output list after right rotating (clockwise) given list by 2. Input : n = 3 List_1 = [3, 0, 1, 4, 2, 3] Output : List_1 = [4, 2, 3, 3, 0, 1]

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach #1 : Traverse the first list one by one and then put the elements at required places in a second list.

Similar questions