Skip to content

Instantly share code, notes, and snippets.

@dannvix
Created April 16, 2012 09:38
Show Gist options
  • Save dannvix/2397318 to your computer and use it in GitHub Desktop.
Save dannvix/2397318 to your computer and use it in GitHub Desktop.
Customized format for combinations in Ruby
# for C(3, 1) it should returns %w(100 010 001)
def get_combinations (n, m)
(n.downto 1).to_a.combination(m).map do |c|
c.inject(0){ |s, k| s |= (1 << k-1) }.to_s(2).rjust(n, '0')
end
end
puts get_combinations(10, 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment