Skip to content

Instantly share code, notes, and snippets.

@mastern2k3
Created February 8, 2018 08:44
Show Gist options
  • Save mastern2k3/a681151c779f60f6f0e0c5c5b9f7df1c to your computer and use it in GitHub Desktop.
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
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