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


 
RANDOMIZE
 
PRINT "Dada una lista aleatoria de números del 1 al 9,"
PRINT "indica cuantos dígitos de la izquierda voltear."
PRINT " El objetivo es obtener los dígitos en orden "
PRINT " con el 1 a la izquierda y el 9 a la derecha."
PRINT
 
DIM nums(1 to 9)
DIM temp(1 to 9)
!valores iniciales
FOR x = 1 to 9
    LET nums(x) = x
NEXT x
 
DO                                !barajamos
   FOR x = 9 to 2 step -1
       LET n = round(int(rnd*(x))+1)
       IF n <> x then
          !swap (nums(n), nums(x)) !no existe comado SWAP
          LET temp(n) = nums(x)
          LET nums(x) = nums(n)
          LET nums(n) = temp(n)
       END IF
   NEXT x
   FOR x = 1 to 8                 !nos aseguramos que no estén en orden
       IF nums(x) > nums(x+1) then EXIT DO
   NEXT x
LOOP
 
LET denuevo = -1
DO
   IF intentos < 10 then PRINT " ";
   PRINT intentos; ":";
   FOR x = 1 to 9
       PRINT nums(x);
   NEXT x
 
   IF (not denuevo <> 0) then EXIT DO
 
   INPUT prompt " -- ¿Cuántos volteamos? ": volteo
   IF volteo < 0 or volteo > 9 then LET volteo = 0
 
   FOR x = 1 to (ip(volteo/2))
       !swap (nums(x), nums(volteo-x+1))  !no existe SWAP
       LET temp(n) = nums(volteo-x+1)
       LET nums(volteo-x+1) = nums(x)
       LET nums(x) = temp(n)
   NEXT x
 
   LET denuevo = 0
   !comprobamos el orden
   FOR x = 1 to 8
       IF nums(x) > nums(x+1) then
          LET denuevo = -1
          EXIT FOR
       END IF
   NEXT x
 
   IF volteo > 0 then LET intentos = intentos+1
LOOP
 
PRINT
PRINT
PRINT "Necesitaste "; ltrim$(rtrim$(str$(intentos))); " intentos."
END
 