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 6Note 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.
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 1The file name will be of your own choosing. But you can use this sample file squares.txt to test your code.
Output: The output will be in a file. The name that you choose for your output file should be different from the name of the input file. The format of the output file should be similar to the input file. You will write valid or invalid next to the size of each square. 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
In your function main() you will prompt the user to enter the name of the input file and the name of the output file. You will compare the two names to make sure that they are not the same. If the names are the same you will write a message to that effect and quit the program. If the names are different you will open the input file for reading and the output file for writing and process the data. When the program has completed processing all the squares you will write a message to the console that the output has been to written to output file. A sample session would look like this:
Enter name of input file: squares.txt Enter name of output file: result.txt The output has been written to result.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 turnin program to submit your MagicSquare.py file. The TAs should receive your work by 11 PM on Monday, 08 August 2011. 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: