How to resolve the algorithm Number reversal game step by step in the SETL programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Number reversal game step by step in the SETL 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 SETL programming language

Source code in the setl programming language

program number_reversal_game;
    setrandom(0);
    tries := 0;
    state := shuffled_numbers();

    loop until state = "123456789" do
        tries +:= 1;
        swapat := read_step(tries, state);
        state := reverse state(..swapat) + state(swapat+1..);
    end loop;
    print(state + " - You win in " + str tries + " tries.");

    proc read_step(tries, state);
        loop until r in [str d : d in [1..9]] do
            putchar(state + " - Reverse how many? ");
            flush(stdout);
            r := getline(stdin);
        end loop;
        return val r;
    end proc;

    proc shuffled_numbers();
        digits := "123456789";
        loop until out /= digits do
            dset := {d : d in digits};
            out := +/[[d := random dset, dset less:= d](1) : until dset = {}];
        end loop;
        return out;
    end proc;
end program;

  

You may also check:How to resolve the algorithm Averages/Root mean square step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Hash from two arrays step by step in the Common Lisp programming language
You may also check:How to resolve the algorithm Array length step by step in the Joy programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Egel programming language
You may also check:How to resolve the algorithm Runtime evaluation step by step in the Common Lisp programming language