Answer to Question #297473 in Python for Ram

Question #297473

Word Rotation


Given a sentence and an integer N, write a program to rotate letters of the sentence among the words without changing the length of those words and positions of the spaces.


Input

The first line of input will be a sentence.

The second line of input will be an integer N.


Output

The output should be a single line containing the sentence by rotating the letters among the words without changing the length of the words and positions of the spaces.


Explanation

For example, if the given sentence and N are


Welcome to your first problem

5

Rotate the sentence 5 letters towards right side without changing the length of the words and position of spaces in the original sentence.


So the output should be

oblemWe lc omet oyour firstpr


Sample Input 1

Welcome to your first problem

5

Sample Output 1

oblemWe lc omet oyour firstpr


Sample Input 2

All the best

2

Sample Output 2

stA llt hebe


1
Expert's answer
2022-02-14T19:21:35-0500
text = input()

n = input()

while type(n) != int:
    if n.isdigit():
        n = int(n)
    else:
        n = input()

indexes = []

for i in range(len(text)):
    if text[i] == ' ':
        indexes.append(i)

text = ''.join(text.split(' '))
newText = text[-n:] + text[:-n]

for i in indexes:
    newText = newText[:i] + " " + newText[i:]

print(newText)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS