Skip to content

Instantly share code, notes, and snippets.

@Yengas
Last active July 5, 2017 12:25
Show Gist options
  • Save Yengas/9010715 to your computer and use it in GitHub Desktop.
Save Yengas/9010715 to your computer and use it in GitHub Desktop.
Combinations of a multidimensional array

Explanation

This algorithm, from an array like this;

example = [["there"], ["is"], ["", "no"], ["urf level"]]

creates the output of; "there is urf level" and "there is no urf level".

Warning

If you understand ruby and just want to take a look at the code of this algorithm, you can find a gist on my github account where i used Linux Dictionary to unscramble a given scrambled phrase and used this algorithm to print every possible result.

Algorithm

After thinking in an algorithmic way, all we want to do is; getting all possible inner-array index combinations of this multidimensional array and concatenating the corresponding elements in the inner-arrays together and then putting them into an array which we will return as the output of this algorithm. To visualize this, all we are doing is;

# For a given array of lengths
# In our case, these are the lengths of our inner arrys. 
lengths = [1, 1, 2, 1]

# Get:
# example[0][0] example[1][0] example[2][0] example[3][0]
# example[0][0] example[1][0] example[2][1] example[3][0]

This is something fairly easy to do if you follow the bellow algorithm.

array = [["there"], ["is"], ["", "no"], ["urf level"]] # Multidimensional array to reference.

length = [1, 1, 2, 1] # Create an array of inner-arrys lengths.
counter = [0, 0, 0, 0] # Create an array of counters which has the same length as length array.

total = length[0] * length[1] * length[2] * length[3] # Calculate the total combinations.

(total).times{ # For total times...

	puts array[0][counter[0]] + array[1][counter[1]] + array[2][counter[2]] + array[3][counter[3]]
	# Find value of current combination by concatenating corresponding values of counters in the inner-arrays
	# The first output as expected would be: Thereisurf level (Since we had no blankspace while concatenating)

	# Then we increment the value of the counter so we go on to the next combination.

	for index in (counter.length - 1).downto(0) # From (counter array's length - 1) to 0

	if counter[index] + 1 < length[index] then # If counter index can be incremented
		counter[index] += 1; # Increment the counter index
		break; # Stop the incrementation/go to the next combination printing/incrementing.
	end

	counter[index] = 0; # Assign current counter index to zero and try incrementing the next counter index. 

end

}

If you got a little bit confused with the inner for loop, all we're doing is starting from last counter index and stoping at the 0; Increment the counter index if it wont exceed or be equal to the corresponding length index, if it does exceed set it to 0. So this inner loop achives incrementation from 0000 to 0010.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment