1
$\begingroup$

I need to write a function that takes in a 2D list, that is a NEARLY magic square, meaning that if you perform ONE swap, it becomes a magic square. The function would take in this nearly magic square, and would return the magic square after the values have been swapped.

I wrote an "isMagic" function already that loops through the sums of the columns and rows, but am having difficulty reasoning through this MAKEMAGICSQUARE function.

So I know that makeMagic([[4,9,1],[3,5,7],[8,2,6]]) would return [[4,9,2],[3,5,7],[8,1,6]].

Also, IMPORTANT REFERENCE http://en.wikipedia.org/wiki/Magic_square.

$\endgroup$
  • 1
    $\begingroup$ This is just a dump of a hoework question, without any show of work by OP. It should be closed. And furthermoe this is not about computer science. $\endgroup$ – vonbrand Mar 1 '14 at 21:43
3
$\begingroup$

Hint: There will be two rows with wrong sums and two columns with wrong sums.

| cite | improve this answer | |
$\endgroup$
1
$\begingroup$

You could try all possibilities for that one swap, and check if the result is a magic square. There are only polynomially many of them.

(Yes, there are faster solutions, but you didn't specify efficiency as a requirement.)

| cite | improve this answer | |
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.