mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-06-30 20:50:02 +02:00
Added list-anagrams.ps1
This commit is contained in:
63
Scripts/list-anagrams.ps1
Executable file
63
Scripts/list-anagrams.ps1
Executable 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
|
||||
}
|
@ -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
|
@ -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
|
Reference in New Issue
Block a user