Generate all the rotational permutations of a string

in code


This does not generate lexicographic permutations (use Lexicographer for that!); this plucks off one letter from the end and attaches it to the start:

rotations = (str) ->
  Array(str.length).fill(str).map (rotation, index) ->
    while index--
      rotation = rotation[-1..-1] + rotation[0..-2]

    rotation

I liked my solution and wanted to share.



Remap a JavaScript Object

in code


Your email address will not be published. Required fields are marked *