Magic Squares (due 11 Apr 2015)

A magic square is a 2-D list where the sum of each row is equal to the sum of each column and is equal to the sum of each of the two diagonals. Here is an example of a 3 x 3 magic square:

  4  9  2
  3  5  7
  8  1  6
Note that the sum of each of the rows, columns, and diagonals is 15. This square is not unique. If you flip the rows for the columns (transpose the square) you will get a magic square. If you add a constant value to each element you will get a magic square, and so on. If you have a magic square of side n, then the sum of the rows or the columns or the diagonals is n (n2 + 1) / 2, if the numbers in the magic square are in the range 1, ..., n2.

Input: The input for this program will be in a file. The first line of the program will be a number n denoting the number of squares that you will have to process. It will be followed by the data for each of the n squares. There will be a blank line separating data for each square. For any given square the first line will give the number of rows (or the columns) followed by the rows of the square - one row per line. Here is an example.

2

3
4  1  2
3  5  7
8  9  6

4
16   3   2  13
 5  10  11   8
 9   6   7  12
 4  15  14   1
The file name will be squares.txt. Your code should work for any file that is in the above format. You can use this sample file squares.txt to test your code.

Output: The output will be in a file called results.txt. For the above input file, the output file should look like this:

2

3 invalid
4  1  2
3  5  7
8  9  6

4 valid
16   3   2  13
 5  10  11   8
 9   6   7  12
 4  15  14   1

When the program has completed processing all the squares you will write a message in the function main() to the console that the output has been to written to the result file. A sample session would look like this:

  The output has been written to results.txt

For this program, you will write a function isMagic() that will determine if a 2-D list forms a magic square. The function should be general enough to accept magic squares of any size greater than or equal to 3. The function signature should look like this:

def isMagic (b):

The file that you will be turning in will be called MagicSquare.py. We will be looking at documentation, descriptive variable and function names, clean logical structure, and adherence to the coding conventions discussed in class. The file will have a header of the following form:

#  File: MagicSquare.py

#  Description:

#  Student Name:

#  Student UT EID:

#  Course Name: CS 303E

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

Use the Canvas program to submit your MagicSquare.py file. We should receive your work by 11 PM on Saturday, 11 Apr 2015. There will be substantial penalties if you do not adhere to the guidelines.

Magic Squares have fascinated mathematicians and lay people alike. There is a rich history behind magic squares as well as a lot of research. Here are some references that you may want to look at: