[Python] How to generate a dynamic matrix based on border pattern rules using Python and...

Discussão em 'Python' iniciado por Stack, Junho 19, 2025.

  1. Stack

    Stack Membro Participativo

    Using dynamic programming, solve the following matrices. Consider the size of the square matrix as the only input parameter (for example, n = 7 generates a 7x7 matrix)

    ⚠️ Important:


    The tables contain a 9x9 matrix where the borders are only visual guides and are not part of the final matrix. The goal is to reconstruct the central 7x7 matrix by applying dynamic programming to identify patterns.

    • Table #1
    55 43 -127 -1063 -5489 -25493 -114535 -508759 -2250809
    55 1 0 0 0 0 0 483772 5
    43 0 -1 0 0 0 66833 0 4
    -127 0 0 1 0 9266 0 0 2
    -1063 0 0 0 -1 0 0 0 -9
    -5489 0 0 145 0 -1 0 0 -40
    -25493 0 11 0 0 0 -1 0 -82
    -114535 17 0 0 0 0 0 -1 -35
    16 17 11 145 1270 9266 66833 483772 3504271
    • Table #2
    16 17 11 145 1270 9266 66833 483772 3504271
    10 1 0 0 0 0 0 -1 55
    15 0 1 0 0 0 1 0 43
    22 0 0 1 0 -1 0 0 -127
    17 0 0 0 -1 0 0 0 -1063
    165 0 0 1 0 1 0 0 -5489
    1563 0 -1 0 0 0 1 0 -25493
    13499 1 0 0 0 0 0 1 -114535
    10 15 22 17 165 1563 13499 116525 -114535

    I have already identified some numerical series with their corresponding Python functions using dynamic programming:

    def serie_1(n):
    dp = [55, 43]
    for i in range(2, n):
    dp.append(6 * dp[i - 1] - 7 * dp[i - 2])
    return dp[:n]

    def serie_2(n):
    dp = [16, 17, 11]
    for i in range(3, n):
    dp.append(8 * dp[i - 1] - 7 * dp[i - 2] + 11 * dp[i - 3])
    return dp[:n]

    def serie_3(n):
    dp = [5, 4, 2]
    for i in range(3, n):
    dp.append(4 * dp[i - 1] - 8 * dp[i - 2] + 3 * dp[i - 3])
    return dp[:n]

    def serie_4(n):
    dp = [15, 17]
    for i in range(2, n):
    dp.append(8 * dp[i - 1] - 12 * dp[i - 2])
    return dp

    def serie_5(n):
    dp = [10, 15, 22, 17]
    for i in range(4, n):
    dp.append(9 * dp[i - 1] - 4 * dp[i - 2] + 8 * dp[i - 3] - 2 * dp[i - 4])
    return dp[:n]


    Outputs of each series:

    serie_1 -> [55, 43, -127, -1063, -5489, -25493, -114535, -508759, -2250809]
    serie_2 -> [16, 17, 11, 145, 1270, 9266, 66833, 483772, 3504271]
    serie_3 -> [5, 4, 2, -9, -40, -82, -35, 396, 1618]
    serie_4 -> [15, 17, -44, -556, -3920, -24688, -150464, -907456, -5454080]
    serie_5 -> [10, 15, 22, 17, 165, 1563, 13499, 116525, 1006903]

    Goal:


    I want to create the functions generate_matrix_1(n) and generate_matrix_2(n) that dynamically build these matrices for any valid n, using only principles of dynamic programming.

    Expected matrix for n = 7 (first):

    1 0 0 0 0 0 483772
    0 -1 0 0 0 66833 0
    0 0 1 0 9266 0 0
    0 0 0 -1 0 0 0
    0 0 145 0 -1 0 0
    0 11 0 0 0 -1 0
    17 0 0 0 0 0 -1

    Expected matrix for n = 7 (second):

    1 0 0 0 0 0 -1
    0 1 0 0 0 1 0
    0 0 1 0 -1 0 0
    0 0 0 -1 0 0 0
    0 0 1 0 1 0 0
    0 -1 0 0 0 1 0
    1 0 0 0 0 0 1
    ⚠️ Constraints:

    • Hardcoding of coordinates is not allowed
    • Do not use static/fixed matrix values
    • The logic should rely solely on dynamic patterns (such as those in the series)
    • It must work for a general n, and for n = 7 it should match the image

    I would really appreciate any ideas, guidance, or perspective on how to build the logic behind these matrix patterns. Thank you!

    Continue reading...

Compartilhe esta Página