如何检查4x4矩阵是否是魔术广场?

发布于 2025-02-10 06:31:26 字数 689 浏览 3 评论 0原文

我正在尝试检查二维阵列是否是魔术方形。对以下一个或多个方向的解释感到困惑?我以身作则学习最好,因此,如果有人可以直接向我展示代码,我将不胜感激。

  • 写一个函数 接受二维列表作为参数,并确定列表是否为lo shu 魔法广场。测试程序中的功能。

  • 您的程序应使用两个不同的输入来测试功能,一个符合魔术正方形的资格,而另一个则不符合魔术正方形的资格。在每个实例中,程序都应打印功能的结果

def main():

    magicsquare = [[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
    notmagicsquare = [[10, 7, 4, 5], [2, 1, 0, 8], [8, 4, 6, 1], [4, 4, 5, 1]]

    for r in range(rows):
        for c in range(columns):
            print(magicsquare)

    for r in range(rows):
        for c in range(columns):
            print(notmagicsquare)


main()

I am trying to check if a two-dimensional array is a magic square. Getting confused about the interpretation of one or more of the directions below? I learn best by example so if anyone could show me directly with code I would appreciate it.

  • Write a function
    that accepts a two-dimensional list as an argument and determines whether the list is a Lo Shu
    Magic Square. Test the function in a program.

  • Your program should test the function with two different inputs, one qualifying as a magic square and another that doesn't qualify as a magic square. In each instance, the program should print the result of the function

def main():

    magicsquare = [[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
    notmagicsquare = [[10, 7, 4, 5], [2, 1, 0, 8], [8, 4, 6, 1], [4, 4, 5, 1]]

    for r in range(rows):
        for c in range(columns):
            print(magicsquare)

    for r in range(rows):
        for c in range(columns):
            print(notmagicsquare)


main()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事与诗 2025-02-17 06:31:26

这对我有用,尝试和任何问题 - 请询问。

编辑:还检查对角线部分。感谢@albert积分。

from typing import List

def check_magic(M: List[List[int]]) -> bool:
    for i in range(len(M)):        # whether the size of each row match the Matrix's?
       if len(M[i]) != len(M):
          return False

    N = sum(M[0])            # all rows, columns and diagonals   
    #  row sums check
    for row in M:
       if sum(row) != sum(M[0]):
          return False
    
    # column sums
    cols = [[r[c] for r in M] for c in range(len(M[0]))]

    for c in cols:
       if sum(c) != sum(M[0]):
          return False
       
    # check diagonal sums     # inspired by @Albert, credit to him
    d1 = d2 = 0 
    R = len(M)
    for i in range(R):
        d1 += M[i][i]
        d2 += M[~i][~i]
    
    return d1 == d2 == N


notmagic = [[10, 7, 4, 5],
            [2, 1, 0, 8],
            [8, 4, 6, 1],
            [4, 4, 5, 1]]



print(check_magic(notmagic))  # False
 
magic = [[16, 2, 3, 13],
         [5, 11, 10, 8],
         [9, 7,  6, 12],
         [4, 14, 15, 1]]

print(check_magic(magic))  # True

This works for me, try it and any questions - please ask.

Edit: also check the diagonal part. Thanks for @Albert points.

from typing import List

def check_magic(M: List[List[int]]) -> bool:
    for i in range(len(M)):        # whether the size of each row match the Matrix's?
       if len(M[i]) != len(M):
          return False

    N = sum(M[0])            # all rows, columns and diagonals   
    #  row sums check
    for row in M:
       if sum(row) != sum(M[0]):
          return False
    
    # column sums
    cols = [[r[c] for r in M] for c in range(len(M[0]))]

    for c in cols:
       if sum(c) != sum(M[0]):
          return False
       
    # check diagonal sums     # inspired by @Albert, credit to him
    d1 = d2 = 0 
    R = len(M)
    for i in range(R):
        d1 += M[i][i]
        d2 += M[~i][~i]
    
    return d1 == d2 == N


notmagic = [[10, 7, 4, 5],
            [2, 1, 0, 8],
            [8, 4, 6, 1],
            [4, 4, 5, 1]]



print(check_magic(notmagic))  # False
 
magic = [[16, 2, 3, 13],
         [5, 11, 10, 8],
         [9, 7,  6, 12],
         [4, 14, 15, 1]]

print(check_magic(magic))  # True
熟人话多 2025-02-17 06:31:26

在“魔术”正方形中,每行的总和必须相同。在Python 2维列表的上下文中,请记住,二维中的列表可能并非全部相同,那么检查您的正方形很重要。

例如:

def ismagic(list_):
    X = len(list_) # number of rows
    for row in list_:
        if len(row) != X:
            return False # not square
    N = sum(list_[0]) # all rows, columns and diagonals must sum to this
    # check row sums
    for row in list_[1:]:
        if sum(row) != N:
            return False
    # check column sums
    for c in range(X):
        if sum(list_[r][c] for r in range(X)) != N:
            return False
    # check diagonal sums
    d1, d2 = 0, 0
    for i in range(X):
        d1 += list_[i][i]
        d2 += list_[X-i-1][X-i-1]
    return d1 == N and d2 == N

im = [[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
nm = [[10, 7, 4, 5], [2, 1, 0, 8], [8, 4, 6, 1], [4, 4, 5, 1]]

print(ismagic(im))
print(ismagic(nm))

输出:

True
False

In a "magic" square the sum of each row, column and diagonal must be the same. In the context of a Python 2-dimensional list and bearing in mind that the lists in the 2nd dimension may not all be of the same length, then it's important to check that you have a square.

For example:

def ismagic(list_):
    X = len(list_) # number of rows
    for row in list_:
        if len(row) != X:
            return False # not square
    N = sum(list_[0]) # all rows, columns and diagonals must sum to this
    # check row sums
    for row in list_[1:]:
        if sum(row) != N:
            return False
    # check column sums
    for c in range(X):
        if sum(list_[r][c] for r in range(X)) != N:
            return False
    # check diagonal sums
    d1, d2 = 0, 0
    for i in range(X):
        d1 += list_[i][i]
        d2 += list_[X-i-1][X-i-1]
    return d1 == N and d2 == N

im = [[16, 2, 3, 13], [5, 11, 10, 8], [9, 7, 6, 12], [4, 14, 15, 1]]
nm = [[10, 7, 4, 5], [2, 1, 0, 8], [8, 4, 6, 1], [4, 4, 5, 1]]

print(ismagic(im))
print(ismagic(nm))

Output:

True
False
勿挽旧人 2025-02-17 06:31:26

假设a lo shu magic square 的每一列,行,对角线和反对道子的总和相同。

def is_magic_square(square_matrix):
    # function used to check migic
    is_magic_sum = sum(square_matrix[0]).__eq__

    # check rows
    if not all(map(is_magic_sum, map(sum, square_matrix))):
        return False
    # check cols
    if not all(map(is_magic_sum, map(sum, zip(*square_matrix)))):
        return False
    # check main diagonal
    if not is_magic_sum(sum(square_matrix[i][i]) for i in range(len(square_matrix))):
        return False
    # check anti diagonal
    if not is_magic_sum(sum(square_matrix[i][len(square_matrix)-i-1] for i in range(len(square_matrix)))):
        return False

    return True

Assumed that a Lo Shu Magic Square has the same sum for every columns, rows, diagonal and anti-diagonal.

def is_magic_square(square_matrix):
    # function used to check migic
    is_magic_sum = sum(square_matrix[0]).__eq__

    # check rows
    if not all(map(is_magic_sum, map(sum, square_matrix))):
        return False
    # check cols
    if not all(map(is_magic_sum, map(sum, zip(*square_matrix)))):
        return False
    # check main diagonal
    if not is_magic_sum(sum(square_matrix[i][i]) for i in range(len(square_matrix))):
        return False
    # check anti diagonal
    if not is_magic_sum(sum(square_matrix[i][len(square_matrix)-i-1] for i in range(len(square_matrix)))):
        return False

    return True
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文