寻找一种算法来计算可能的模式数量

时间:2013-09-04 13:02:47

标签: python r algorithm matlab matrix

我需要你帮助构建一个算法来解决以下问题:

A 5x5 table can be filled with the values ​​0 and 1 so that each line and each column of the table consists of exactly two ones and three zeros. How many solutions exist?

如果您想提供一些代码,您可以自由使用您的首选语言。我主要使用R,Matlab和Python。

我尝试将表格转换为矢量:

unique(perms([ones(1,10),zeros(1,15)]), 'rows')

然后,对于每一行,我将形成5x5表并检查所有行总和和col总和是否等于2.但是上面的命令生成了错误:??? Maximum variable size allowed by the program is exceeded.

2 个答案:

答案 0 :(得分:1)

这是一个python表达式,bute强制所有矩阵每行两个1:

from itertools import *
print len(filter(
  lambda candidate: all(imap(
    lambda index: sum(imap(lambda _: _[index], candidate)) == 2,
    xrange(5)
  )),
  product(set(permutations([0,0,0,1,1])), repeat=5)
))

答案 1 :(得分:0)

查看向量perms的Matlab函数[0 0 0 1 1],我想你会得到它。