Answered step by step
Verified Expert Solution
Link Copied!

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
image text in transcribed
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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

5. Structure your speech to make it easy to listen to

Answered: 1 week ago

Question

1. Describe the goals of informative speaking

Answered: 1 week ago