سؤال

I'm trying to get the values of a table given the row, column, and the number size of each column variable.

For example:

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

would return

1

this is the table the function is "generating" to get the return value. It doesn't actually have to generate the entire table (only the return value) and the table is not represented by a data structure.

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

The returned value has a * next to it for clairity.

any ideas on how to write the getValue function?

Thanks

EDIT: another example of a call of getValue()

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

would return

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
... |  ... ... ... ...

again, the table does not exist per se. Only the value returned is generated by the function.

column_variable_sizes refers to the cardinality of each column variable's domain.

For example [3,2,3,2] means:

  • the variable in column 0 can have 3 values (0, 1, 2)
  • the variable in column 1 can have 2 values (0 ,1)
  • the variable in column 2 can have 3 values (0, 1, 2)
  • the variable in column 3 can have 2 values (0, 1)
هل كانت مفيدة؟

المحلول

The following Python script should create the same "table"

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top