mirror of
https://github.com/fleschutz/PowerShell.git
synced 2025-04-22 01:48:20 +02:00
Updated list-anagrams.ps1
This commit is contained in:
parent
df1ccce8f4
commit
340ce64c9d
@ -7,46 +7,43 @@
|
|||||||
# License: CC0
|
# License: CC0
|
||||||
|
|
||||||
param([string]$Word)
|
param([string]$Word)
|
||||||
|
$Columns = 8
|
||||||
|
|
||||||
Function Get-StringPermutation {
|
Function GetPermutations {
|
||||||
[cmdletbinding()]
|
[cmdletbinding()]
|
||||||
Param(
|
Param(
|
||||||
[parameter(ValueFromPipeline=$True)]
|
[parameter(ValueFromPipeline=$True)]
|
||||||
[string]$String = 'the'
|
[string]$String = 'the'
|
||||||
)
|
)
|
||||||
Begin {
|
Begin {
|
||||||
#region Internal Functions
|
Function NewAnagram { Param([int]$NewSize)
|
||||||
Function New-Anagram {
|
if ($NewSize -eq 1) {
|
||||||
Param([int]$NewSize)
|
|
||||||
If ($NewSize -eq 1) {
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
For ($i=0;$i -lt $NewSize; $i++) {
|
for ($i=0;$i -lt $NewSize; $i++) {
|
||||||
New-Anagram -NewSize ($NewSize - 1)
|
NewAnagram -NewSize ($NewSize - 1)
|
||||||
If ($NewSize -eq 2) {
|
if ($NewSize -eq 2) {
|
||||||
New-Object PSObject -Property @{
|
New-Object PSObject -Property @{
|
||||||
Permutation = $stringBuilder.ToString()
|
Permutation = $stringBuilder.ToString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Move-Left -NewSize $NewSize
|
MoveLeft -NewSize $NewSize
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Function Move-Left {
|
Function MoveLeft { Param([int]$NewSize)
|
||||||
Param([int]$NewSize)
|
|
||||||
$z = 0
|
$z = 0
|
||||||
$position = ($Size - $NewSize)
|
$position = ($Size - $NewSize)
|
||||||
[char]$temp = $stringBuilder[$position]
|
[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)] = $stringBuilder[$z]
|
||||||
}
|
}
|
||||||
$stringBuilder[($z-1)] = $temp
|
$stringBuilder[($z-1)] = $temp
|
||||||
}
|
}
|
||||||
#endregion Internal Functions
|
|
||||||
}
|
}
|
||||||
Process {
|
Process {
|
||||||
$size = $String.length
|
$size = $String.length
|
||||||
$stringBuilder = New-Object System.Text.StringBuilder -ArgumentList $String
|
$stringBuilder = New-Object System.Text.StringBuilder -ArgumentList $String
|
||||||
New-Anagram -NewSize $Size
|
NewAnagram -NewSize $Size
|
||||||
}
|
}
|
||||||
End {}
|
End {}
|
||||||
}
|
}
|
||||||
@ -54,8 +51,9 @@ Function Get-StringPermutation {
|
|||||||
try {
|
try {
|
||||||
if ($Word -eq "" ) {
|
if ($Word -eq "" ) {
|
||||||
$Word = read-host "Enter word"
|
$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
|
exit 0
|
||||||
} catch {
|
} catch {
|
||||||
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
Write-Error "ERROR in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
|
||||||
|
Loading…
Reference in New Issue
Block a user