114k views
3 votes
Write a function called rotateRight that takes a String as its first argument and a positive int as its second argument and rotates the String right by the given number of characters. Any characters that get moved off the right side of the string should wrap around to the left.

1 Answer

4 votes

Answer:

The function in Python is as follows:

def rotateRight(strng, d):

lent = len(strng)

retString = strng[lent - d : ] + strng[0 : lent - d]

return retString

Step-by-step explanation:

This defines the function

def rotateRight(strng, d):

This calculates the length of the string

lent = len(strng)

This calculates the return string

retString = strng[lent - d : ] + strng[0 : lent - d]

This returns the return string

return retString

Addition:

The return string is calculated as thus:

This string is split from the index passed to the function to the last element of the string, i.e. from dth to last.

The split string is then concatenated to the beginning of the remaining string

User Ali Saberi
by
5.3k points