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


# Input: the initial array
def play:
  def sorted: . == sort;
 
  def reverse(n):  (.[0:n] | reverse)  + .[n:];
 
  def prompt: "List: \(.list)\nEnter a pivot number: ";
 
  def report: "Great! Your score is \(.score)";
 
  {list: ., score: 0}
  | (if .list | sorted then "List was sorted to begin with."
     else
     prompt,
     ( label $done
     | foreach inputs as $n (.;
         .list |= reverse($n) | .score +=1;
         if .list | sorted then report, break $done else prompt end ))
     end); 