Answer to Question #182819 in Python for vijay

Question #182819

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-04-19T01:40:21-0400
def RotateSentence(sentence, n):
    words = sentence.split()
    lengths = [len(w) for w in words]
    s = ''.join(words)
    s = s[-n:] + s[:-n]
    res = ''
    i = 0
    for l in lengths:
        if res:
            res += ' '
        res += s[i:i+l]
        i += l
    return res

def _main():
    s = input()
    n = int(input())
    s = RotateSentence(s, n)
    print(s)

if __name__ == '__main__':
    _main()

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