There is such a task: There are several arrays of possible numbers. There may be 2 such arrays, maybe 10, it does not matter. Also the size of arrays can be different. The task is as follows: it is necessary to randomly generate all non-repeating combinations of numbers from these arrays. For example,

const first : number[]= [1,2]; const second : number[] = [2,3,4]; const third : number[] = [3,4,5,6]; 

The task is as follows: it is necessary to generate non-repeating combinations of one number from each array. Those.

 (1,2,5), (2,3,4), (1,4,6), (2,3,5) и т.д. 

Additional and mandatory limitation. When choosing a number from an array, we do not need to know which other two numbers lie inside this combination. Those. the algorithm must be stateless, i.e. stateless. Those. for example, the algorithm generated two combinations (1,2,5) and (2,3,4). And after the generation (2,3,4), the algorithm should not check whether such a combination was before (he does not and cannot do it, because he does not have a previous state).

Is it possible to solve this problem?

  • It seems that boost has functions for generating all combinations, permutations and placements. In short, this problem can be solved and it has already been solved. I didn’t really understand about stateless, but I think that it is also possible. - pepsicoca1 1:06 pm
  • @ pepsicoca1, I would have a solution on TypeScript, if possible. By stateless, you mean that you do not know what was generated before the current generation. Here is the current generation of a set of numbers and it should not depend on previous generations - Nick Laptev
  • About TypeScript I will not say anything since I work with C / C ++. But I think that has also been done for a long time. - pepsicoca1
  • @ pepsicoca1, the difficulty of the task is that there are essentially no permanent ones in it. The contents of the arrays of numbers may be different, the number of arrays is also unlimited, there is no saving the history of previous generations, so it seems to me that the task from the number of difficult ones is Nick Laptev
  • It is impossible to make this completely stateless - Zergatul pm

1 answer 1

The task looks completely trivial. We simply create exactly as many nested loops as arrays are given: by the first, by the second and by the third, and so on. array - and ready generator combinations. Everything. No checks on whether this combination was already needed are not needed. What for?

The stateless requirement is not entirely clear. If you need to generate such combinations not all at once, but by external requests, while not saving any state between requests, then by the previous combination you can always build the following simply by searching for elements in arrays and thereby restoring the state of iterations (implying that the input arrays do not have duplicate elements).

Of course, something will have to be given to the input of a stateless generator, i.e. for example, the previous combination. There cannot be a generator of a deterministic sequence that requires nothing to enter and does not store state.