Soluție HackerRank pentru Linear Algebra Foundations #5 - The 100th Power of a Matrix, subdomeniul Linear Algebra Foundations, în Python 3. Include…

  • Problemă: Linear Algebra Foundations #5 - The 100th Power of a Matrix
  • Domeniu: Linear Algebra Foundations
  • Limbaj: Python 3

Challenge: Linear Algebra Foundations #5 – The 100th Power of a Matrix

Subdomeniu: Linear Algebra Foundations (linear-algebra-foundations)

Scor cont: 5.0 / 5

Submission status: Accepted

Submission score: 1.0

Submission ID: 464718707

Limbaj: python3

Link challenge: https://www.hackerrank.com/challenges/linear-algebra-foundations-5-the-100th-power-of-a-matrix/problem

Cerință

Given the following matrix A:

[1 1 0 ]
A = [0 1 0 ]
[0 0 1 ]


We compute that

A<sup>100</sup> =

[A B 0]
[0 C 0]
[0 D E]

In the text box below, enter the values of the integers A, B, C, D, E each in a new line. Do not leave any extra leading or trailing spaces.

Cod sursă

def matrix_mul(A, B):
    ROWS_A, COLS_A = len(A), len(A[0])
    ROWS_B, COLS_B = len(B), len(B[0])
    assert COLS_A == ROWS_B
    COMMON_D = ROWS_B

    # zeros for Rows-A x Cols-B
    M_TIMES = []
    for r in range(ROWS_A):
        M_TIMES.append( [0] * COLS_B )

    # Loop over Rows-A, then Cols-B, then the common dimension
    for rA in range(ROWS_A):
        for cB in range(COLS_B):
            for t in range(COMMON_D):
                M_TIMES[rA][cB] += A[rA][t]*B[t][cB]
    return M_TIMES
    
def print_matrix(M, skip_indexes=[]):
    ROWS, COLS = len(M), len(M[0])
    for r in range(ROWS):
        for c in range(COLS):
            if skip_indexes[r][c]:
                continue
            print(M[r][c])
            
#####
A = [[1,1,0],[0,1,0],[0,0,1]]
B = A
EXP=100
for _ in range(EXP-1):
    B = matrix_mul(B, A)
# print(B)
print_matrix(B, skip_indexes=[[False, False,True],[True, False, True],[True, False, False]])
HackerRank Linear Algebra Foundations – Linear Algebra Foundations #5 – The 100th Power of a Matrix