14
1
Fannkuch is a classic benchmark program. The name comes from the German "Pfannkuchen"- pancakes- for the algorithm's resemblance to flipping stacks of pancakes. A Fannkuch sequence of numbers is formed as follows:
Take a permutation of {1.....n}, for example: {4,2,1,5,3}. Take the first element, here 4, and reverse the order of the first 4 elements: {5,1,2,4,3}. Repeat this until the first element is a 1, so flipping won't change anything more: {3,4,2,1,5}, {2,4,3,1,5}, {4,2,3,1,5}, {1,3,2,4,5}
You are to write a program or function which calculates a Fannkuch-like sequence for strings of alphabetic characters. Instead of using numbers to indicate how many elements of the list should be flipped each time, the position of a letter in the alphabet should be used. For example, a leading c
would indicate that you should reverse the order of the first 3 elements, while a leading a
indicates that the sequence is complete.
Input
Input will be provided as a string via stdin or as a function argument. The string will contain between 1 and 26 distinct lowercase letters. Strings will not contain letters whose equivalent index would cause the Fannkuch algorithm to flip more elements than exist.
Output
Programs or functions should return or print to stdout the sequence of terms produced by applying the Fannkuch algorithm until a leading a
is encountered, including the initial string. For example, if the input is bca
, you might print:
bca
cba
abc
Printed results can use any reasonable separator- commas, newlines, etc. Any choice of whitespace is acceptable.
As another example, if your input is eabdc
you might return:
("eabdc"
"cdbae"
"bdcae"
"dbcae"
"acbde")
Rules and Scoring
This is code-golf- the shortest program wins. Standard Loopholes are disallowed.
Nice work. We don't get much
proc fcmp
around here. – Alex A. – 2015-07-27T20:24:31.393