Last active
August 29, 2015 13:57
-
-
Save Lukasa/9464000 to your computer and use it in GitHub Desktop.
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
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