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


100 PROGRAM "Reversal.bas"
110 RANDOMIZE 
120 NUMERIC NR(1 TO 9)
130 LET TRIES=0
140 TEXT :PRINT "Given a jumbled list of the numbers 1 to 9, you must select how many digits from the left to reverse.":PRINT "Your goal is to get the digits in order with 1 on the left and 9 on the right.":PRINT 
150 FOR I=1 TO 9
160   LET NR(I)=I
170 NEXT
180 DO
190   FOR I=2 TO 9
200     LET N=RND(I)+1
210     IF N<>I THEN CALL SWAP(N,I)
220   NEXT
230 LOOP WHILE ORDERED
240 DO
250   PRINT USING "##: ":TRIES;
260   FOR I=1 TO 9
270     PRINT NR(I);
280   NEXT
290   PRINT
300   IF ORDERED THEN EXIT DO
310   DO
320     INPUT PROMPT "How many numbers should be flipped? ":FLIP
330   LOOP WHILE FLIP<2 OR FLIP>9
340   FOR I=1 TO FLIP/2
350     CALL SWAP(I,FLIP-I+1)
360   NEXT 
370   LET TRIES=TRIES+1
380 LOOP
390 PRINT :PRINT "You took";TRIES;"tries to put the digits in order."
400 DEF SWAP(A,B)
410   LET T=NR(A):LET NR(A)=NR(B):LET NR(B)=T
420 END DEF
430 DEF ORDERED
440   LET ORDERED=-1
450   FOR J=1 TO 8
460     IF NR(J)>NR(J+1) THEN LET ORDERED=0:EXIT FOR
470   NEXT
480 END DEF