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
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))
Comments
Leave a comment