Write a Python program to check whether a nxn matrix is a magic square.The magic square is a set of numbers in a square grid where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number (15 in the example shown below) 276|15 5| 1 15 4| 3 | 8 | 15 15 15 15 15 15 The program first calculates and stores the sum of each one of the lines, columns, and the two diagonals in the list sum list, then tests whether all elements in this list are equal. Your main program prints True if the square is magic and False otherwise, and uses two functions: magic_square(): function takes the matrix as a table and returns sum list test(): takes sum list then tests whether all elements in the list are equal and returns True if the square is magic, and returns False otherwise. def main(): m = [[6, 1, 8],[7, 5, 3],[2, 9, 41]] #Function magic_square #@params: my_matrix (list of lists) #@return sum_lists (list)

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
Write a Python program to check whether a nxn matrix is a magic square.The magic square is a set of numbers in a square grid where
the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number
(15 in the example shown below)
27
6 15
95|1|15
43815
15 15 15 15 15
The program first calculates and stores the sum of each one of the lines, columns, and the two diagonals in the list sum_list,then
tests whether all elements in this list are equal.
Your main program prints True if the square is magic and False otherwise, and uses two functions:
magic_square(): function takes the matrix as a table and returns sum list
test(): takes sum_list then tests whether all elements in the list are equal and returns Trueif the square is magic, and
returns False otherwise.
def main():
m = [[6, 1, 8],[7, 5, 3],[2, 9, 4]]
#Function magic_square
#@params: my_matrix (list of lists)
#@return sum_lists (list)
Transcribed Image Text:Write a Python program to check whether a nxn matrix is a magic square.The magic square is a set of numbers in a square grid where the numbers in each row, and in each column, and the numbers in the main and secondary diagonals, all add up to the same number (15 in the example shown below) 27 6 15 95|1|15 43815 15 15 15 15 15 The program first calculates and stores the sum of each one of the lines, columns, and the two diagonals in the list sum_list,then tests whether all elements in this list are equal. Your main program prints True if the square is magic and False otherwise, and uses two functions: magic_square(): function takes the matrix as a table and returns sum list test(): takes sum_list then tests whether all elements in the list are equal and returns Trueif the square is magic, and returns False otherwise. def main(): m = [[6, 1, 8],[7, 5, 3],[2, 9, 4]] #Function magic_square #@params: my_matrix (list of lists) #@return sum_lists (list)
n = len(my_matrix[0])
%3D
sum_list = []
%3D
#Horizontal Part: compute the sum for lines
#Vertical Part: Compute the sum for columns
#Compute the sum for the main diagonal (Hint: uses index i)
#Compute the sum for the secondary diagonal (Hint: uses index j)
result2 = 0
%3D
Transcribed Image Text:n = len(my_matrix[0]) %3D sum_list = [] %3D #Horizontal Part: compute the sum for lines #Vertical Part: Compute the sum for columns #Compute the sum for the main diagonal (Hint: uses index i) #Compute the sum for the secondary diagonal (Hint: uses index j) result2 = 0 %3D
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY