How to resolve the algorithm Comma quibbling step by step in the PowerShell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Comma quibbling step by step in the PowerShell programming language

Table of Contents

Problem Statement

Comma quibbling is a task originally set by Eric Lippert in his blog.

Write a function to generate a string output which is the concatenation of input words from a list/sequence where:

Test your function with the following series of inputs showing your output here on this page:

Note: Assume words are non-empty strings of uppercase characters for this task.

Let's start with the solution:

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

Source code in the powershell programming language

function Out-Quibble
{
    [OutputType([string])]
    Param
    (
        # Zero or more strings.
        [Parameter(Mandatory=$false, Position=0)]
        [AllowEmptyString()]
        [string[]]
        $Text = ""
    )

    # If not null or empty...
    if ($Text)
    {
        # Remove empty strings from the array.
        $text = "$Text".Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
    }
    else
    {
        return "{}"
    }

    # Build a format string.
    $outStr = ""
    for ($i = 0; $i -lt $text.Count; $i++)
    { 
        $outStr += "{$i}, "
    }
    $outStr = $outStr.TrimEnd(", ")

    # If more than one word, insert " and" at last comma position.
    if ($text.Count -gt 1)
    {
        $cIndex = $outStr.LastIndexOf(",")
        $outStr = $outStr.Remove($cIndex,1).Insert($cIndex," and")
    }

    # Output the formatted string.
    "{" + $outStr -f $text + "}"
}


Out-Quibble
Out-Quibble "ABC"
Out-Quibble "ABC", "DEF"
Out-Quibble "ABC", "DEF", "G", "H"


$file = @'

ABC
ABC, DEF
ABC, DEF, G, H
'@ -split [Environment]::NewLine

foreach ($line in $file)
{
    Out-Quibble -Text ($line -split ", ")
}


  

You may also check:How to resolve the algorithm Loops/Infinite step by step in the Occam programming language
You may also check:How to resolve the algorithm Prime decomposition step by step in the Perl programming language
You may also check:How to resolve the algorithm Calculating the value of e step by step in the Racket programming language
You may also check:How to resolve the algorithm Primality by Wilson's theorem step by step in the PROMAL programming language
You may also check:How to resolve the algorithm Feigenbaum constant calculation step by step in the Lua programming language