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$

closed as off-topic by Yuval Filmus, D.W., vonbrand, Juho, Luke Mathieson Mar 4 '14 at 9:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – Yuval Filmus, D.W., vonbrand, Juho, Luke Mathieson
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 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.

$\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.)

$\endgroup$

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