나는 이렇게 학습한다/Algorithm & SQL

Multiplication table

daco2020 2022. 3. 19. 14:14
반응형

Description:

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:

1. Use the for statement twice to create a two-dimensional array.

2. The numbers in the given array are added in the second for statement.

3. Returns the numbers in a two-dimensional array.

 

 

def multiplication_table(size):
    result = []        
    for i in range(1, size+1):
        count, arr = 0, []
        for _ in range(size):
            count += i
            arr.append(count)
        result.append(arr)
    return result

 

 

 

Best Practice:

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

 

 

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

자료구조 _ Hash table  (0) 2022.03.20
자료구조 _ Queue 그리고 Stack  (0) 2022.03.20
Primorial Of a Number  (0) 2022.03.18
자료구조 _ Linked List란?  (0) 2022.03.17
자료구조 _ Array와 Dynamic Array  (0) 2022.03.17