Created
April 16, 2012 09:38
-
-
Save dannvix/2397318 to your computer and use it in GitHub Desktop.
Customized format for combinations in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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