How to resolve the algorithm Number reversal game step by step in the BBC BASIC programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Number reversal game step by step in the BBC BASIC programming language
Table of Contents
Problem Statement
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.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Number reversal game step by step in the BBC BASIC programming language
Source code in the bbc programming language
DIM list%(8), done%(8), test%(8)
list%() = 1, 2, 3, 4, 5, 6, 7, 8, 9
done%() = list%()
REM Shuffle:
REPEAT
FOR i% = 9 TO 2 STEP -1
SWAP list%(i%-1), list%(RND(i%)-1)
NEXT
test%() = list%() - done%()
UNTIL MOD(test%()) <> 0
REM Run the game:
tries% = 0
REPEAT
tries% += 1
FOR i% = 0 TO 8 : PRINT ; list%(i%) " " ; : NEXT
INPUT ": Reverse how many", n%
PROCreverse(list%(), n%)
test%() = list%() - done%()
UNTIL MOD(test%()) = 0
PRINT "You took " ; tries% " attempts."
END
DEF PROCreverse(a%(), n%)
IF n% < 2 ENDPROC
LOCAL i%
n% -= 1
FOR i% = 0 TO n% DIV 2
SWAP a%(i%), a%(n%-i%)
NEXT
ENDPROC
You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the Julia programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the SparForte programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the sed programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the Raku programming language
You may also check:How to resolve the algorithm Sorting algorithms/Sleep sort step by step in the Dart programming language