Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Using Python. Thanks 2. (Magical Squares, 10pt) A square of numbers is called magic, if the numbers in each row and the numbers in each
Using Python. Thanks
2. (Magical Squares, 10pt) A square of numbers is called magic, if the numbers in each row and the numbers in each column all sum up to the same number. One of the oldest magic squares is the Lo Shu square: As you can see all rows and columns sum up to 15 (as do the diagonals, moreover, all the entries are distinct and digits between 1 and 9, we won't worry about that in this problem). Write a program magic(L) that takes a magic square (of unknown size, you cannot assume that it is a 3x3 square, you can assume that it is a real square (so it is a list of lists all of which have the same length and that length is the same as the number of lists)), and which returns True if the square is a magic square, and False otherwise. You are allowed to use the sum function if you want. Python 3.5.2 Shell Eile Edit Shell Debug Optionsindow Help >>>magic([[4, 9, 2], [3, 5, 7], [8, 1, 6]]) True >>>magic([[4, 9, 2], [3, 5, 7], [8, 2, 5]]) False >>>magic ([[16, 3, 2, 13, [5, 10, 11, 8, [9 6, 7, 12], [4, 15, 14, 11]) True 13, 2]]) False Ln: 64 Col: 4 Hint: Do this one step at a time. I'm including some hints here in case you want them (but feel free to try without reading them first): determine two values first: dimension of the square, and the target sum (pick the easiest row/column to cace with). Then split the problem into two: checking row sums is relatively easy (since you can use sumO). Column sums are harder, you need a nested accumulator loop. Do that part last. To simplify that part you may want to rewrite the first part (checking the column sums) without using the sum() function. Thatll help you work on the last part Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started