Answer to Question #248728 in Python for sai krishna

Question #248728

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-10-08T23:51:00-0400
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


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