Skip to content

Instantly share code, notes, and snippets.

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