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


// Rosetta Code problem: https://www.rosettacode.org/wiki/Number_reversal_game
// by Jjuanhdez, 06/2022
 
print "Given a jumbled list of the numbers 1 to 9,   "
print "you must select how many digits from the left "
print "to reverse. Your goal is to get the digits in "
print "order with 1 on the left and 9 on the right.\n"
 
dim nums(10)
dim a(10)
intentos = 0: denuevo = true: colum = 6
 
//valores iniciales
for i = 1 to 9
    nums(i) = i
next i
 
for i = 9 to 2 step -1
    n = int(ran(i)) + 1
    if n <> i then
        a(i) = nums(i)
        nums(i) = nums(n)
        nums(n) = a(i)
    fi
next i
 
repeat
    if intentos < 10  print " ";
    print intentos, ": ";
    for i = 1 to 9
        print nums(i), " ";
    next i
 
    if not denuevo  break
 
    input "  -- How many do we flip " volteo
    if volteo < 0 or volteo > 9  volteo = 0
 
    for i = 1 to int(volteo / 2)
        a(i) = nums(volteo - i + 1)
        nums(volteo - i + 1) = nums(i)
        nums(i) = a(i)
    next i
 
    denuevo = false
    //comprobamos el orden
    for i = 1 to 8
        if nums(i) > nums(i + 1) then
            denuevo = true
            break
        fi
    next i
 
    if volteo > 0  intentos = intentos + 1
until false
print "\n\n   You needed ", intentos, " attempts."
end