Axios_Andrew
Axios_Andrew

Reputation: 63

Trying to find all possible combinations and groupings

I have been trying to work through the following problem using excel and realized this may be better suited for Python however, am not sure where to start, I have only a basic understanding of Python. how would you guys approach solving this?

There are 6 kinds of coffee and there are 10 kinds of flavor shots and you can put one, two or three shots in each kind of coffee. Based on this, I would like to know (and list) the unique flavor combinations and how long you could go without having the same cup of coffee.

Upvotes: 1

Views: 1351

Answers (3)

Ffisegydd
Ffisegydd

Reputation: 53718

Using itertools.combinations you can easily get all the different possibly combinations of flavour shot:

from itertools import combinations

shots = range(1,11)
N = 3

coms = [c for n in range(1, N+1) for c in combinations(shots, n)]

The length of coms will give you the number of combinations, which in the case of 6 flavours and one, two, or three shots is 175.

The number of flavour shot combinations when added to coffee is simply 6 * 175 = 1050.

EDIT

Additionally (as an aside) you don't even need to do this through programming. Assuming you have n elements and you want to work out how many different ways you can pick k of them then the number is given by the Binomial coefficient which can be easily calculated with C(n, k) = n!/(k!*(n-k)!),

In your case you have n=10 and k=1, k=2, and k=3. Thus your solution is:

C(10, {1, 2, 3}) = C(10, 3) + C(10, 2) + C(10, 1)
                 = 10!/(3!*(10-3)!) + 10!/(2!*(10-2)!) + 10!/(1!*(10-1)!)
                 = 175
           Total = 175 * 6 = 1050

Upvotes: 2

Josh Smeaton
Josh Smeaton

Reputation: 48730

>>> flavor = ["F1","F2","F3","F4","F5","F6","F7","F8","F9","F10"]
>>> coffee = ["C1","C2","C3","C4","C5","C6"]
>>> from itertools import product, combinations, chain
>>> len(list(product(coffee, chain(combinations(flavor, 1), combinations(flavor, 2), combinations(flavor, 3)))))
1050
>>> list(product(coffee, chain(combinations(flavor, 1), combinations(flavor, 2), combinations(flavor, 3))))
[('C1', ('F1',)), ('C1', ('F2',)), ('C1', ('F3',)), ('C1', ('F4',)), ('C1', ('F5',)), ('C1', ('F6',)), ('C1', ('F7',)), ('C1', ('F8',)), ('C1', ('F9',)), ('C1', ('F10',)), ('C1', ('F1', 'F2')), ('C1', ('F1', 'F3')), ('C1', ('F1', 'F4')), ('C1', ('F1', 'F5')), ('C1', ('F1', 'F6')), ('C1', ('F1', 'F7')), ('C1', ('F1', 'F8')), ('C1', ('F1', 'F9')), ('C1', ('F1', 'F10')), ('C1', ('F2', 'F3')), ...

I think this is correct. We build up all the 1, 2, and 3 tuple combination of coffee flavors, and chain them together into a single iterable. Then we take the product of combining every combination of flavors with each coffee.

Upvotes: 2

Mark
Mark

Reputation: 108567

This actually sounds like a job for itertools.product:

Define coffees as:

 coffee = ["C1","C2","C3","C4","C5","C6"]

And flavor shot as:

 flavor = ["F1","F2","F3","F4","F5","F6","F7","F8","F9","F10"]

One flavor shot:

 itertools.product(coffee,flavor)

Two flavor shot:

 [i for i in itertools.product(coffee,flavor,flavor) if i[1] != i[2]]

Three flavor shot:

 [i for i in itertools.product(coffee,flavor,flavor, flavor) if i[1] != i[2] and i[2] != i[3] and i[1] != i[3]]

Upvotes: 1

Related Questions