Wednesday, March 2, 2022

Multiplication table Python Codewars

Task:

Your task, is to create NxN multiplication table, of size provided in parameter.

for example, when given size is 3:

1 2 3
2 4 6
3 6 9

for given example, the return value should be: [[1,2,3],[2,4,6],[3,6,9]]

Solution:



def multiplication_table(size):
    
    res = []
    for i in range(1,size+1):
        row = []
        for j in range(1,size+1):
            row.append(j*i)
        res.append(row)
    return res

def multiplicationTable(size):
    return [[j*i for j in range(1, size+1)] for i in range(1, size+1)]

print(multiplication_table(3))

Training on Multiplication table | Codewars



Related Posts


No comments :

Post a Comment