Updated list-anagrams.ps1

This commit is contained in:
Markus Fleschutz 2020-12-22 10:53:15 +00:00
parent df1ccce8f4
commit 340ce64c9d

View File

@ -7,46 +7,43 @@
# License: CC0
param([string]$Word)
$Columns = 8
Function Get-StringPermutation {
Function GetPermutations {
[cmdletbinding()]
Param(
[parameter(ValueFromPipeline=$True)]
[string]$String = 'the'
)
Begin {
#region Internal Functions
Function New-Anagram {
Param([int]$NewSize)
If ($NewSize -eq 1) {
Function NewAnagram { Param([int]$NewSize)
if ($NewSize -eq 1) {
return
}
For ($i=0;$i -lt $NewSize; $i++) {
New-Anagram -NewSize ($NewSize - 1)
If ($NewSize -eq 2) {
for ($i=0;$i -lt $NewSize; $i++) {
NewAnagram -NewSize ($NewSize - 1)
if ($NewSize -eq 2) {
New-Object PSObject -Property @{
Permutation = $stringBuilder.ToString()
}
}
Move-Left -NewSize $NewSize
MoveLeft -NewSize $NewSize
}
}
Function Move-Left {
Param([int]$NewSize)
Function MoveLeft { Param([int]$NewSize)
$z = 0
$position = ($Size - $NewSize)
[char]$temp = $stringBuilder[$position]
For ($z=($position+1);$z -lt $Size; $z++) {
for ($z=($position+1);$z -lt $Size; $z++) {
$stringBuilder[($z-1)] = $stringBuilder[$z]
}
$stringBuilder[($z-1)] = $temp
}
#endregion Internal Functions
}
Process {
$size = $String.length
$stringBuilder = New-Object System.Text.StringBuilder -ArgumentList $String
New-Anagram -NewSize $Size
NewAnagram -NewSize $Size
}
End {}
}
@ -54,8 +51,9 @@ Function Get-StringPermutation {
try {
if ($Word -eq "" ) {
$Word = read-host "Enter word"
$Columns = read-host "Enter number of columns"
}
Get-StringPermutation -String $Word | Format-Wide -Column 1
GetPermutations -String $Word | Format-Wide -Column $Columns
exit 0
} catch {
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"