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


puts(1,"Given a jumbled list of the numbers 1 to 9,\n")
puts(1,"you must select how many digits from the left to reverse.\n")
puts(1,"Your goal is to get the digits in order with 1 on the left and 9 on the right.\n")
 
constant inums = tagset(9)
sequence nums
integer turns = 0, flip
 
while 1 do
    nums = shuffle(inums)
    if nums!=inums then exit end if
end while
 
while 1 do
    printf(1,"%2d : %d %d %d %d %d %d %d %d %d ",turns&nums)
    if nums=inums then exit end if
    flip = prompt_number(" -- How many numbers should be flipped? ",{1,9})
    nums[1..flip] = reverse(nums[1..flip])
    turns += 1
end while
 
printf(1,"\nYou took %d turns to put the digits in order.", turns)