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
from collections import deque
S =input("Enter the string: ")
N=int(input("Enter an interger N: "))
S_deq = deque(S.split())
S_deq.rotate(N)
print (' '.join(S_deq))
Sample output:
Enter the string: python is a programming language
Enter an interger N: 2
programming language python is a
Comments
Leave a comment