I have an array / list of numbers. Each number has a certain priority / importance.
I need an algorithm that generate all combinations of numbers, but begin form numbers with the most importance.
e.g. [number, priority]: [1,1], [2,3], [3,2]. Highest priority is 1.
Combinations:
1, 3, 2, 1 1, 1 3, 3 3, 3 1, 1 2, 3 2, 2 1, 2 2, 1 1 1, 1 1 3, 1 3 1...
Any idea how to do this?
Of course, I want to generate a certain number of combinations.
I changed my answer to an example code, this way you don’t even need a recursion. You have to sort first the elements by the priority. The example is in Perl, which is not so far from Pseudocode