The way you have your code structured does duplicate rotation because you reassign n on each step of the loop and use the iteration variable i in your slices.
So the process in the loop from your example is:
i = 0
- You have
n = 197 and the rotation logic does nothing with i = 0 so you add 197 to rotations
i = 1
- Again you have
n = 197, and rotation logic makes n = 971 and you add that to rotations.
i = 2
- Now
n = 971, and the rotation logic slices from index 2, but n has already been rotated so we have n = 197 again, which is added to rotations (and removed since rotations is a set). Basically n has already been rotated forward, and now it is being rotated forward 2 steps (back to the initial value and skipping over n = 719)
To fix this you can either:
1. Keep n at its initial value and on each step rotate n the full amount (i) and add that to rotation without modifying n. Like so:
def rotation(n):
rotations = set()
for i in range( len( str(n) ) ):
rotations.add(int( str(n)[i:] + str(n)[:i] ))
return rotations
2. Rotate n forward on each step, but only rotate it forward one position each time. Like so:
def rotation(n):
rotations = set()
for i in range( len( str(n) ) ):
n = int( str(n)[1:] + str(n)[:1] )
rotations.add(n)
return rotations
n(and work with element at index0), or work with the indexi(and assign the result to a temp variable. Don't do both. Write what you are trying to do on a piece of paper, you''ll understand. :)