Answer to Question #217020 in Python for srikanth

Question #217020
Rotate Words In Sentence

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
2021-07-15T01:53:26-0400
sentence = input()
N = int(input())
words = sentence.split()
lengths = [len(word) for word in words]
tempSentence = ''.join(words)
tempSentence = tempSentence[-N:] + tempSentence[:-N]
rotatedSentence = ''
letterCounter = 0
for letter in lengths:
    if rotatedSentence:
        rotatedSentence += ' '
    rotatedSentence += tempSentence[letterCounter:letterCounter+letter]
    letterCounter += letter


print(rotatedSentence)




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
New on Blog
APPROVED BY CLIENTS