Answer to Question #208215 in Python for Sudheer

Question #208215

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.


1
Expert's answer
2021-06-19T07:00:33-0400
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)

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