Question

The definition of a magic square is a matrix of distinct positive integers in which every...

The definition of a magic square is a matrix of distinct positive integers in which every row, column and diagonal adds up to the same number.

Given a magic square of unknown size, encoded as a nested tuple, write a python function checkMagic(square) that, given a magic square square creates a custom exception MuggleError if a given magic square is not actually magic. Otherwise, it does nothing.

python 3

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

Code

class MuggleError(Exception):

# Constructor or Initializer
def __init__(self, value):
self.value = value

# __str__ is to print() the value
def __str__(self):
return(repr(self.value))

def checkMagic(square):

try:
#calculating 1st diagonal sum
sum1 = 0
for i in range(0, len(square)) :
sum1 = sum1 + square[i][i]

# the secondary diagonal
sum2 = 0
for i in range(0, len(square)) :
sum2 = sum2 + square[i][len(square)-i-1]

if(sum1!=sum2) :
raise(MuggleError("This is not a magic square"))

# calculating each row sum and compare with sum1
for i in range(0,len(square)) :
rowSum = 0;
for j in range(0, len(square)) :
rowSum += square[i][j]
# check if every row sum is
# equal to first diagonal sum
if (rowSum != sum1) :
raise(MuggleError("This is not a magic square"))

# calculating each column sum and compare with sum1
for i in range(0, len(square)):
colSum = 0
for j in range(0, len(square)) :
colSum += square[j][i]
# check if every column sum is
# equal to first diagonal sum
if (sum1 != colSum) :
raise(MuggleError("This is not a magic square"))

except MuggleError as error:
print('A New MuggleError occured: ',error.value)


mat=(( 2, 7, 6 ),( 9, 5, 6 ),( 4, 3, 8 ))
print("matrix is")
print(mat)
print()
checkMagic(mat)

output

code snaps

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.

Add a comment
Know the answer?
Add Answer to:
The definition of a magic square is a matrix of distinct positive integers in which every...
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
  • 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...

  • Magic Square question for Python

    You have this solution in Java, I am interested in this same solution for Python.One interesting application of two-dimensional arrays is magic squares. A magic square is a square matrix in which the sum of every row, every column, and bothdiagonals is the same. Magic squares have been studied for many years, and there are some particularly famous magic squares. In this exercise you will write code todetermine whether a square is magic.You should find that the first, second, and...

  • Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elemen...

    Question A matrix of dimensions m × n (an m-by-n matrix) is an ordered collection of m × n elements. which are called eernents (or components). The elements of an (m × n)-dimensional matrix A are denoted as a,, where 1im and1 S, symbolically, written as, A-a(1,1) S (i.j) S(m, ). Written in the familiar notation: 01,1 am Gm,n A3×3matrix The horizontal and vertical lines of entries in a matrix are called rows and columns, respectively A matrix with the...

  • CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write...

    can i get some help with this program CMPS 12B Introduction to Data Structures Programming Assignment 2 In this project, you will write a Java program that uses recursion to find all solutions to the n-Queens problem, for 1 Sns 15. (Students who took CMPS 12A from me worked on an iterative, non-recursive approach to this same problem. You can see it at https://classes.soe.ucsc.edu/cmps012a/Spring l8/pa5.pdf.) Begin by reading the Wikipcdia article on the Eight Queens puzzle at: http://en.wikipedia.org/wiki/Eight queens_puzzle In...

  • **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack)...

    **TStack.py below** # CMPT 145: Linear ADTs # Defines the Stack ADT # # A stack (also called a pushdown or LIFO stack) is a compound # data structure in which the data values are ordered according # to the LIFO (last-in first-out) protocol. # # Implementation: # This implementation was designed to point out when ADT operations are # used incorrectly. def create(): """ Purpose creates an empty stack Return an empty stack """ return '__Stack__',list() def is_empty(stack): """...

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
ADVERTISEMENT