How to resolve the algorithm Number reversal game step by step in the PARI/GP programming language

Published on 12 May 2024 09:40 PM

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

Source code in the pari/gp programming language

game()={
  my(v=numtoperm(9,random(9!-1)),score,in,t); \\ Create vector with 1..9, excluding the one sorted in ascending order
  while(v!=vecsort(v),
    print(concat(concat([""],v)));
    in=input();
    for(i=0,in\2-1,
      t=v[9-i];
	  v[9-i]=v[10-in+i];
	  v[10-in+i]=t
    );
    score++
  );
  score
};

  

You may also check:How to resolve the algorithm Accumulator factory step by step in the zkl programming language
You may also check:How to resolve the algorithm 2048 step by step in the C# programming language
You may also check:How to resolve the algorithm Arbitrary-precision integers (included) step by step in the Lua programming language
You may also check:How to resolve the algorithm Amb step by step in the Mercury programming language
You may also check:How to resolve the algorithm Singly-linked list/Element definition step by step in the Factor programming language