<?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; algorithms</title>
	<atom:link href="http://trevoke.net/blog/tag/algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://trevoke.net/blog</link>
	<description>Martial arts and technology, $DEITY what a mix!</description>
	<lastBuildDate>Tue, 11 May 2010 20:52:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Random constrained permutations in Ruby</title>
		<link>http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/</link>
		<comments>http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 18:50:20 +0000</pubDate>
		<dc:creator>Trevoke</dc:creator>
				<category><![CDATA[ruby]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://trevoke.net/blog/?p=199</guid>
		<description><![CDATA[Look, Ma, these are my baby steps in algorithms! # list is the elements to be permuted # y is the number of results desired # z is the number of elements per result # equalizer keeps track of who got used how many times def constrained_permutations list, y, z list.uniq! # Never trust the [...]]]></description>
			<content:encoded><![CDATA[<p>Look, Ma, these are my baby steps in algorithms!</p>
<pre class="brush: ruby;"># list is the elements to be permuted
# y is the number of results desired
# z is the number of elements per result
# equalizer keeps track of who got used how many times
def constrained_permutations list, y, z
  list.uniq! # Never trust the user. We want no repetitions.
  equalizer = {}
  list.each { |element| equalizer[element] = 0 }

  results = []
  # Do this until we get as many results as desired
  while results.size &lt; y
    pool = []
    puts pool
    least_used = equalizer.each_value.min
    # Find how used the least used element was
    while pool.size &lt; z
      # Do this until we have enough elements in this resultset
      element = nil
      while element.nil?
        # If we run out of &quot;least used elements&quot;, then we need to increment
        # our definition of &quot;least used&quot; by 1 and keep going.
        element = list.shuffle.find do |x|
          !pool.include?(x) &amp;&amp; equalizer[x] == least_used
        end
        least_used += 1 if element.nil?
      end
      equalizer[element] += 1
      # This element has now been used one more time.
      pool &lt;&lt; element
    end
    results &lt;&lt; pool
  end
  return results
end

constrained_permutations [0,1,2,3,4,5,6], 6, 2
=&gt; [[4, 0], [1, 3], [2, 5], [6, 0], [2, 5], [3, 6]]
constrained_permutations [0,1,2,3,4,5,6], 6, 2
=&gt; [[4, 5], [6, 3], [0, 2], [1, 6], [5, 4], [3, 0]]
</pre>
<!-- Social Bookmarking Reloaded BEGIN --><div class="social_bookmark"><em>Send to:</em><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/&amp;title=Random+constrained+permutations+in+Ruby" title="Add 'Random constrained permutations in Ruby' to digg"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/digg.png" title="Add 'Random constrained permutations in Ruby' to digg" alt="Add 'Random constrained permutations in Ruby' to digg" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/&amp;title=Random+constrained+permutations+in+Ruby" title="Add 'Random constrained permutations in Ruby' to reddit"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/reddit.png" title="Add 'Random constrained permutations in Ruby' to reddit" alt="Add 'Random constrained permutations in Ruby' to reddit" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/" title="Add 'Random constrained permutations in Ruby' to Technorati"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/technorati.png" title="Add 'Random constrained permutations in Ruby' to Technorati" alt="Add 'Random constrained permutations in Ruby' to Technorati" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/&amp;h=Random+constrained+permutations+in+Ruby" title="Add 'Random constrained permutations in Ruby' to Newsvine"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/newsvine.png" title="Add 'Random constrained permutations in Ruby' to Newsvine" alt="Add 'Random constrained permutations in Ruby' to Newsvine" /></a><br /><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/&amp;title=Random+constrained+permutations+in+Ruby" title="Add 'Random constrained permutations in Ruby' to Stumble Upon"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/stumbleupon.png" title="Add 'Random constrained permutations in Ruby' to Stumble Upon" alt="Add 'Random constrained permutations in Ruby' to Stumble Upon" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?title=Random+constrained+permutations+in+Ruby&amp;url=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/" title="Add 'Random constrained permutations in Ruby' to SlashDot"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/slashdot.png" title="Add 'Random constrained permutations in Ruby' to SlashDot" alt="Add 'Random constrained permutations in Ruby' to SlashDot" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/share.php?u=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/&amp;t=Random+constrained+permutations+in+Ruby" title="Add 'Random constrained permutations in Ruby' to FaceBook"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/facebook.png" title="Add 'Random constrained permutations in Ruby' to FaceBook" alt="Add 'Random constrained permutations in Ruby' to FaceBook" /></a><a class="social_img" onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,border=0,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.myspace.com/Modules/PostTo/Pages/?t=Random+constrained+permutations+in+Ruby&amp;c=http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/" title="Add 'Random constrained permutations in Ruby' to MySpace"><img src="http://trevoke.net/blog/wp-content/plugins/social-bookmarking-reloaded/myspace.png" title="Add 'Random constrained permutations in Ruby' to MySpace" alt="Add 'Random constrained permutations in Ruby' to MySpace" /></a></div>
<!-- Social Bookmarking Reloaded END -->]]></content:encoded>
			<wfw:commentRss>http://trevoke.net/blog/2009/12/17/random-constrained-permutations-in-ruby/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
