简体   繁体   中英

Creating a magic square

So i'm creating a magic square and here is my code

def is_magic_square(s): ''' Return whether a two dimensional integer array s is magic, that is: 1) The dimensions of s is nxn 2) Every integer in [1,2,...,n*n] appears in s, exactly once. 3) The sum of all rows in s is the same as the sum of all columns in s, is the same as the sum of the diagonal elements in s.

:param s: A two dimensional integer array represented as a nested list.
:return: True if square is magic, False otherwise

QUESTION 1: Write DocTest for

TEST Matricies that are magic a few different sizes, special cases seem to be, 1x1 matrix, 2x2 matrix.
>>> is_magic_square([[1]])
True
>>> is_magic_square([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
True

YOUR TEST CASES GO HERE

NOTE:!!! LEAVE A BLANK LINE AFTER THE LAST TEST RESULT, BEFORE A COMMENT !!!
TEST Matricies that are not magic.

TEST NOT 1) The dimensions of s is nxn
>>> is_magic_square([[1,2],[3,4],[5,6]])
False
>>> is_magic_square([[1,2],[3,4,5],[6,7]])
False

YOUR TEST CASES GO HERE
>>>is_magic_square([[8, 3, 4], [1, 5, 9], [6, 7, 2]])
True

TEST NOT 2) Every integer in [1,2,...,n*n] appears in s, exactly once.

YOUR TEST CASES GO HERE
>>> is_magic_square([8, 3, 4],[9,3,3],[6,7,2])
False

TEST NOT 3) The sum of all rows in s is the same as the sum of all
   columns in s, is the same as the sum of the diagonal
   elements in s.

YOUR TEST CASES GO HERE
>>> is_magic_square([8,3,4], [1, 5, 9], [6,7,1])
False

nrows = len(s)
ncols = 0
if nrows != 0:
    ncols = len(s[0])
if nrows != ncols:
    return False
l = []
for row in s:
    for elem in row:
        if elem in l:
            return False
        l.append(elem)
m = sum(s[0])
for row in s:
    sums = 0
    for elem in row:
        sums+=elem
    if sums != m:
        return False


return True

I tested basically everything so far if the length of the nxn square is the same length, the same sum, etc. My problem is now im trying to calculate if the diagonal sum is the same as the row and column. How would i do that?

您可以通过对sum的sum([ s[i][i] for i in range(len(s))])sum([ s[len(s)-i-1][i] for i in range(len(s))])计算对角线和sum([ s[len(s)-i-1][i] for i in range(len(s))]) ,其中s变量是您要测试的列表的列表

I solved a similar problem not too long ago for a programming puzzle site. My problem was somewhat larger in scope -- verify that an NxN Sudoku puzzle is properly solved.

You'll have two diagonals in an NxN square.

import itertools

square = [ [1, 2, 3],
           [4, 5, 6],
           [7, 8, 9] ]  # not a valid square!

n = len(square)  # nxn square

rows = iter(square)
columns = zip(*square)  # this is a beautiful idiom! Learn it!
diagonals = zip(*[(square[i][i], square[-i-1][i]) for i in range(n)])

Then you can just do a simple sum check

sum(rows) == sum(columns) == sum(diagonals)

The only weird part of the code is the zip star of the list comprehension. It basically does:

diagonal1 = []
diagonal2 = []
for i in range(n):
    diagonal1.append(square[i][i])
    diagonal2.append(square[-i-1][i])
diagonals = [diagonal1, diagonal2]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM