【Kata Daily 190911】Multiplication Tables(乘法表)

本文介绍了一个简单的Python函数,用于创建指定尺寸的乘法表格。该函数接收行数和列数作为参数,并返回一个二维数组,其中每个元素等于其所在行号与列号的乘积。

题目:

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)]

 

转载于:https://www.cnblogs.com/bcaixl/p/11504825.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值