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 23 5 78 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.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.pyIn 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.txtEnter name of output file: result.txtThe output has been written to result.txtFor 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 squareone row per line. Here is an example:234 1 23 5 78 9 6416 3 2 135 10 11 89 6 7 124 15 14 1The 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:23 invalid4 1 23 5 78 9 64 valid16 3 2 135 10 11 89 6 7 124 15 14 1
Programming
[Solved] Magic square Solution
$25
File Name: Magic_square_Solution.zip
File Size: 197.82 KB
Only logged in customers who have purchased this product may leave a review.
Reviews
There are no reviews yet.