Aldric Giacomoni wrote:
> Suraj Kurapati wrote:
>> Look at Array#enumeration in my ISC-licensed combinatorics library:
>> http://github.com/sunaku/inochi/blob/master/lib/inochi/util/combo.rb
>
> My jaw is gaping at the code I read..

:-)  To be fair, my library also implements combinations and 
enumerations: both in the "n choose k" subset form and in the exhaustive 
"all items in this list" form.

It's interesting to note that the only algorithmic difference between 
permutations and combinations is that combinations prevent already 
visited vertices from being visited again in subsequent iterations, 
whereas permutations do not.  I was quite pleased with this observation, 
because it allowed me to base nPk and nCk on a single implementation: 
pnk_cnk_impl().

> Would this help at all?
> http://trevoke.net/blog/2008/12/20/lexicographic-permutations-in-ruby/

I did not understand how to use that simpler permutations() method:

skurapati@skurapati-dt ~> irb
## ruby 1.8.7 (2008-08-11 patchlevel 72) [i486-linux]
>> def permutations li
>>   if li.length < 2
>>     yield li
>>     else
?>       li.each do |element|
?>         permutations(li.select() {|n| n != element}) \
?>         {|val| yield([element].concat val)}
>>       end
>>     end
>>   end
=> nil
>> permutations ["0", "1"]
LocalJumpError: no block given
  from (irb):7:in `permutations'
  from (irb):3:in `permutations'
  from (irb):6:in `permutations'
  from (irb):5:in `each'
  from (irb):5:in `permutations'
  from (irb):12
>> permutations ["0", "1"] do end
=> ["0", "1"]
>> permutations ["0", "1"] do |val| val end
=> ["0", "1"]
>> permutations ["0", "1", "2"] do end
=> ["0", "1", "2"]
>> permutations ["0", "1", "2"] do |val| val end
=> ["0", "1", "2"]
>> permutations ["0", "1", "2"] do |val| [val] end
=> ["0", "1", "2"]

Any suggestions?
-- 
Posted via http://www.ruby-forum.com/.