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.
The first line of input will be a sentence.
The second line of input will be an integer N.
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.
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
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()
Comments
Leave a comment