题目:
Create a function that accepts dimensions, of Rows x Columns, as parameters in order to create a multiplication table sized according to the given dimensions. **The return value of the function must be an array, and the numbers must be Fixnums, NOT strings.
Example:
multiplication_table(3,3)
1 2 3
2 4 6
3 6 9
-->[[1,2,3],[2,4,6],[3,6,9]]
Each value on the table should be equal to the value of multiplying the number in its first row times the number in its first column.
解题办法:
def multiplication_table(row, col): L = [] # Good Luck! for i in range(1, row+1): L1 = [] for j in range(1, col+1): mul = i * j L1.append(mul) L.append(L1) return L
这题还是挺简单的。按照思路来就好
看一下网友的简写一句话的写法:
def multiplication_table(row,col): return [[(i+1)*(j+1) for j in range(col)] for i in range(row)]