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


 
print "Dada una lista aleatoria de numeros del 1 al 9,"
print "indica cuantos digitos de la izquierda voltear."
print " El objetivo es obtener los digitos en orden "
print " con el 1 a la izquierda y el 9 a la derecha." + chr(10)
 
dim nums(10)
dim a(10)
intentos = 0: denuevo = true: colum = 6
 
#valores iniciales
for i = 1 to 9
	nums[i] = i
next i
 
do  #barajamos
	for i = 9 to 2 step -1
		n = int(rand * i) + 1
		if n <> i then
			a[i] = nums[i]
			nums[i] = nums[n]
			nums[n] = a[i]
		end if
	next i
	for i = 1 to 8 #nos aseguramos que no estén en orden
		if nums[i] > nums[i + 1] then exit do
	next i
until false
 
do
	if intentos < 10 then print " ";
	print intentos; ": ";
	for i = 1 to 9
		print nums[i];" ";
	next i
 
	if not denuevo then exit do
 
	input "  -- Cuantos volteamos ", volteo
	if volteo < 0 or volteo > 9 then volteo = 0
 
	for i = 1 to (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
			exit for
		end if
	next i
 
	if volteo > 0 then intentos += 1
until false
print chr(10) + chr(10) + "   Necesitaste "; intentos; " intentos."
end
 