Starting from:

$20

Magic square Solved

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.
You may work in pairs for this assignment, but you must follow the guidelines covered in Pair Programming Guidelines.
Note: You may not use any programming construct that we have not covered in class.
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***** write a message to that effect and quit the program. If the names are***** 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 to 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.
Input: The input for this program is a file. The first line of the file is 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***** 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

More products