92.4k views
3 votes
Write a method called isMagicSquare that accepts a two-dimensional array of integers as a parameter and returns true if it is a magic square. A square matrix is a magic square if it is square in shape (same number of rows as columns, and every row the same length), and all of its row, column, and diagonal sums are equal. For example, [[2, 7, 6], [9, 5, 1], [4, 3, 8]] is a magic square because all eight of the sums are exactly 15.

User Qwtel
by
8.1k points

1 Answer

1 vote

Answer:

def isMagicSquare(myArray: list) -> bool :

row_num = len(myArray)

cols_num = [len(x) for x in myArray]

count =0

for x in cols_num:

if row_num == x:

count += 1

if count == len(cols_num):

return True

else:

return False

print(isMagicSquare([[1,2,3,1],[3,34,2,4],[43,5,7,5],[2,3,4,5]]))

Step-by-step explanation:

In object-oriented programming, a method is a function defined in a class. Methods are actions that can be taken or executed in a class or to a class object.

The "isMagicSquare" is a python function or method in a class that returns true if a two array or list is a perfect square (same column and row length).

User Zoxaer
by
6.6k points