Created
July 3, 2019 14:01
-
-
Save sgobin/1ba7be38e0714d8097db1f853c4cf49c to your computer and use it in GitHub Desktop.
Acha quais os números em uma lista que geram a soma de um número.
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
def subset_sum(numbers, target, partial=[]): | |
s = sum(partial) | |
# check if the partial sum is equals to target | |
if s == target: | |
print "sum(%s)=%s" % (partial, target) | |
if s >= target: | |
return # if we reach the number why bother to continue | |
for i in range(len(numbers)): | |
n = numbers[i] | |
remaining = numbers[i+1:] | |
subset_sum(remaining, target, partial + [n]) | |
subset_sum(numeros, alvo) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment