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 "Given a jumbled list of the numbers 1 to 9,"
PRINT "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."
 
RANDOMIZE TIMER
 
DIM nums(1 TO 9) AS INTEGER
DIM L0 AS INTEGER, n AS INTEGER, flp AS INTEGER, tries AS INTEGER, again AS INTEGER
 
'initial values
FOR L0 = 1 TO 9
    nums(L0) = L0
NEXT
 
DO  'shuffle
    FOR L0 = 9 TO 2 STEP -1
        n = INT(RND * (L0)) + 1
        IF n <> L0 THEN SWAP nums(n), nums(L0)
    NEXT
    FOR L0 = 1 TO 8 'make sure it's not in order
        IF nums(L0) > nums(L0 + 1) THEN EXIT DO
    NEXT
LOOP
 
again = -1
DO
    IF tries < 10 THEN PRINT " ";
    PRINT tries; ":";
    FOR L0 = 1 TO 9
        PRINT nums(L0);
    NEXT
 
    IF NOT again THEN EXIT DO
 
    INPUT " -- How many numbers should be flipped"; flp
    IF flp < 0 THEN flp = 0
    IF flp > 9 THEN flp = 0
 
    FOR L0 = 1 TO (flp \ 2)
        SWAP nums(L0), nums(flp - L0 + 1)
    NEXT
 
    again = 0
    'check for order
    FOR L0 = 1 TO 8
        IF nums(L0) > nums(L0 + 1) THEN
            again = -1
            EXIT FOR
        END IF
    NEXT
 
    IF flp > 0 THEN tries = tries + 1
LOOP
 
PRINT : PRINT "You took "; LTRIM$(RTRIM$(STR$(tries))); " tries to put the digits in order."