Question
In Python: In this problem we will be building a function to rotate a string by a positive integer n that puts the last n
In Python:
In this problem we will be building a function to rotate a string by a positive integer n that puts the last n characters in front of the string.
rotate_string("hello world",3) would return "rldhello wo"
**If the string is empty or a single character, the function should simply return the string unchanged.**
If shift amount is greater than the length of the string, return the difference between shift-len(string) as spaces placed at the beginning of the string.
rotate_string("cat",5) would yield " cat" (cat with 2 spaces) since shift=5, len(string)=3, 5-3=2 therefore 2 spaces get padded to the string on the front
This is an **excellent** opportunity to get comfortable with string slicing (this problem can be solved other ways, but slicing is quite effective!)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started