Question

PYTHON 3: An n x n matrix forms a magic square if the following conditions are...

PYTHON 3:

An n x n matrix forms a magic square if the following conditions are met:

1. The elements of the matrix are numbers 1,2,3, ..., n2

2. The sum of the elements in each row, in each column and in the two diagonals is the same value.

Question: Complete the function that tets if the given matrix m forms a magic square.

def is_square(m):
'''2d-list => bool
Return True if m is a square matrix, otherwise return False
(Matrix is square if it has the same number of rows and columns'''
for i in range(len(m)):
if len(m[i]) != len(m):
return False
return True

def magic(m):
'''2D list->bool
Returns True if m forms a magic square
Precondition: m is a matrix with at least 2 rows and 2 columns '''

if(not(is_square(m))):
return False   

#and this is where I need help on what code can complete this

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The python 3 code for the magic function is as. use proper indentation

def magic(m):
'''2D list->bool
Returns True if m forms a magic square
Precondition: m is a matrix with at least 2 rows and 2 columns '''

if(not(is_square(m))):
return False   
n=len(m)
diag1_sum=0
diag2_sum=0
for i in range(n):
diag1_sum+=m[i][i]
  
for i in range(n):
diag2_sum+=m[n-i-1][i]
  
if diag1_sum!=diag2_sum:
return False
  
for i in range(n):
row_sum=0
for j in range(n):
row_sum+=m[i][j]
if row_sum!=diag1_sum:
return False
  
for j in range(n):
col_sum=0
for i in range(n):
col_sum+=m[i][j]
if col_sum!=diag1_sum:
return False
return True
  
  


Add a comment
Know the answer?
Add Answer to:
PYTHON 3: An n x n matrix forms a magic square if the following conditions are...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use basic java for this after importing PrintWriter object You will make a simple Magic Square...

    Use basic java for this after importing PrintWriter object You will make a simple Magic Square program for this Java Programming Assignment. Carefully read all the instructions before beginning to code. It is also required to turn in your pseudocode or a flowchart along with this program. Here are the instructions: At the beginning of the program, briefly describe to the user what a Magic Square Matrix is (described further on), and then allow them to enter “start” to begin...

  • how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagona...

    how can i solve the system of these magic matrices using matlab software ? Exercice 3. A magic matrix is a square matrix with integer entries in which all the rows, columns and the two diagonals have the same sum. For example, A- 3 5 7 4 9 2 Complete the following magic matrices 17? ?? 3 ? 2 ? 2? ? Do the following steps in each case: 1. Write the system of equations and put it under the...

  • Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement...

    Write C++ programs that create TEN(10) different N*N magic squares. A square matrix is the arrangement of the numbers 1, 2, ., N2, in which the sum of rows, columns, and diagonals are the same. The users (i.e., TAs) will specify the size of the square matrix: N. The value N must be an odd number between 3 and 15. Example Run For example, you program is expected to run as the following way. NOTE: We only list 5 magic...

  • Java Magic Square: A n x n matrix that is filled with the numbers 1, 2,...

    Java Magic Square: A n x n matrix that is filled with the numbers 1, 2, 3,.... n^2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. I need to write an application that gets 16 values as user input, in any order. Store these values in an ArrayList. When the numbers are put into a square, this would be a 4x4 two-dimensional array....

  • C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is...

    C++ Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square, place k+1 into the square...

  • I need this written in C++. Magic Squares. An n x n matrix that is filled...

    I need this written in C++. Magic Squares. An n x n matrix that is filled with the numbers 1,2,3,…,n2 is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value. The following algorithm will construct magic n x n squares; it only works if n is odd: Place 1 in the middle of the bottom row. After k has been placed in the (i,j) square,...

  • Magic Squares; Java code

    Magic squares. An n × n matrix that is filled with the numbers 1, 2, 3, ..., n2 is a magic square if the sum ofthe elements in each row, in each column, and in the two diagonals is the same value. For example,16 3 2 135 10 11 89 6 7 124 15 14 1Write a program that reads in n2 values from the keyboard and tests whether they form a magic squarewhen arranged as a square matrix. You...

  • Write a Python program that generate randomly a magic square of 3 x 3 elements. For...

    Write a Python program that generate randomly a magic square of 3 x 3 elements. For example: 4 3 8 9 5 1 2 7 6      = Matrix1 Matrix 1 above is an example of magic square. The rows total, the columns total, and the diagonal totals are all 15. Your program should randomly generate a 3x3 matrix. Check if it is a magic square. If it is a magic square, then print some information and quit. If the...

  • I'm having trouble sorting this square matrix (a 2d array that has number of rows and...

    I'm having trouble sorting this square matrix (a 2d array that has number of rows and same number of column n x n) using row-wise approach in C programming. Please help me program this in C to sort it using row-wise approach, here is the code: #include <stdio.h> #define MAX 100 int main() { int mat[MAX][MAX]; int i, j, m, n; int rowsum, columnsum, diagonalsum; int k; int magic = 0; int transpose[MAX][MAX]; printf("Enter the # of rows and columns...

  • def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in...

    def slice_list(lst: List[Any], n: int) -> List[List[Any]]: """ Return a list containing slices of <lst> in order. Each slice is a list of size <n> containing the next <n> elements in <lst>. The last slice may contain fewer than <n> elements in order to make sure that the returned list contains all elements in <lst>. === Precondition === n <= len(lst) >>> slice_list([3, 4, 6, 2, 3], 2) == [[3, 4], [6, 2], [3]] True >>> slice_list(['a', 1, 6.0, False],...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT