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


 
# Project : Number reversal game
 
rever = 1:9
leftrever = []
for n = 1 to len(rever)
     rnd = random(8) + 1
     temp = rever[n]
     rever[n] = rever[rnd]
     rever[rnd] = temp
next
see rever
see nl
while true
         num = 0
         leftrever = []
         showarray(rever)
         see " : Reverse how many = "
         give r
         r = number(r)
         for n = 1 to r
               add(leftrever, rever[n])
         next
         leftrever = reverse(leftrever)
         for pos = 1 to r
               rever[pos] = leftrever[pos]
         next  
         for m = 1 to len(rever)
              if rever[m] = m
                 num = num + 1
              ok
         next
         if num = len(rever)
            exit
         ok
end
see "You took " + num + " attempts." + nl
 
func swap(a, b)
       temp = a
       a = b
       b = temp
       return [a, b]
 
func showarray(vect)
       svect = ""
       for n = 1 to len(vect)
             svect = svect + vect[n] + " "
       next
       svect = left(svect, len(svect) - 1)
       see svect 
 