Rotate Words Longer than given Length L by K lette
Given a sentence, write a program to rotate words longer than given length L by K letters towards the right.
Note: Even if K is greater than word length, rotate the word multiple times so that K letters will be rotated towards right.Input
The first line of input will contain a string.
The second line of input will contain two space-separated integers, denoting L and K.Output
The output should be a single line containing the sentence by rotating the words whose length is greater than L by K letters towards right.
Sample Input 1
My parents and I went to a movie
2 1
Sample Output 1
My sparent dan I twen to a emovi
Sample Input 2
We drink coffee every morning
4 30
Sample Output 2
We drink coffee every ngmorni
def rotate(word, k):
for i in range(k):
c = word[-1]
word = word[0:-1]
c += word
word = c
return word
source_string = input()
line2 = [int(i) for i in input().split()]
L = line2[0]
K = line2[1]
source = [i for i in source_string.split()]
result = []
for w in source:
if len(w) > L:
result.append(rotate(w, K))
else:
result.append(w)
result_string = result[0]
for w in result[1:]:
result_string += ' '
result_string += w
print(result_string)
Comments
Leave a comment