<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title>Process Over Content - Latest Comments in Simple Permutations in Python and Ruby</title><link>http://abachman.disqus.com/</link><description></description><atom:link href="https://abachman.disqus.com/simple_permutations_in_python_and_ruby/latest.rss" rel="self"></atom:link><language>en</language><lastBuildDate>Thu, 10 Dec 2009 11:01:34 -0000</lastBuildDate><item><title>Re: Simple Permutations in Python and Ruby</title><link>http://blog.adambachman.org/2008/10/simple-permutations-in-python-and-ruby.html#comment-25446936</link><description>&lt;p&gt;this won't work if the list has duplicates... :(&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">blah</dc:creator><pubDate>Thu, 10 Dec 2009 11:01:34 -0000</pubDate></item><item><title>Re: Simple Permutations in Python and Ruby</title><link>http://blog.adambachman.org/2008/10/simple-permutations-in-python-and-ruby.html#comment-3397325</link><description>&lt;p&gt;*chuckle* I tried to look at simple output from the function (I'll be honest, I was doing Project Euler, too) to figure out how it worked and I realized, from the output, it was nesting lists. I also figured you could do the .flatten but I think that adds quite a bit of time to the computation. Your original bugged version found the answer to the Euler problem in 24.8 seconds or so, and I didn't bother waiting past a minute for the .flatten.&lt;/p&gt;&lt;p&gt;On the other hand, using &lt;br&gt;[element].concat (val)&lt;/p&gt;&lt;p&gt;got me a result in ~25 seconds (only two runs, so not conclusive). Since this is Ruby, this should work on most anything that's enumerable.. Even sets (just tested it), however worthless that may be on a set.&lt;/p&gt;&lt;p&gt;This, I must admit, is by far the most elegant permutation implementation I've found on the web. Do you mind if I keep it, add your name/handle/URL to the source and reuse / distribute as I see fit (Not that I expect to find too many people who need to permute in Ruby/Python, but who knows) ?&lt;/p&gt;&lt;p&gt;And, to answer, yes, this was very helpful. It's really the "plain" permutation system, but quite ruby-ified with a flavor of LISP ;-)&lt;/p&gt;&lt;p&gt;Thanks for your explanation!&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aldric</dc:creator><pubDate>Thu, 30 Oct 2008 16:30:11 -0000</pubDate></item><item><title>Re: Simple Permutations in Python and Ruby</title><link>http://blog.adambachman.org/2008/10/simple-permutations-in-python-and-ruby.html#comment-3394244</link><description>&lt;p&gt;&amp;gt; I'm a little curious about this bit :&lt;/p&gt;&lt;p&gt;&lt;code&gt;permutations(li.select() {|n| n != element})&lt;br&gt;    {|val| yield([element] &amp;lt;&amp;lt; val)}&lt;/code&gt;&lt;/p&gt;&lt;p&gt;I think you've got the &lt;code&gt;li.select()&lt;/code&gt; chunk.  I'm passing the list that was given as input after removing &lt;code&gt;element&lt;/code&gt;.  The block following the function call is handed down to the next level.  Since yield executes the block that was passed in, I'm asking the next level down to execute the block that was passed in to the current level.  The block that's passed down internally is different than the block that's passed externally, so we're dealing with two possible behaviors for the yield call.&lt;/p&gt;&lt;p&gt;The recursive call passes a new block to the next level down containing a reference to the block that was passed in to the current level, so every level under the top level (level 0, below) passes a block containing the accumulator function (&lt;code&gt;[element] &amp;lt;&amp;lt; val&lt;/code&gt;).  The internal yield call accumulates the "returned" values from the descendent calls (calls that are at deeper levels of recursion).  It may be easier to think of the yield calls in the Ruby and Python versions as almost like &lt;code&gt;return&lt;/code&gt; statements.  In Ruby, instead of doing something with the returned value outside the function, we pass a block of code into the function that we want to execute every time the function "returns" (hits a &lt;code&gt;yield&lt;/code&gt; statement).&lt;/p&gt;&lt;p&gt;Here's what the flow of the function looks like: &lt;br&gt;&lt;code&gt;&lt;br&gt;# level 0&lt;br&gt;=&amp;gt; permutations([1,2,3]) {|n| puts n}&lt;br&gt;    element = 1&lt;br&gt;    yield = {|n| print n}&lt;br&gt;# level 1&lt;br&gt;===&amp;gt; permutations([2,3]) {|val| yield#level0([1] &amp;lt;&amp;lt; val}&lt;br&gt;      element = 2&lt;br&gt;      yield = {|val| yield#level0([1] &amp;lt;&amp;lt; val)}&lt;br&gt;# level 2&lt;br&gt;=====&amp;gt; permutations([3]) {|val| yield#level1([2] &amp;lt;&amp;lt; val)}&lt;br&gt;        call([2] &amp;lt;&amp;lt; 3) &lt;br&gt;        return [2,3]&lt;br&gt;# level 1&lt;br&gt;===&amp;gt; (execute yield)&lt;br&gt;      call([1] &amp;lt;&amp;lt; [2,3]) # BUG!&lt;br&gt;# level 0&lt;br&gt;=&amp;gt; (execute yield)&lt;br&gt;    call(print [1, [2, 3]])    &lt;br&gt;# level 1&lt;br&gt;===&amp;gt; (continue li.each loop)&lt;br&gt;      element = 3&lt;br&gt;      yield {|val| yield#level0([1] &amp;lt;&amp;lt; val)}&lt;br&gt;# level 2&lt;br&gt;=====&amp;gt; permutations([2]) {|val| yield#level1([3] &amp;lt;&amp;lt; val)}&lt;br&gt;        call([3] &amp;lt;&amp;lt; 2)&lt;br&gt;        return [3,2]&lt;br&gt;# level 1&lt;br&gt;===&amp;gt; (execute yield)&lt;br&gt;      call([1] &amp;lt;&amp;lt; [3,2]) # BUG!&lt;br&gt;# level 0&lt;br&gt;=&amp;gt; (execute yield)&lt;br&gt;    call(print [1, [3, 2]])    &lt;br&gt;   (continue li.each loop)&lt;br&gt;    element = 2&lt;br&gt;    yield = {|n| print n}&lt;br&gt;&lt;br&gt;etc...&lt;br&gt;&lt;/code&gt;&lt;/p&gt;&lt;p&gt;Wow, that may be a lot more confusing than helpful, but I managed to spot a bug, so it's worthwhile to me at least :|.&lt;/p&gt;&lt;p&gt;&lt;code&gt;[a] &amp;lt;&amp;lt; b&lt;/code&gt; appends `b` to the list containing a single element, `a`.  The bug is that if `b` is a list, we end up with [1, [2, 3]].  You can change &lt;code&gt;{|val| yield([element] &amp;lt;&amp;lt; val)}&lt;/code&gt; to &lt;code&gt;{|val| &lt;br&gt;yield(([element] &amp;lt;&amp;lt; val).flatten)}&lt;/code&gt; to fix that.&lt;/p&gt;&lt;p&gt;I hope I have helped a little bit.  I at least hope I haven't driven you to despair.  You could try looking for an algorithm that generates permutations iteratively, instead of recursively, and code that in Ruby.  Then this recursive version might make more sense.&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">abachman</dc:creator><pubDate>Thu, 30 Oct 2008 13:50:42 -0000</pubDate></item><item><title>Re: Simple Permutations in Python and Ruby</title><link>http://blog.adambachman.org/2008/10/simple-permutations-in-python-and-ruby.html#comment-3392638</link><description>&lt;p&gt;Okay - funny, I've been looking for a good way to do permutations (had no clue how to even begin) and found your code (I'm teaching myself Ruby). It taught me quite a bit about co-routines and what on earth yield could be used for.. But I'm a little curious about this bit :&lt;br&gt;      permutations(&lt;a href="http://li.select" rel="nofollow noopener" target="_blank" title="li.select"&gt;li.select&lt;/a&gt;() {|n| n != element}) \&lt;br&gt;       {|val| yield([element] &amp;lt;&amp;lt; val)}&lt;/p&gt;&lt;p&gt;Now, what this is saying is ...&lt;br&gt;run the permutation function on every element in the list except the current one, and run that against ... What, exactly? What is  "yield(|element &amp;lt;&amp;lt; val)" ? I can't find a good way to put this in english..&lt;/p&gt;</description><dc:creator xmlns:dc="http://purl.org/dc/elements/1.1/">Aldric</dc:creator><pubDate>Thu, 30 Oct 2008 12:30:01 -0000</pubDate></item></channel></rss>