, string , , , , .
, , , "rotate_to_smallest".
my_strings = ['1234', '123', '2341', '4312', '312', '56', '65', '1236']
def rotate_to_smallest(x):
smallest = x
for i in xrange(1, len(x)):
rotation = x[i :] + x[: i]
if rotation < smallest:
smallest = rotation
return smallest
def unique_rotations(my_strings):
uniques = set(())
for s in my_strings:
smallest_rotation = rotate_to_smallest(s)
if smallest_rotation not in uniques:
uniques.add(smallest_rotation)
return uniques
:
>>> unique_rotations(my_strings)
set(['1234', '56', '1243', '123', '1236'])