2012-11-09 25 views
0

我想獲得給定的行,列和每個列變量的數量大小表的值。準真值平板函數

例如:

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* 

返回的值在它旁邊有一個*表示clairity。

有關如何編寫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)

回答

1

以下Python腳本sho uld創建相同的「表」

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