How to resolve the algorithm Number reversal game step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Number reversal game step by step in the jq 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 jq programming language
Source code in the jq programming language
# Input: the initial array
def play:
def sorted: . == sort;
def reverse(n): (.[0:n] | reverse) + .[n:];
def prompt: "List: \(.list)\nEnter a pivot number: ";
def report: "Great! Your score is \(.score)";
{list: ., score: 0}
| (if .list | sorted then "List was sorted to begin with."
else
prompt,
( label $done
| foreach inputs as $n (.;
.list |= reverse($n) | .score +=1;
if .list | sorted then report, break $done else prompt end ))
end);
[1,2,3,9,8,7,6,5,4] | play
You may also check:How to resolve the algorithm Mouse position step by step in the Delphi programming language
You may also check:How to resolve the algorithm Write float arrays to a text file step by step in the C programming language
You may also check:How to resolve the algorithm Eban numbers step by step in the Lua programming language
You may also check:How to resolve the algorithm Introspection step by step in the Lasso programming language
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Delphi programming language