Added list-anagrams.ps1

This commit is contained in:
Markus Fleschutz
2020-12-22 10:42:32 +00:00
parent 5404722dc8
commit df1ccce8f4
4 changed files with 69 additions and 5 deletions

63
Scripts/list-anagrams.ps1 Executable file
View File

@ -0,0 +1,63 @@
#!/snap/bin/powershell
# Syntax: ./list-anagrams.ps1 [<word>]
# Description: lists all anagrams of the given word
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell
# License: CC0
param([string]$Word)
Function Get-StringPermutation {
[cmdletbinding()]
Param(
[parameter(ValueFromPipeline=$True)]
[string]$String = 'the'
)
Begin {
#region Internal Functions
Function New-Anagram {
Param([int]$NewSize)
If ($NewSize -eq 1) {
return
}
For ($i=0;$i -lt $NewSize; $i++) {
New-Anagram -NewSize ($NewSize - 1)
If ($NewSize -eq 2) {
New-Object PSObject -Property @{
Permutation = $stringBuilder.ToString()
}
}
Move-Left -NewSize $NewSize
}
}
Function Move-Left {
Param([int]$NewSize)
$z = 0
$position = ($Size - $NewSize)
[char]$temp = $stringBuilder[$position]
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
}
End {}
}
try {
if ($Word -eq "" ) {
$Word = read-host "Enter word"
}
Get-StringPermutation -String $Word | Format-Wide -Column 1
exit 0
} catch {
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}

View File

@ -1,6 +1,6 @@
#!/snap/bin/powershell
# Syntax: ./write-MOTD.ps1
# Syntax: ./write-motd.ps1
# Description: writes the message of the day (MOTD)
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell

View File

@ -1,6 +1,6 @@
#!/snap/bin/powershell
# Syntax: ./write-ROT13.ps1 [<text>]
# Syntax: ./write-rot13.ps1 [<text>]
# Description: encodes or decodes the given text with ROT13
# Author: Markus Fleschutz
# Source: github.com/fleschutz/PowerShell