ruby-changes:3769
From: ko1@a...
Date: Sun, 27 Jan 2008 04:49:43 +0900 (JST)
Subject: [ruby-changes:3769] akr - Ruby:r15258 (trunk): make combination recursionless.
akr 2008-01-27 04:49:37 +0900 (Sun, 27 Jan 2008) New Revision: 15258 Modified files: trunk/test/ruby/test_sprintf_comb.rb Log: make combination recursionless. http://svn.ruby-lang.org/cgi-bin/viewvc.cgi/trunk/test/ruby/test_sprintf_comb.rb?r1=15258&r2=15257&diff_format=u Index: test/ruby/test_sprintf_comb.rb =================================================================== --- test/ruby/test_sprintf_comb.rb (revision 15257) +++ test/ruby/test_sprintf_comb.rb (revision 15258) @@ -107,15 +107,18 @@ VS.reverse! def combination(*args) - if args.empty? - yield [] - else - arg = args.shift - arg.each {|v| - combination(*args) {|vs| - yield [v, *vs] - } + args = args.map {|a| a.to_a } + i = 0 + while true + n = i + as = [] + args.reverse_each {|a| + n, m = n.divmod(a.length) + as.unshift a[m] } + break if 0 < n + yield as + i += 1 end end -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/