Skip to main content
added 248 characters in body
Source Link
Jérôme
  • 14.9k
  • 9
  • 74
  • 119

You're almost there. Except you're erasing the input n at each iteration. Use another variable name.

def rotation(n):
  rotations = set()
  for i in range( len( str(n) ) ):
    m = int( str(n)[i:] + str(n)[:i] )
    rotations.add(m)
  return rotations

print(rotation(197))

I would write it more like this, using a set comprehension:

def rotation(number):
    str_number = str(number)
    return {
        int( str_number[i:] + str_number[:i] )
        for i in range(len(str_number))
    }

Solution 2 by @Henry Woody is nice too. Rather than rotate input string by i at each iteration, rotate by 1 from last iteration.

You're almost there. Except you're erasing the input n at each iteration. Use another variable name.

def rotation(n):
  rotations = set()
  for i in range( len( str(n) ) ):
    m = int( str(n)[i:] + str(n)[:i] )
    rotations.add(m)
  return rotations

print(rotation(197))

You're almost there. Except you're erasing the input n at each iteration. Use another variable name.

def rotation(n):
  rotations = set()
  for i in range( len( str(n) ) ):
    m = int( str(n)[i:] + str(n)[:i] )
    rotations.add(m)
  return rotations

print(rotation(197))

I would write it more like this, using a set comprehension:

def rotation(number):
    str_number = str(number)
    return {
        int( str_number[i:] + str_number[:i] )
        for i in range(len(str_number))
    }

Solution 2 by @Henry Woody is nice too. Rather than rotate input string by i at each iteration, rotate by 1 from last iteration.

Source Link
Jérôme
  • 14.9k
  • 9
  • 74
  • 119

You're almost there. Except you're erasing the input n at each iteration. Use another variable name.

def rotation(n):
  rotations = set()
  for i in range( len( str(n) ) ):
    m = int( str(n)[i:] + str(n)[:i] )
    rotations.add(m)
  return rotations

print(rotation(197))