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
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)
Comments
Leave a comment