I’ve been making a lot of progress on a problem I’ve been trying to solve lately, but I could use some advice on a particular ordering problem. I’m trying to find a way to represent the lexicographical order of a list of numbers, each having it’s own range (defined before ordering is a concern).
Each list has 7 elements. Each element can have a range of anywhere from 0 to 0-3. Perhaps a concrete example will help.
I have an array with 7 elements [2, 1, 0, 1, 3, 2, 3]. This list abstractly represents a number of possible lists for which I want to generate a lexicographical order. (Edit: To be more clear. The values at each digit represent the maximum value that that digit can be in the set of possible lists. As such, the first digit in the example could be thought of as a base 4 number, the second as a base 2 number, the third as a base 1 number, etc.) The first few elements to this list would be as follows:
- [0, 0, 0, 0, 0, 0, 0]
- [1, 0, 0, 0, 0, 0, 0]
- [2, 0, 0, 0, 0, 0, 0]
- [0, 1, 0, 0, 0, 0, 0]
- [1, 1, 0, 0, 0, 0, 0]
- [2, 1, 0, 0, 0, 0, 0]
- [0, 0, 0, 1, 0, 0, 0]
- [1, 0, 0, 1, 0, 0, 0]
Hopefully the pattern is clear. I would then like to be able to efficiently call a function f(m) which returns the mth value in this sequence. I found this article which feels like it gets really close to what I’m looking for (it provides an efficient way to get the lexicographical mth place in a set with fixed value combinations), but I’m having trouble bridging the gap between the two ideas (although I have recreated the results in the article and I believe I understand them somewhat).
Anyone have any ideas on how to create a function f(m) which returns the mth value in a sequence defined by a 7 element list similar to the one provided in the above example?
P.S. I apologize if this problem has been restated in some other form and I have not found it. I have done significant searching, but nothing seems to quite map back on to this. Links, brainstorming, general ideas are all welcome!
Edit 2: Fixed the error in my example where the first element was 3 instead of 2.
Edit: Replaced the original answer with the far more straightforward conversion. Check the earlier revision if you really want to see the backward solution.
Note that the initial array you give does not match the sample list (the first element of the array should be 2). I’ll use the corrected array in my answer.Given an input array [2, 1, 0, 1, 3, 2, 3], the number of possible elements at each position is,
giving us 3*2*1*2*4*3*4 = 576 possibilities.
To determine leftmost (low order) element the value of the mth member of the list, where 0 <= m < 576, we need to find the remainder of m/3, or m mod 3. The next element would be (m div 3) mod 2, and so on. In pseudocode, then:
If you adjust your sample list above to start at 0 you should get the same results. Here’s a sample calculation using A = [3,2,1,2,4,3,4] and m = 11:
So the 11th list element (zero-based) is [2,1,0,1,0,0,0].