<?xml version="1.0" encoding="UTF-8"?><!-- generator="wordpress/2.2.3" -->
<rss version="2.0" 
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	>
<channel>
	<title>Comments on: Creating Combinations of Sets/Arrays/Things in Ruby</title>
	<link>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/</link>
	<description>Semantic Web, Ruby on Rails, and Massive Data</description>
	<pubDate>Fri, 21 Nov 2008 00:06:23 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.2.3</generator>

	<item>
		<title>By: Brian Adkins</title>
		<link>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-233</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Mon, 10 Sep 2007 18:12:30 +0000</pubDate>
		<guid>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-233</guid>
		<description>&lt;p&gt;BTW you can combine your first and third line since they both have 'k==0' and both return [[]]&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>BTW you can combine your first and third line since they both have &#8216;k==0&#8242; and both return [[]]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-232</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Mon, 10 Sep 2007 18:10:05 +0000</pubDate>
		<guid>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-232</guid>
		<description>&lt;p&gt;boy that looks bad! Maybe you can wrap the code with &#60;pre&#62;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>boy that looks bad! Maybe you can wrap the code with &lt;pre&gt;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Brian Adkins</title>
		<link>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-231</link>
		<dc:creator>Brian Adkins</dc:creator>
		<pubDate>Mon, 10 Sep 2007 18:09:22 +0000</pubDate>
		<guid>http://www.semergence.com/2007/03/27/creating-combinations-of-setsarraysthings-in-ruby/#comment-231</guid>
		<description>&lt;p&gt;Here's an alternative version. I'm still not real pleased with it. I don't like the inefficiency of slicing the array repeatedly, but probably the only way to avoid that is to use a set of indices to navigate through the array which would clutter things up.&lt;/p&gt;

Return all combinations of n elements from array

combinations([1,2,3],2) -&#62; [[1,2],[1,3],[2,3]]

&lt;p&gt;def combinations array, n
  result = []
  if n &#62; 0
    (0 .. array.length - n).each do &#124;i&#124;
      combs = combinations(array[i + 1 .. -1], n - 1)
      combs = [[]] if combs.empty?
      combs.collect {&#124;comb&#124; [array[i]] + comb}.each {&#124;x&#124; result &#60;&#60; x}
    end
  end
  return result
end&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Here&#8217;s an alternative version. I&#8217;m still not real pleased with it. I don&#8217;t like the inefficiency of slicing the array repeatedly, but probably the only way to avoid that is to use a set of indices to navigate through the array which would clutter things up.</p>
<p>Return all combinations of n elements from array</p>
<p>combinations([1,2,3],2) -&gt; [[1,2],[1,3],[2,3]]</p>
<p>def combinations array, n<br />
  result = []<br />
  if n &gt; 0<br />
    (0 .. array.length - n).each do |i|<br />
      combs = combinations(array[i + 1 .. -1], n - 1)<br />
      combs = [[]] if combs.empty?<br />
      combs.collect {|comb| [array[i]] + comb}.each {|x| result &lt;&lt; x}<br />
    end<br />
  end<br />
  return result<br />
end</p>
]]></content:encoded>
	</item>
</channel>
</rss>
