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


 
// set the initial list of digits
set currentList to 1..9 sorted by random of a million
 
// make sure we don't start off already sorted
repeat while currentList equals 1..9
	sort currentList by random of a million
end repeat
 
// ask until it is solved
repeat while currentList isn't equal to 1..9
	add 1 to numberOfTurns
	set headline to "Turn #" & numberOfTurns & ":  You have " & currentList joined by empty
 
	ask "How many digits do you want to reverse?" titled headline message "On each turn, specify the number of initial digits to reverse."
	if it isn't a number then exit all -- allow the user to exit
 
	sort items 1 to it of currentList descending by the counter
end repeat
 
answer "You sorted it in " & numberOfTurns & " turns!" titled "Congratulations!"
 