Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I rotate a string to the right until every letter has been rotated?

I want to rotate a word to the right, so that every letter has passed.

What I tried to do is make a function. It looks like this (yeah yeah ik lmao):

word = "Abobus";

length = len(word);

n = 1;

def rotation():
    for i in range(length + 1):
        c = word[0 : length-n] + word[length-n:]
        print(c)

rotation();

I needed the output to be:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
Abobus

Instead, the output was:

Abobus
Abobus
Abobus
Abobus
Abobus
Abobus
Abobus

What exactly am I doing wrong?

>Solution :

You are splitting at an index in word, but then just putting the two pieces back together. Also, you are not use i to move the split index.

def rotation(word):
    length = len(word)
    for i in range(length):
        c = word[-i:] + word[:-i]
        print(c)

rotation('Abobus')
# prints:
Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading