Created
February 8, 2018 08:44
-
-
Save mastern2k3/a681151c779f60f6f0e0c5c5b9f7df1c to your computer and use it in GitHub Desktop.
Print all binary permutations of a given size and a given amount of ones
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
def print_confs(c_len, c_ones): | |
inner_print_confs("", c_len, c_ones) | |
def inner_print_confs(c_prefix, c_len, c_ones): | |
if c_len == c_ones: | |
print(c_prefix + "1" * c_len) | |
return | |
if c_ones == 0: | |
print(c_prefix + "0" * c_len) | |
return | |
if c_len > c_ones: | |
inner_print_confs(c_prefix + "1", c_len - 1, c_ones - 1) | |
inner_print_confs(c_prefix + "0", c_len - 1, c_ones) | |
print_confs(10, 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment