Skip to content

Instantly share code, notes, and snippets.

@kmikael
Last active April 21, 2017 17:31
Show Gist options
  • Save kmikael/42dd8e658cfe77a8d1f38547ae2c8818 to your computer and use it in GitHub Desktop.
Save kmikael/42dd8e658cfe77a8d1f38547ae2c8818 to your computer and use it in GitHub Desktop.
Generate `count` weights from a range, `seq` that add up to a given `target`
def subsequences(count, seq = range(5, 40, 5), target = 100, partial = []):
if count == 0:
return
s = sum(partial)
if s >= target:
return
if s == target or (count == 1 and (target - s) in seq):
yield partial + [target - s]
for w in seq:
yield from subsequences(count - 1, seq = seq, target = target, partial = partial + [w])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment