How to resolve the algorithm Guess the number step by step in the jq programming language

Published on 12 May 2024 09:40 PM
#Jq

How to resolve the algorithm Guess the number step by step in the jq programming language

Table of Contents

Problem Statement

Write a program where the program chooses a number between   1   and   10. A player is then prompted to enter a guess.   If the player guesses wrong,   then the prompt appears again until the guess is correct. When the player has made a successful guess the computer will issue a   "Well guessed!"   message,   and the program exits. A   conditional loop   may be used to repeat the guessing until the user is correct.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Guess the number step by step in the jq programming language

Source code in the jq programming language

{prompt: "Please enter your guess:", random: (1+rand(9)) }
| ( (while( .guess != .random; .guess = (input|tonumber) ) | .prompt),
  "Well done!"

# LCG::Microsoft generates 15-bit integers using the same formula
# as rand() from the Microsoft C Runtime.
# Input: [ count, state, random ]
def next_rand_Microsoft:
  .[0] as $count
  | ((214013 * .[1]) + 2531011) % 2147483648 # mod 2^31
  | [$count+1 , ., (. / 65536 | floor) ];

def rand_Microsoft(seed):
  [0,seed]
  | next_rand_Microsoft  # the seed is not so random
  | next_rand_Microsoft | .[2]; 

# A random integer in [0 ... (n-1)]:
def rand(n): n * (rand_Microsoft($seed|tonumber) / 32768) | trunc;

  

You may also check:How to resolve the algorithm 9 billion names of God the integer step by step in the Julia programming language
You may also check:How to resolve the algorithm Luhn test of credit card numbers step by step in the Elixir programming language
You may also check:How to resolve the algorithm Sudoku step by step in the Haskell programming language
You may also check:How to resolve the algorithm Exponentiation operator step by step in the E programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the XLISP programming language