Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Looks like you need only one method</p> <pre><code> /// &lt;summary&gt; /// Gets all combinations (of a given size) of a given list, either with or without reptitions. /// &lt;/summary&gt; /// &lt;typeparam name="T"&gt;The type of the elements in the list.&lt;/typeparam&gt; /// &lt;param name="list"&gt;The list of which to get combinations.&lt;/param&gt; /// &lt;param name="action"&gt;The action to perform on each combination of the list.&lt;/param&gt; /// &lt;param name="resultSize"&gt;The number of elements in each resulting combination; or &lt;see langword="null"/&gt; to get /// premutations of the same size as &lt;paramref name="list"/&gt;.&lt;/param&gt; /// &lt;param name="withRepetition"&gt;&lt;see langword="true"/&gt; to get combinations with reptition of elements; /// &lt;see langword="false"/&gt; to get combinations without reptition of elements.&lt;/param&gt; /// &lt;exception cref="ArgumentNullException"&gt;&lt;paramref name="list"/&gt; is &lt;see langword="null"/&gt;. -or- /// &lt;paramref name="action"/&gt; is &lt;see langword="null"/&gt;.&lt;/exception&gt; /// &lt;exception cref="ArgumentException"&gt;&lt;paramref name="resultSize"/&gt; is less than zero.&lt;/exception&gt; /// &lt;remarks&gt; /// The algorithm performs combinations in-place. &lt;paramref name="list"/&gt; is however not changed. /// &lt;/remarks&gt; public static void GetCombinations&lt;T&gt;(IList&lt;T&gt; list, Action&lt;IList&lt;T&gt;&gt; action, int? resultSize = null, bool withRepetition = false) { if (list == null) throw new ArgumentNullException("list"); if (action == null) throw new ArgumentNullException("action"); if (resultSize.HasValue &amp;&amp; resultSize.Value &lt;= 0) throw new ArgumentException("error", "resultSize"); var result = new T[resultSize.HasValue ? resultSize.Value : list.Count]; var indices = new int[result.Length]; for (int i = 0; i &lt; indices.Length; i++) indices[i] = withRepetition ? -1 : indices.Length - i - 2; int curIndex = 0; while (curIndex != -1) { indices[curIndex]++; if (indices[curIndex] == (curIndex == 0 ? list.Count : indices[curIndex - 1] + (withRepetition ? 1 : 0))) { indices[curIndex] = withRepetition ? -1 : indices.Length - curIndex - 2; curIndex--; } else { result[curIndex] = list[indices[curIndex]]; if (curIndex &lt; indices.Length - 1) curIndex++; else action(result); } } } </code></pre> <p>Usage:</p> <pre><code>var x = new int[] { 1, 2, 3, 4, 5, 6 }; var combinationsResult = new List&lt;int&gt;(); GetCombinations&lt;int&gt;(x, (list =&gt; combinationsResult.Add(list.Sum())), limit, false); </code></pre> <p>Code taken from <a href="http://blog.noldorin.com/2010/05/combinatorics-in-csharp/" rel="nofollow">this beautiful blog</a></p>
 

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