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

Published on 12 May 2024 09:40 PM

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

Source code in the powershell programming language

#adding the below function to the previous users submission to prevent the small
#chance of getting an array that is in ascending order.

#Full disclosure: I am an infrastructure engineer, not a dev. My code is likely 
#bad.

function generateArray{
    $fArray = 1..9 | Get-Random -Count 9
    if (-join $fArray -eq -join @(1..9)){
        generateArray
    }
    return $fArray
}
$array = generateArray

#everything below is untouched from original submission
$nTries = 0
While(-join $Array -ne -join @(1..9)){
    $nTries++
    $nReverse = Read-Host -Prompt "[$Array] -- How many digits to reverse? "
    [Array]::Reverse($Array,0,$nReverse)
}
"$Array"
"Your score: $nTries"


  

You may also check:How to resolve the algorithm Left factorials step by step in the AWK programming language
You may also check:How to resolve the algorithm Guess the number/With feedback step by step in the Sidef programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the TI-83 BASIC programming language
You may also check:How to resolve the algorithm Undefined values step by step in the C programming language
You may also check:How to resolve the algorithm Sieve of Eratosthenes step by step in the Scala programming language