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


$ include "seed7_05.s7i";
 
const proc: main is func
  local
    const array integer: sortedList is [] ( 1, 2, 3, 4, 5, 6, 7, 8, 9);
    var array integer: list is 0 times 0;
    var array integer: reversedList is 0 times 0;
    var integer: score is 0;
    var integer: index is 0;
    var integer: number is 0;
    var integer: reverse is 0;
  begin
    for number range sortedList do
      index := rand(1, succ(length(list)));
      list := list[.. pred(index)] & [] (number) & list[index ..];
    end for;
    repeat
      write("Current list: ");
      for number range list do
        write(number <& " ");
      end for;
      repeat
        write(" Digits to reverse? ");
        readln(reverse);
        if reverse < 2 or reverse > 9 then
          write("Please enter a value between 2 and 9.");
        end if;
      until reverse >= 2 and reverse <= 9;
      incr(score);
      reversedList := 0 times 0;
      for index range reverse downto 1 do
        reversedList &:= list[index];
      end for;
      list := reversedList & list[succ(reverse) ..];
    until list = sortedList;
    writeln("Congratulations, you sorted the list in " <& score <& " reversals.");
  end func;