Skip to content

Instantly share code, notes, and snippets.

@sgobin
Created July 3, 2019 14:01
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 sgobin/1ba7be38e0714d8097db1f853c4cf49c to your computer and use it in GitHub Desktop.
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.
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