Suppose I have a list of three products (A, B C). Each Product has a price. Given a total cost, I want to find all the possible product combinations to equal exactly that cost.
So far I've tried stuff like:
for price in product:
ret = []
for i in range(int(totalCost / price), -1, -1):
ret.append(i)
for c in range(1, len(products)+1, 1):
ret.append(int(products[c-1][1]/products[c][1]))
And here is where I get stuck. This will get me a list of possibilities, but it will only include the products that are later (than the current location) in the list. It won't wrap around to include the beginning, and thus, give me every possibility.
What do I need to do to get every possibility?
The itertools module offers combinatoric generators to help with problems such as this:
That gives you: