没有办法将k个可区分的项目放入n个可区分的框中 [英] No of ways of placing k distinguishable items into n distinguishable boxes
问题描述
这与发布的问题非常相似。
我正在尝试获取此问题的python代码。
注意,尽管相似,但有一个关键区别。即
一个存储桶可以为空,而其他存储桶则包含所有项目。
It is very similar to this question posted. I'm trying to get python code for this question. Note although it is similar there is a key difference. i.e. A bucket can be empty, while the other buckets contain all the items. Even this case will be considered as a separate case.
例如:
考虑我有3件物品A ,B,C和3个桶B1,B2,B3
Consider I have 3 items A,B,C and 3 buckets B1, B2, B3
下表将显示预期结果:
B1 B2 B3
(A,B,C) () ()
() (A,B,C) ()
() () (A,B,C)
(A) (B) (C)
(A) (C) (B)
(B) (A) (C)
(B) (C) (A)
(C) (B) (A)
(C) (A) (B)
(A,B) (C) ()
(A,B) () (C)
(B,C) (A) ()
(B,C) () (A)
(A,C) (B) ()
(A,C) () (B)
() (A,B) (C)
(C) (A,B) ()
() (B,C) (A)
(A) (B,C) ()
() (A,C) (B)
(B) (A,C) ()
() (C) (A,B)
(C) () (A,B)
() (A) (B,C)
(A) () (B,C)
() (B) (A,C)
(B) () (A,C)
Length is 27.
>>def make_sets(items, num_of_baskets=3):
pass
>>make_sets(('A', 'B', 'C', 'D', 'E'), 3)
我期望函数的输出以元组列表的形式给我这些组合。我再说一遍,项目的数量是可变的,而存储桶的数量也是可变的。
I'm expecting the output of a function to give me these combinations in a form of list of lists of tuples. I'm saying this again the number of items is variable and the number of buckets is variable too.
**请为make_sets函数提供python代码。
** Please provide python code for the make_sets function.
如果有人可以解释数学组合学。我也非常感谢。我在这个问题上花了超过2天的时间,但没有找到确定的解决方案。
If someone can explain the math combinatorics. I'd greatly appreciate that too. I spent more than 2 days on this problem without reaching a definite solution.
推荐答案
请注意,这对应于以n为底的数字(希望您可以美化输出)。此解决方案独立于n和k:
Notice that this corresponds to base-n numbers (I hope you can beautify the output). This solution is independent of n and k:
n = 3
k = 4
a = [0] * k
def to_base_n(x):
num = 0
while x is not 0:
num *= 10
num += x % n
x //= n
return num
for i in range(0, n ** k):
s = ('%0' + str(k) + 'd') % (to_base_n(i))
x = [list() for _ in range(n)]
for i in range(k):
x[int(s[i])].append(str(i))
print(x)
这篇关于没有办法将k个可区分的项目放入n个可区分的框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!