Answer to Question #180149 in Python for vijay

Question #180149

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


1
Expert's answer
2021-04-11T15:53:19-0400
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)

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