-1

I want to rotate the string so that its last character goes on the first place and so on.

word = "Burrow"

Sample Output:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

So far I can rotate the string in the opposite direction. Here's what I have done:

def main():
    word = "Burrow"
    a = word
    for i in range(len(word)):
        c = a[i:] + a[:i]
        print(c)

if __name__ == "__main__":
    main()

Burrow
urrowB
rrowBu
rowBur
owBurr
wBurro

Cannot find out what to do to get the desirable output. Tried negative indices but never succeeded.

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
rg4s
  • 811
  • 5
  • 22
  • Could your task be described with ["reversing a string"](https://stackoverflow.com/questions/931092/reverse-a-string-in-python)…? – deceze Sep 01 '22 at 14:10
  • @deceze I don't see how this is reversing a string at all. – iced Sep 01 '22 at 14:16
  • @iced *”swap the string so that its last character goes on the first place and so on”* — Sounds like “reverse”, only more complicated. But it’s open to interpretation. – deceze Sep 01 '22 at 15:27
  • @deceze Look at the sample output, there's no reversing occuring here. – iced Sep 01 '22 at 15:31

6 Answers6

2

You're on the right track, but you need to reverse the order that you iterate over the characters in the input word. You want to start at the end and go backwards.

def main():
    word = "Burrow"
    a = word
    for i in range(len(word), 0, -1):
        c = a[i:] + a[:i]
        print(c)

if __name__ == "__main__":
    main()

Output:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB

The change was in range(len(word), 0, -1), which starts the range at len(word) and counts backwards towards 0.

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
  • 1
    oh, did not know about inverse looping. i am not fluent in python syntax yet :) thank you! – rg4s Sep 01 '22 at 14:22
1

Try this:


def main(word):
    l = len(word)
    out = list(word)

    for a in range(l):
        print(''.join(out[:l]))
        out.insert(0,out[l-1])

word = "Burrow"
if __name__ =='__main__':
    main(word)


output

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB
codester_09
  • 5,622
  • 2
  • 5
  • 27
1

Remember that negative indices in python correspond to counting backwards from the end of your string / list, and think about what index you want to break your string at. Also note that your first slice starts at the index that your second slice ends, since slices exclude the end index.

Iteration (i) Expected output Num letters from end to rotate around
0 Burrow 0
1 wBurro 1
2 owBurr 2
3 rowBur 3
... ...

Notice a pattern? The first part of your output is the last i letters in the ith iteration, so should start at the index -i and extend to the end. This means your slice would be [-i:]. The second part of your output ends at the start of your first part, so your slice would be [:-i]

So do this:

word = "Burrow"
a = word
for i in range(len(word)):
    c = a[-i:] + a[:-i]
    print(c)

Which outputs as expected:

Burrow
wBurro
owBurr
rowBur
rrowBu
urrowB
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
0

Try this :

  1. If you don't want to change the original variable word Solution : Create new word with index manipulation in each loop
word = "Burrow"

for i in range(len(word)-1):
    new_word = word[-1-i:] + word[:len(word)-1-i]
    print(new_word)
  1. Allowed to change the variable word itself Solution : In each iteration over the characters of word, keep on replacing the first and last index of the word
word = "Burrow"

for i in range(len(word)-1):
    word = word[-1] + word[:-1]
    print(word)
Prashant Kumar
  • 2,057
  • 2
  • 9
  • 22
0

Try this:

def main():
    word = "Burrow"
    a = word
    c = ''
    for i in range(len(word)):
        c = a if i == 0 else a[-1] + a[0:-1]
        print(c)
        a=c

if __name__ == "__main__":
    main()
PepeChuy
  • 84
  • 3
0

This will work for you.

def main():
    word = "Burrow"
    print(word)
    for i in range(len(word) - 1):
        word = word[-1] + word[:-1]
        print(word)

if __name__ == "__main__":
     main()
tcotts
  • 170
  • 1
  • 10