Skip to content

Instantly share code, notes, and snippets.

@jmg
Last active September 19, 2017 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmg/071fdd266376c9694aadf2ac0739d03d to your computer and use it in GitHub Desktop.
Save jmg/071fdd266376c9694aadf2ac0739d03d to your computer and use it in GitHub Desktop.
Gets all the combinations of the sum of a list of elements.
import itertools
def get_all_sum_combinations(elements):
"""
Gets all the combinations of the sum of a list of elements.
"""
combinations = []
for i in range(1, len(elements) + 1):
for subset in itertools.combinations(elements, r=i):
result = sum(subset)
if result not in combinations:
combinations.append(result)
return combinations
print get_all_sum_combinations([3,4,10])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment