<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Seven steps &#187; algorithm</title>
	<atom:link href="http://trevoke.net/blog/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://trevoke.net/blog</link>
	<description>Martial arts and technology, $DEITY what a mix!</description>
	<lastBuildDate>Wed, 01 Feb 2012 03:43:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Inter-array permutations in Ruby</title>
		<link>http://trevoke.net/blog/2009/12/17/inter-array-permutations-in-ruby/</link>
		<comments>http://trevoke.net/blog/2009/12/17/inter-array-permutations-in-ruby/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 17:51:06 +0000</pubDate>
		<dc:creator>Trevoke</dc:creator>
				<category><![CDATA[food for thought]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://trevoke.net/blog/?p=193</guid>
		<description><![CDATA[I don&#8217;t really have a better name for this. It&#8217;s also not completely clean, but it works. I had, almost a year ago (362 days ago), written a blog post about lexicographic permutations. That was about permutations of elements within one array. Someone on ruby-forum asked about permutations between multiple arrays. I found something in <a href='http://trevoke.net/blog/2009/12/17/inter-array-permutations-in-ruby/'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t really have a better name for this. It&#8217;s also not completely clean, but it works. I had, almost a year ago (362 days ago), written a blog post about 
<a  href="http://trevoke.net/blog/2008/12/20/lexicographic-permutations-in-ruby" onclick="javascript:pageTracker._trackPageview('/external/trevoke.net/blog/2008/12/20/lexicographic-permutations-in-ruby');" >lexicographic permutations</a>. That was about permutations of elements within one array.<br />
Someone on ruby-forum asked about permutations between multiple arrays. I 
<a  href="http://stackoverflow.com/questions/710670/c-permutation-of-an-array-of-arraylists" onclick="javascript:pageTracker._trackPageview('/external/stackoverflow.com/questions/710670/c-permutation-of-an-array-of-arraylists');" >found something in C#</a>, which I was happy to transcribe to Ruby and tweak a little.</p>
<pre class="brush: ruby; title: ; notranslate">def array_permutations array, index=0
  # index is 0 by default : start at the beginning, more elegant.
  return array[-1] if index == array.size - 1 # Return last element if at end.
  result = []
  array[index].each do |element| # For each array
    array_permutations(array, index + 1).each do |x| # Permute permute permute
      result &lt;&lt; &quot;#{element}, #{x}&quot;
    end
  end
  return result
end</pre>
<p>So, we get this:</p>
<pre class="brush: ruby; title: ; notranslate">first = ['one', 'two']
second = ['three', 'four']
third = 'five', 'six']
result = array_permutations [first, second, third]
=&gt; [&quot;one, three, five&quot;, &quot;one, three, six&quot;, &quot;one, four, five&quot;, &quot;one, four, six&quot;, &quot;two, three, five&quot;, &quot;two, thre
e, six&quot;, &quot;two, four, five&quot;, &quot;two, four, six&quot;]</pre>
<p>Magic!</p>
<p>&#8212;&#8212;<br />
Edit &#8211; of course, my solution is hackish, and someone came up with a quicker and more elegant solution:</p>
<pre class="brush: ruby; title: ; notranslate">def fancy_array_permutation array
  return array[0] if array.size == 1
  first = array.shift
  return first.product( fancy_array_permutation(array) ).map {|x| x.flatten.join(&quot; &quot;)}
end</pre>
<p>This gives the same result as above.</p>
]]></content:encoded>
			<wfw:commentRss>http://trevoke.net/blog/2009/12/17/inter-array-permutations-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.255 seconds -->

