How do I rotate a string to the right until every letter has been rotated?
Question:
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:
Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
Abobus
Instead, the output was:
Abobus
Abobus
Abobus
Abobus
Abobus
Abobus
Abobus
What exactly am I doing wrong?
Answers:
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
I changed the logic of the program you have written..
This will work
word = "Abobus"
def rotation():
print(word)
n = len(word) - 1
for _ in range(len(word)):
c = word[n:] + word[0: n]
n -= 1
print(c)
rotation()
Another option (which has potential to be more efficient when working with large inputs) is to use collections.deque
which is optimized for such operations:
import collections
word = "Abobus"
word_as_deque = collections.deque("Abobus")
for _ in word:
print("".join(word_as_deque))
word_as_deque.appendleft(word_as_deque.pop())
## output:
Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
To rotate a word to the right, you can use slicing to extract the last letter of the word and append it to the beginning of the word. Here’s how you can do it:
def rotate_word(word):
rotated_word = word[-1] + word[:-1]
return rotated_word
word = "Abobus"
print(rotate_word(word)) # prints "sAbobu"
To get all the rotated versions of the word, you can use a loop to call the rotate_word
function multiple times. Here’s an example of how you can do that:
def rotate_word(word):
rotated_word = word[-1] + word[:-1]
return rotated_word
def get_rotated_words(word):
for i in range(len(word)):
rotated_word = rotate_word(word)
word = rotated_word
print(word)
word = "Abobus"
get_rotated_words(word)
or you can use this code(same code but its like your way):
def rotation(word):
for i in range(len(word)):
rotated_word = word[-1] + word[:-1]
word = rotated_word
print(word)
word = "Abobus"
rotation(word)
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:
Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
Abobus
Instead, the output was:
Abobus
Abobus
Abobus
Abobus
Abobus
Abobus
Abobus
What exactly am I doing wrong?
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
I changed the logic of the program you have written..
This will work
word = "Abobus"
def rotation():
print(word)
n = len(word) - 1
for _ in range(len(word)):
c = word[n:] + word[0: n]
n -= 1
print(c)
rotation()
Another option (which has potential to be more efficient when working with large inputs) is to use collections.deque
which is optimized for such operations:
import collections
word = "Abobus"
word_as_deque = collections.deque("Abobus")
for _ in word:
print("".join(word_as_deque))
word_as_deque.appendleft(word_as_deque.pop())
## output:
Abobus
sAbobu
usAbob
busAbo
obusAb
bobusA
To rotate a word to the right, you can use slicing to extract the last letter of the word and append it to the beginning of the word. Here’s how you can do it:
def rotate_word(word):
rotated_word = word[-1] + word[:-1]
return rotated_word
word = "Abobus"
print(rotate_word(word)) # prints "sAbobu"
To get all the rotated versions of the word, you can use a loop to call the rotate_word
function multiple times. Here’s an example of how you can do that:
def rotate_word(word):
rotated_word = word[-1] + word[:-1]
return rotated_word
def get_rotated_words(word):
for i in range(len(word)):
rotated_word = rotate_word(word)
word = rotated_word
print(word)
word = "Abobus"
get_rotated_words(word)
or you can use this code(same code but its like your way):
def rotation(word):
for i in range(len(word)):
rotated_word = word[-1] + word[:-1]
word = rotated_word
print(word)
word = "Abobus"
rotation(word)