Last active
April 21, 2017 17:31
-
-
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`
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 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