How to resolve the algorithm Number reversal game step by step in the Egel programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Number reversal game step by step in the Egel 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 Egel programming language
Source code in the egel programming language
import "prelude.eg"
import "io.ego"
import "random.ego"
using System
using IO
using List
using Math
def swap =
[ (I J) XX -> insert I (nth J XX) (insert J (nth I XX) XX) ]
def shuffle =
[ XX ->
let INDICES = reverse (fromto 0 ((length XX) - 1)) in
let SWAPS = map [ I -> I (between 0 I) ] INDICES in
foldr [I J -> swap I J] XX SWAPS ]
def prompt =
[ XX TURN ->
let _ = print TURN ". " in
let _ = map [ X -> print X " " ] XX in
let _ = print " : " in
toint getline ]
def game =
[ GOAL SHUFFLE TURN ->
if SHUFFLE == GOAL then
let _ = print "the goal was " in
let _ = map [ X -> print X " " ] GOAL in
print "\nit took you " TURN " turns\n"
else
let N = prompt SHUFFLE TURN in
let YY = (reverse (take N SHUFFLE)) ++ (drop N SHUFFLE) in
game GOAL YY (TURN + 1) ]
def main =
let XX = fromto 1 9 in game XX (shuffle XX) 0
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the BCPL programming language
You may also check:How to resolve the algorithm Kaprekar numbers step by step in the Perl programming language
You may also check:How to resolve the algorithm Magic squares of doubly even order step by step in the AWK programming language
You may also check:How to resolve the algorithm Deepcopy step by step in the Lingo programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Ecstasy programming language