Skip to content

Instantly share code, notes, and snippets.

@Lukasa Lukasa/
Last active Aug 29, 2015

What would you like to do?
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
You can’t perform that action at this time.