Last active
March 31, 2024 19:45
-
-
Save AfroThundr3007730/ffaaf3d8b8f5a748e3d341c0c042b8df to your computer and use it in GitHub Desktop.
Gets recursive unique combinations of characters in a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-StringPermutations { | |
<# .SYNOPSIS | |
Calculates the permutations of an input string #> | |
[Alias('permutate')] | |
Param( | |
# String to calculate permutations from | |
[Parameter(Mandatory)] | |
[String]$String, | |
# Return only unique permutations | |
[Parameter()] | |
[Switch]$Unique | |
) | |
function _Permutate($String, $Stub = '') { | |
if ($String.length -eq 2) { | |
return ($Stub + $String[0] + $String[1]), ($Stub + $String[1] + $String[0]) | |
} | |
for ($i = 0; $i -lt $String.Length; $i++) { | |
_Permutate -String $String.remove($i, 1) -Stub ($Stub + $String[$i]) | |
} | |
} | |
if ($String.length -eq 1) { return $String } | |
if (!$Unique) { _Permutate -String $String } else { | |
[Collections.Generic.HashSet[string]]::new([string[]](_Permutate -String $String)) | |
} | |
} | |
$needs_work = @' | |
function Get-StringPermutations { | |
<# .SYNOPSIS | |
Calculates the permutations of an input string #> | |
[Alias('permutate')] | |
Param( | |
# String to calculate permutations from | |
[Parameter(Mandatory)] | |
[String]$String, | |
# Return only unique permutations | |
[Parameter()] | |
[Switch]$Unique | |
) | |
function _Permutate($String, $Stub, $Level) { | |
for ($i = 0; $i -lt $String.Length; $i++) { | |
if ($Level -eq 1) { return $Stub + $String[$i] } | |
_Permutate -String $String.remove($i, 1) -Stub ($Stub + $String[$i]) -Level ($Level - 1) | |
} | |
} | |
function _UPermutate($String, $Stub, $Level) { | |
$Pool = [Collections.Generic.HashSet[char]]::new($String.length) | |
foreach ($i in (0..($String.length - 1) | & { process { if ($Pool.add([char]$String[$_])) { $_ } } })) { | |
if ($Level -eq 1) { return $Stub + $String[$i] } | |
_UPermutate -String $String.remove($i, 1) -Stub ($Stub + $String[$i]) -Level ($Level - 1) | |
} | |
} | |
# function _UPermutate($String, $Stub, $Level) { | |
# foreach ($i in [Collections.Generic.HashSet[char]]::new([char[]]$String)) { | |
# if ($Level -eq 1) { return $Stub + $i } | |
# _UPermutate -String $String.remove($String.LastIndexOf($i), 1) -Stub ($Stub + $i) -Level ($Level - 1) | |
# } | |
# } | |
if (!$Unique) { _Permutate -String $String -Stub '' -Level $String.length } | |
else { _UPermutate -String $String -Stub '' -Level $String.length } | |
} | |
'@ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Implement runspaces for parallelization of large jobs
Notes: