Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Note you can generate the sequence by recursively generating all combinations with the first element, then all combinations without. In both recursive cases, you drop the first element to get all combinations from n-1 elements. In Python:</p> <pre><code>def combination(l, r): if r == 0: yield [] elif len(l) == r: yield l else: for c in (combination(l[1:], r-1)): yield l[0:1]+c for c in (combination(l[1:], r)): yield c </code></pre> <p>Any time you're generating a sequence by making a choice like this, you can recursively generate the k<sub>th</sub> element by counting how many elements a choice generates and comparing the count to k. If k is less than the count, you make that choice. Otherwise, subtract the count and repeat for the other possible choices you could make at that point. If there are always <code>b</code> choices, you can view this as generating a number in base <code>b</code>. The technique still works if the number of choices varies. In pseudocode (when all choices are always available):</p> <pre><code>kth(k, choicePoints) if choicePoints is empty return empty list for each choice in head of choicePoints: if k &lt; size of choice return choice and kth(k, tail of choicePoints) else k -= size of choice signal exception: k is out-of-bounds </code></pre> <p>This gives you a 0-based index. If you want 1-based, change the comparison to <code>k &lt;= size of choice</code>.</p> <p>The tricky part (and what is unspecified in the pseudocode) is that the size of a choice depends on previous choices. Note the pseudocode can be used to solve a more general case than the problem.</p> <p>For this specific problem, there are two choices (<code>b</code>= 2) and the size of the 1st choice (i.e. including the 1<sup>st</sup> element) is given by <sub>n-1</sub>C<sub>r-1</sub>. Here's one implementation (which requires a suitable <code>nCr</code>):</p> <pre><code>def kthCombination(k, l, r): if r == 0: return [] elif len(l) == r: return l else: i=nCr(len(l)-1, r-1) if k &lt; i: return l[0:1] + kthCombination(k, l[1:], r-1) else: return kthCombination(k-i, l[1:], r) </code></pre> <p>If you reverse the order of the choices, you reverse the order of the sequence. </p> <pre><code>def reverseKthCombination(k, l, r): if r == 0: return [] elif len(l) == r: return l else: i=nCr(len(l)-1, r) if k &lt; i: return reverseKthCombination(k, l[1:], r) else: return l[0:1] + reverseKthCombination(k-i, l[1:], r-1) </code></pre> <p>Putting it to use:</p> <pre><code>&gt;&gt;&gt; l = [6, 4, 2, 1] &gt;&gt;&gt; [kthCombination(k, [6, 4, 2, 1], 3) for k in range(nCr(len(l), 3)) ] [[6, 4, 2], [6, 4, 1], [6, 2, 1], [4, 2, 1]] &gt;&gt;&gt; powOf2s=[2**i for i in range(4,-1,-1)] &gt;&gt;&gt; [sum(kthCombination(k, powOf2s, 3)) for k in range(nCr(len(powOf2s), 3))] [28, 26, 25, 22, 21, 19, 14, 13, 11, 7] &gt;&gt;&gt; [sum(reverseKthCombination(k, powOf2s, 3)) for k in range(nCr(len(powOf2s), 3))] [7, 11, 13, 14, 19, 21, 22, 25, 26, 28] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload