Task

Given a jumbled list of the numbers   1   to   9   that are definitely   not   in
ascending order.

Show the list,   and then ask the player how many digits from the
left to reverse.

Reverse those digits,   then ask again,   until all the digits end up in ascending order.



The score is the count of the reversals needed to attain the ascending order.



Note: Assume the player's input does not need extra validation.



Related tasks

   Sorting algorithms/Pancake sort
   Pancake sorting.
   Topswops


correctList,scrambledList,N:=[1..9].walk(), correctList.shuffle(),correctList.len();
correctList,scrambledList=correctList.concat(""), scrambledList.concat(""); // list to string
attempts:=0;
while(scrambledList!=correctList){ // Repeat until the sequence is correct
   n:=ask(("[%d] %s How many numbers (from the left) should be flipped? ")
	      .fmt(attempts,scrambledList));
   try{ n=n.toInt() }catch{ println("Not a number"); continue; }
   if(not (0<=n<N)){ println("Out of range"); continue; }
   attempts+=1;
   // Reverse the first part of the string and add the second part
   scrambledList=scrambledList[0,n].reverse() + scrambledList[n,*];
}
println("You took %d attempts to get the correct sequence.".fmt(attempts));