D
David Palm
Hi,
I have a problem involving combinatorics and permutations and my brain hurts.
The problem: I have a hash of products in input. I then build an array of equivalent products for each input product. I end up with an array such as this:
{
:input_prod_1 => [1_1, 1_2, ... , p1_x],
:input_prod_2 => [2_1, 2_1, ..., p2_y],
...
:input_prod_m => [m_1, m_1, ..., pm_z]
}
I need all the unique combinations of each product from the first group with each of the second. For example:
given: [ [1, 2], [3, p4] ]
I need to obtain: [ [1, 3], [1, 4], [2, 3], [2, 4] ]
Given: [ [1], [2, 3, 4] ]
I need: [ [1, 2], [1, 3], [1, 4] ]
The number of input products is in the order of tens (max); the same goes for the number of equivalent products. This means the total number of partitions is pretty large and I need to weed out the equivalent ones soon/fast enough to reduce the total number.
I've been making attempts with the permutation gem all morning; looks like it's doing the "right thing", but frankly my maths skills are lacking and I can't really figure out how to use it (should I?).
Also: order is not important for the result arrays.
This is easy, right? :-/
I have a problem involving combinatorics and permutations and my brain hurts.
The problem: I have a hash of products in input. I then build an array of equivalent products for each input product. I end up with an array such as this:
{
:input_prod_1 => [1_1, 1_2, ... , p1_x],
:input_prod_2 => [2_1, 2_1, ..., p2_y],
...
:input_prod_m => [m_1, m_1, ..., pm_z]
}
I need all the unique combinations of each product from the first group with each of the second. For example:
given: [ [1, 2], [3, p4] ]
I need to obtain: [ [1, 3], [1, 4], [2, 3], [2, 4] ]
Given: [ [1], [2, 3, 4] ]
I need: [ [1, 2], [1, 3], [1, 4] ]
The number of input products is in the order of tens (max); the same goes for the number of equivalent products. This means the total number of partitions is pretty large and I need to weed out the equivalent ones soon/fast enough to reduce the total number.
I've been making attempts with the permutation gem all morning; looks like it's doing the "right thing", but frankly my maths skills are lacking and I can't really figure out how to use it (should I?).
Also: order is not important for the result arrays.
This is easy, right? :-/