Last active
August 29, 2015 14:18
-
-
Save buddy-sandidge/f4b25eb2459b1759469b to your computer and use it in GitHub Desktop.
combinations sum problem
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
#!/usr/bin/env python | |
from __future__ import print_function | |
from itertools import combinations | |
import sys | |
def combinations_sum_range(lower, upper, numbers): | |
for size in range(len(numbers)): | |
for combo in set(combinations(numbers, size + 1)): | |
if lower <= sum(combo) <= upper: | |
yield combo | |
if __name__ == '__main__': | |
try: | |
args = list(map(int, sys.argv[1:])) | |
lower = args[0] | |
upper = args[1] | |
numbers = args[2:] | |
except (ValueError, IndexError,): | |
print('Usage: {0} MIN MAX [NUM]...'.format(sys.argv[0])) | |
sys.exit(1) | |
for combo in combinations_sum_range(lower, upper, numbers): | |
print(' '.join(map(str, combo))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment