Paste: k-subsets

Author: neonfreon
Mode: ruby
Date: Wed, 27 Jan 2010 16:06:29
Plain Text |
def combos(n,r)
  rows = []
  q = (0...r).to_a
  curval = q.reverse

  while(true)
    rows << curval.dup
    curval[0] += 1
    q.each do |j|
      if curval[j] == n-j
        return rows if j == (r - 1)
        next if (curval[j+1] += 1) == n - (j+1)
        (0...(j+1)).each { |r| curval[(j - r)] = curval[(j - r)+1] + 1 }
      else
        break
      end
    end
  end
end

New Annotation

Summary:
Author:
Mode:
Body: