Python Program to Print the List by Rotating D Times to the Left

 

Rotate D Times

Write a program to print the list by rotating D times to the left.

Input

The first line of input will contain comma-separated integers.
The second line of input will contain a positive integer (D).

Output

The output should be a single line containing a list by rotating the list D times to the left.

Explanation

For example, if the given string is “1,2,3,4,5” and D is 2. Rotate the two elements of the list to the left. So the output should be

[3, 4, 5, 1, 2]

Sample Input 1
1,2,3,4,5
2

Sample Output 1
[3, 4, 5, 1, 21

Code:

def convert_string_to_int(str_num_list):
    new_list = []
    for item in str_num_list:
        num = int(item)
        new_list.append(num)
    return new_list

str_num_list = input ().split(",")
rotate_times = int(input())
int_list = convert_string_to_int(str_num_list)
len_of_list =len(int_list)
val = rotate_times % len_of_list
first_part= int_list[0: val]
second_part=int_list[val:]
second_part.extend(first_part)
print(second_part)

Input
1,3,5,7,9,11
25

Output
[3, 5, 7, 9, 11, 11

Comments

Popular posts from this blog

CODING ASSIGNMENT 4

CODING PRACTICE 11 Music Page

CCBP Static Website Coding Assignment 2 Solution | Yoga Page