【问题标题】:quasi truth tablet function准真值片功能
【发布时间】:2012-11-09 06:25:04
【问题描述】:

我试图在给定行、列和每个列变量的数字大小的情况下获取表的值。

例如:

getValue(row = 5, column = 1, column_variable_sizes = [3,2]) 

会回来

1

这是函数“生成”以获取返回值的表。它实际上不必生成整个表(仅返回值),并且该表不是由数据结构表示的。

       column 
row |  0   1
---------------
0   |  0   0
1   |  0   1
2   |  1   0
3   |  1   1
4   |  2   0
5   |  2   1*

为了清晰起见,返回的值旁边有一个 *。

关于如何编写 getValue 函数有什么想法吗?

谢谢

编辑: 另一个调用getValue()的例子

getValue(row = 7, column = 2, column_variable_sizes = [3,2,3,2]) 

会回来

0


       column 
row |  0   1   2   3
--------------------
0   |   0   0   0   0
1   |   0   0   0   1
2   |   0   0   1   0
3   |   0   0   1   1
4   |   0   0   2   0
5   |   0   0   2   1
6   |   0   1   0   0
7   |   0   1   0*  1
8   |   0   1   1   0
9   |   0   1   1   1
10  |   0   1   2   0
11  |   0   1   2   1
... |  ... ... ... ...

同样,该表本身并不存在。函数只生成返回值。

column_variable_sizes 指的是每个列变量的域的基数。

例如 [3,2,3,2] 表示:

  • 第 0 列中的变量可以有 3 个值(0、1、2)
  • 第 1 列中的变量可以有 2 个值 (0 ,1)
  • 第 2 列中的变量可以有 3 个值(0、1、2)
  • 第 3 列中的变量可以有 2 个值 (0, 1)

【问题讨论】:

    标签: truthtable


    【解决方案1】:

    以下 Python 脚本应创建相同的“表”

    def getCell(row, column, column_variable_sizes):
        basecap = 1;
        if column + 1 < len(column_variable_sizes):     
            for i in range(column + 1, len(column_variable_sizes)):
                basecap *= column_variable_sizes[i];
    
        columncap = column_variable_sizes[column];
    
        return (row / (basecap)) % columncap
    
    column_sizes = [3, 2, 3, 2]
    
    for y in range(0, 12):
        column = "";
        for x in range(0, 4):
            column += str(getCell(y, x, column_sizes)) + " "
    
    print column
    

    【讨论】:

      猜你喜欢
      • 2019-10-22
      • 1970-01-01
      • 1970-01-01
      • 2013-03-13
      • 2013-08-17
      • 1970-01-01
      • 2012-01-08
      • 1970-01-01
      相关资源
      最近更新 更多