Answer to Question #240519 in Python for sai krishna

Question #240519

given a sentence S. and a positive integer N. write a program to shift the sentence by N words to the right

INPUT

The first line pf input is a string S.

The second line of input is an integer N

OUTPUT

The output should be a single line containing the sentence by shifitng the N word to the right

EXPLANATION

in the example the sentence is python is a programming language and N is 2 shift the sentence two words to the right which means the last two words will be moved to the front.

so the output should be programming language python is a

sample input 1

Python is a programming language

2

sample output 1

programming language Python is a

sample input 2

Cats hate water

4

Sample output 2

water Cats hate


1
Expert's answer
2021-09-22T23:44:49-0400
sentence = input()
N = int(input())
words = sentence.split()
output_list = []
      
for item in range(len(words) - N, len(words)):
    output_list.append(words[item])
      
new=" "   
for item in range(0, len(words) - N): 
    output_list.append(words[item])


print(new.join(output_list))

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