Skip to content

Instantly share code, notes, and snippets.

@DBremen
Last active August 31, 2015 12:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DBremen/a7209498dbaacb1ef951 to your computer and use it in GitHub Desktop.
Save DBremen/a7209498dbaacb1ef951 to your computer and use it in GitHub Desktop.
Returns the cartesian product for an array of arrays
#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