Actions
Feature #8645
closedenumerable#permutation and combination.
Status:
Closed
Priority:
Normal
Assignee:
-
Category:
-
Target version:
-
Start date:
2011-06-20
Due date:
% Done:
0%
Estimated time:
Resolution:
Invalid
Description
Hello
Methods permutation and combination are defined for array but it make more sense to define them for enumerable.
Here is sample implementation which for simplicity works only with blocks.
module Enumerable def perm(n) comb(n){|ary| ary.permutation{|p| yield(p)} } end def comb(n,&m) ary=[] e=to_enum.with_index _comb(e,n-1,1.0/0.0,ary,m) end def _comb(e,n,bound,ary,m) e.each{|el,i| return if i>=bound ary[n]=el if n==0 m.call(ary) else _comb(e,n-1,i,ary,m) end } end end [1,2,4,3,5].comb(2){|a| puts a.inspect} (1..4).comb(2){|a| puts a.inspect} (1..4).perm(2){|a| puts a.inspect}
Updated by Ondrej Bilka almost 14 years ago
Note that implementation works lazily.
It changes traversal order but documentation states that order is unspecified.
Updated by Ondrej Bilka almost 14 years ago
sorry confused with ruby issue tracking
Updated by Jean-Baptiste Barth almost 14 years ago
- Status changed from Resolved to Closed
- Resolution set to Invalid
No problem. Ruby issue tracking can be found here : http://redmine.ruby-lang.org/
Actions