I
I
Ilya2019-04-16 19:02:20
Algorithms
Ilya, 2019-04-16 19:02:20

How to sort through all possible combinations?

Good day.
Please tell me how to implement this algorithm.
At the input there are 2 arrays of objects, for example:

var array1 = [obj_1, obj_2, obj_3, obj_4],
array2 = [obj_5, obj_6, obj_7];

At the output, you need to get an array of S elements, where S is the number of placements without repetitions from n (the number of elements of the array1 array) to k (the number of elements of the array2 array), i.e. in this particular example S = n!/(nk)! = 4!/(4-3)! = 24.
Each element of the output array will contain an associative array, which for each element of the array1 array will match an element from the array2 array. The result should start like this:
1: {
obj_1 => obj_5,
obj_2 => obj_6,
obj_3 => obj_7
},
2: {
obj_1 => obj_5,
obj_2 => obj_6,
obj_4 => obj_7
},
3: {
obj_1 => obj_5,
obj_3 => obj_6,
obj_4 => obj_7
},
...,
24: {
...
}

Well, and so on. I don't understand how to sort it all out. There should be no repetition of objects for each plan. Thank you.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question