I’m having trouble solving this problem:
Create a function that given a character set C, can generate the Nth combination OR return the series of combination given a starting position (Ns) and ending position (Ne) and the maximum length of the combination (Mx).
A concrete example:
Let C = [A,B,C]
We know that different combinations would look like the following assuming Mx = 3 (the combination would be different for different lengths):
1. AAA
2. AAB
3. AAC
4. ABA
5. ABB
6. ABC
N. ... Etc
If we was to pass the following parameters :
C = [A,B,C] Mx = 3 Ns = 3 Ne = 3
we would expect the following result:
AAC
If we was to pass the following parameters :
C = [A,B,C] Mx = 3 Ns = 4 Ne = 6
we would expect the following result:
4. ABA
5. ABB
6. ABC
For the solution, the programming language is not relevant. However C# would be preferred. Also most important would be an explanation of how its solved.
I look forward to the amazing Guru’s of Stack Overflow…
Given an index N (0-based) into the sequence of combinations of n symbols, you can get the i‘th symbol by calculating N / ni % n (using integer division and remainder)
For example:
The sequence is treated as a base-n number, and the individual digits are calculated.