How to resolve the algorithm Permutations step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Permutations step by step in the PowerShell programming language

Table of Contents

Problem Statement

Write a program that generates all   permutations   of   n   different objects.   (Practically numerals!)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Permutations step by step in the PowerShell programming language

Source code in the powershell programming language

function permutation ($array) {
    function generate($n, $array, $A) {
        if($n -eq 1) {
            $array[$A] -join ' '
        }
        else{
            for( $i = 0; $i -lt ($n - 1); $i += 1) {
                generate ($n - 1) $array $A
                if($n % 2 -eq 0){
                    $i1, $i2 = $i, ($n-1)
                    $A[$i1], $A[$i2] = $A[$i2], $A[$i1]
                }
                else{
                    $i1, $i2 = 0, ($n-1)
                    $A[$i1], $A[$i2] = $A[$i2], $A[$i1]
                }
            }
            generate ($n - 1) $array $A
        }
    }
    $n = $array.Count
    if($n -gt 0) {
        (generate $n $array (0..($n-1)))
    } else {$array}
}
permutation @('A','B','C')

  

You may also check:How to resolve the algorithm Word wrap step by step in the Commodore BASIC programming language
You may also check:How to resolve the algorithm Delegates step by step in the Sidef programming language
You may also check:How to resolve the algorithm Execute a system command step by step in the Rust programming language
You may also check:How to resolve the algorithm Strip comments from a string step by step in the Wren programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Pascal programming language