Last active
September 19, 2017 17:35
-
-
Save jmg/071fdd266376c9694aadf2ac0739d03d to your computer and use it in GitHub Desktop.
Gets all the combinations of the sum of a list of elements.
This file contains hidden or 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 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