I have a dict
like so:
a = {"a" : 1.04,
"b" : 1.07,
"c" : 10.99,
...}
I am trying to get all of the combinations of keys
that value
sum
produces x
, there can be repeating keys
.
So for example a target of 3.12
can be a result of ["a", "a", "a"]
And I wrote a code like so:
def combinations(data, target, result = [], partial_target = 0):
if partial_target == target:
yield result
if partial_target >= target:
return
for key, value in data.items():
yield from combinations(data, target, result + [key], partial_target + value)
list(combinations(a, 199.45))
But this keeps on spinning forever with no result, I edited my code after reading this answer. So I am not sure if it works or its just stuck in an infinite loop, and if not, if this is the optimal way to solve this problem.