Question: 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
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. For this assignment, you will write a program that reads squares in from a file, determines whether they are magic, and writes the output to a file.
File Name: MagicSquares.py
In your main() function, prompt the user to enter the name of the input file and the name of the output file.If the names are the same, write a message to that effect and quit the program. If the names are different, 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, write a message to the console that the output has been written to output file. A sample session would look like this (user input in green):
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, write a function isMagic() that determines 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 should accept a 2-D list as a parameter and return a Boolean value that indicates whether the square is magic or not. This function must call at least four (04)other functions.
Input: The input for this program is a file. The first line of the fileis a number n denoting the number of squares that need to be processed. It is followed by the data for each of the n squares.There is 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 input file name is of your own choosing. But you can use this sample file squares.txt to test your code. You may assume that the input file is in the correct format.
Output: The program will output to 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
Test your program by running it several times with different inputs. Put your test runs and their output in comments at the end of your .py file
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
