Need Help ?

Home >> Assignments >> Other >> Please implement in Python or Java :) A magic square of size K is a K*K square grid filled with inte

(Solved): Please implement in Python or Java :) A magic square of size K is a K*K square grid filled with inte ...



Please implement in Python or Java :)

A magic square of size K is a K*K square grid filled with integers such that the sum of the integers in each row, column and diagonal of the grid is equal. For instance the following diagram shows a magic square of size 3. The top row has a sum of 7+ 3+ 8 = 18, the last column has a sum of 8 + 5 + 5 = 18 and one of the two diagonals has a sum of 4 + 6 + 8 = 18. Every other row and column (and the second diagonal) also has a sum of 18: 7 6 5 Note that integers in the magic square do not have to be distinct. Also note that every square of size 1 is magic. Write a function: def solution(A) that, given a matrix A consisting of N rows and M columns, returns the size of the largest magic square that can be found within this matrix. Examples: 1. Given matrix A with four rows and five columns: 0 1 2 3 4 0 4 3 | 4 | 5 | 3 |2|7|3|8|4 21 7 6 5 2 | 8|4|9|5|5 the function should return 3. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 18. 2. Given matrix A with three rows and four columns: 0 1 2 3 0 | 2211 1 2 2 2 2 2 1 2 2 2 the function should return 2. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 4. 3. Given matrix A with five rows and three columns: 0 1 2 0 7 2 4 127 6 2 9 5 1 Co the function should return 3. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 15. Assume that: • N and M are integers within the range (1..20]; • each element of matrix A is an integer within the range [1..100,000). Show transcribed image text A magic square of size K is a K*K square grid filled with integers such that the sum of the integers in each row, column and diagonal of the grid is equal. For instance the following diagram shows a magic square of size 3. The top row has a sum of 7+ 3+ 8 = 18, the last column has a sum of 8 + 5 + 5 = 18 and one of the two diagonals has a sum of 4 + 6 + 8 = 18. Every other row and column (and the second diagonal) also has a sum of 18: 7 6 5 Note that integers in the magic square do not have to be distinct. Also note that every square of size 1 is magic. Write a function: def solution(A) that, given a matrix A consisting of N rows and M columns, returns the size of the largest magic square that can be found within this matrix. Examples: 1. Given matrix A with four rows and five columns: 0 1 2 3 4 0 4 3 | 4 | 5 | 3 |2|7|3|8|4 21 7 6 5 2 | 8|4|9|5|5
the function should return 3. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 18. 2. Given matrix A with three rows and four columns: 0 1 2 3 0 | 2211 1 2 2 2 2 2 1 2 2 2 the function should return 2. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 4. 3. Given matrix A with five rows and three columns: 0 1 2 0 7 2 4 127 6 2 9 5 1 Co the function should return 3. The sum of the integers in each row, column and diagonal of the largest magic square is equal to 15. Assume that: • N and M are integers within the range (1..20]; • each element of matrix A is an integer within the range [1..100,000).


We have an Answer from Expert

View Expert Answer

Get Expert Solution


We have an Answer from Expert

Buy This Answer $4

Place Order

QUICK ORDER

Why Place An Order With Us?

  • Certified Editors
  • 24/7 Customer Support
  • Profesional Research
  • Easy to Use System Interface
  • Student Friendly Pricing
Order Now