import itertools | |
def blocks(size, total_sum): | |
def ascending(iterable): | |
last = 0 | |
for elem in iterable: | |
if elem < last: | |
return False | |
last = elem | |
return True | |
candidate_values = range(1, size) | |
candidate_blocks = itertools.combinations_with_replacement(candidate_values, size) | |
candidate_blocks = filter(ascending, candidate_blocks) | |
candidate_blocks = filter(lambda b: sum(b) == total_sum, candidate_blocks) | |
return candidate_blocks | |
# Usage: blocks(6, 6) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment