/* String rotation (code golf) in Picat. https://codegolf.stackexchange.com/questions/177221/string-rotation-output-string-repeatedly-moving-first-character-to-the-end """ String rotation - output string repeatedly moving first character to the end The challenge here is to take a string and output all its rotations, by repeatedly moving the first character to the end, once per character in the string, ending with the original string: john -> ohnj, hnjo, njoh, john You may also cycle in the other direction, moving characters from the end: john -> njoh, hnjo, ohnj, john You should still output one rotation per letter even if the original word is reached before that: heehee -> eeheeh, eheehe, heehee, eeheeh, eheehe, heehee Character arrays are allowed, as long as the result works as shown above. Shortest answer wins! """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util. % import cp. main => go. go ?=> rotate_all("john").println, rotate_all("heehee").println, rotate_all2("john").println, rotate_all2("heehee").println, nl. go => true. % 51 chars + "import util" = 51+11 + 62 chars rotate_all(L)=[[drop(L,I-1),take(L,I-1)].flatten:I in 1..L.len+1]. % 39 chars rotate_all2(L)=[L[I+1..N]++L[1..I]:I in 0..N]=>N=L.len.