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.
sentences = input('Enter string: ')
L, K = [int(i) for i in input().split()]
source = [i for i in sentences.split()]
result = []
def rotate(strings, k):
for i in range(k):
c = strings[-1]
strings = strings[0:-1]
c += strings
strings = c
return strings
for cha in source:
if len(cha) > L:
result.append(rotate(cha, K))
else:
result.append(cha)
final = result[0]
for cha in result[1:]:
final += ' '
final += cha
print(final)
Comments
Leave a comment