Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The Lo Shu Magic Square is a 3 by 3 grid with the following properties: The grid contains the numbers 1 through 9 where each
The Lo Shu Magic Square is a 3 by 3 grid with the following properties: The grid contains the numbers 1 through 9 where each number appears exactly once in the grid. The sum of each row, each column, and each diagonal all add up to the same number, 15. An example of a Lo Shu Magic Square is as follows: 4 3 8 15 9 5 1 15 15 2 7 6 Remark: Image adopted from http://en.wikipedia.org/wiki/Magic square 15 + 15 15 15 15 In a program, you can simulate a magic square by using a two-dimensional list. Write a Python program that tests whether or not a 3 by 3 grid is a valid magic square. The program reads the input from a text file named input.txt. Each line in the file contains exactly 9 numbers that correspond to the values in a possible magic square. The first three numbers correspond to the values in the first row, the next three values correspond to the second row, and the last three values correspond to the last row. For example, the above magic square would appear in the file as the line 4 38 9512 7 6.Sample input file is as follows: 438951276 8341 596 72 61 8753 294 698753214 61 8753 214 613294875 5 5 5 5 5 5 5 5 5 The output from the program is the word valid if the corresponding grid is a valid magic square and the word invalid if it is not. Sample program run using the above input file is as follows: valid valid valid invalid invalid invalid invalid Notice that a different input file with a different number of lines may be used to test your code. All your code must be written inside functions. Make sure to use loops where needed instead of writing many additions. You do not need to use loops to find the sum of diagonals. Use comments to document and explain your code where needed. Please use descriptive names for your variables and functions. When you are done with the homework, upload it to Blackboard. Please name the homework file your lastName_firstName_HW1.py. For example, if a student name is John Doe, then the file should be named Doe_john_HW1.py. Please follow this naming convention for all homework assignments and labs. Please make sure to write your name inside a docstring at the top of the file. Multiple submissions are enabled on Blackboard in case that a student submitted the wrong version of the file and needs to make a second submission. In case of multiple submissions, only the last submitted version will be graded. input.txt
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started