Last active
August 31, 2015 12:44
-
-
Save DBremen/a7209498dbaacb1ef951 to your computer and use it in GitHub Desktop.
Returns the cartesian product for an array of arrays
This file contains 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
#build the cartesian product for an array of arrays | |
function CartesianProduct($row, $currCol=0){ | |
if ($currCol -eq 0){ | |
$wordIndices = New-Object int[] $row.Length | |
} | |
$wordIndex = 0 | |
#walk through the items in the current column | |
foreach($word in $row[$currCol]){ | |
#add the index to the indices for the current column | |
$wordIndices[$currCol] = $wordIndex | |
$wordIndex++ | |
#if we reach the end of the row | |
if ($currCol -eq ($row.Length - 1)) { | |
$cartesianSet = @() | |
$colIndex = 0 | |
foreach($column in $row){ | |
#add the items to the result set based on the collected indices | |
$cartesianSet += $row[$colIndex][$wordIndices[$colIndex]] | |
$colIndex++ | |
} | |
$cartesianSet -join ',' | |
} | |
#do this for every column | |
else { | |
CartesianProduct $row ($currCol + 1) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment