Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><em>Permutations</em> are about taking an ordered set of things and moving these things around (i.e. changing order). Your question is about <em>combinations</em> of things from your list.</p> <p>Now, an easy way of enumerating combinations is by mapping entries from your list to bits in a number. For example, lets assume that if bit #0 is set (i.e. 1), then number <code>lst[0]</code> participates in the combination, if bit #1 is set, then <code>lst[1]</code> participates in the combination, etc. This way, numbers in range <code>0 &lt;= n &lt; 2**(len(lst))</code> identify all possible combinations of <code>lst</code> members, including an empty one (<code>n = 0</code>) and the whole <code>lst</code> (<code>n = 2**(len(lst)) - 1</code>).</p> <p>You need only combinations of 2 items or more, i.e. only those combination IDs that have at least two nonzero bits in their binary representation. Here is how to identify these:</p> <pre><code>def HasAtLeastTwoBitsSet(x) : return (x &amp; (x-1)) != 0 # Testing: &gt;&gt;&gt; [x for x in range(33) if HasAtLeastTwoBitsSet(x)] [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31] </code></pre> <p>Next step is to extract a combination of list members identified by a combination id. This is easy, thanks to the power of list comprehensions:</p> <pre><code>def GetSublistByCombination(lst, combination_id) : res = [x for (i,x) in enumerate(lst) if combination_id &amp; (1 &lt;&lt; i)] return res # Testing: &gt;&gt;&gt; GetSublistByCombination([0,1,2,3], 1) [0] &gt;&gt;&gt; GetSublistByCombination([0,1,2,3], 3) [0, 1] &gt;&gt;&gt; GetSublistByCombination([0,1,2,3], 12) [2, 3] &gt;&gt;&gt; GetSublistByCombination([0,1,2,3], 15) [0, 1, 2, 3] </code></pre> <p>Now let's make a generator that produces all sums, together with their string representations:</p> <pre><code>def IterAllSums(lst) : combinations = [i for i in range(1 &lt;&lt; len(lst)) if HasAtLeastTwoBitsSet(i)] for comb in combinations : sublist = GetSublistByCombination(lst, comb) sum_str = '+'.join(map(str, sublist)) sum_val = sum(sublist) yield (sum_str, sum_val) </code></pre> <p>And, finally, let's use it:</p> <pre><code>&gt;&gt;&gt; for sum_str, sum_val in IterAllSums([1,2,3,4]) : print sum_str, sum_val 1+2 3 1+3 4 2+3 5 1+2+3 6 1+4 5 2+4 6 1+2+4 7 3+4 7 1+3+4 8 2+3+4 9 1+2+3+4 10 </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