Magic Square ( Due 10 Sep 2021 )

A n x n matrix that is filled with the numbers 1, 2, 3, ..., n² is a magic square if the sum of the elements in each row, in each column, and in the two diagonals is the same value.

Implement the following algorithm to construct the magic n-by-n squares. This algorithm works only if n is odd.

Input: You will read your input data from a file called square.in. The format of the file will be as follows:

5
1
16
23
35
The first line will be the dimension of the magic square. It will always be odd and greater than 1 and less than 100. This will be followed by an arbitrary number of lines. There will be a single number on each line. These numbers will be numbers inside the magic square. Some of these numbers will be interior numbers, others will be numbers on the edge, and yet others will be numbers at the corners of the magic square. There might even be numbers outside of the range of numbers in the magic square. Assume that the input file that we will be testing your program will be valid.

According to our algorithm, a magic square of dimension 5 will be as follows:

11  18  25   2   9
10  12  19  21   3
 4   6  13  20  22
23   5   7  14  16
17  24   1   8  15

Output: For each of the numbers inside the magic square, your output will be the sum of all the numbers adjacent to this number, but not including this number. For the above input file you will output on the console:

58
79
56
0
The sum of the numbers adjacent to 1 is 58 (= 5 + 7 + 14 + 24 + 8). The sum of numbers adjacent to 35 is 0 since 35 is outside the range of numbers in the magic square.

The skeleton of your program will be as follows. In order for you to receive full credit, your functions must match the specifications given here. In particular, return types and values must match, and you should only include print statements when specified. You may add helper functions as needed. We will be looking for good documentation, descriptive variable names, clean logical structure, and adherence to the standard coding conventions discussed in class. You may work with a partner on this assignment. Both of you must read the paper on Pair Programming and abide by the ground rules as stated in that paper. The file will have a header of the following form:

#  File: MagicSquare.py

#  Description:

#  Student's Name:

#  Student's UT EID:
 
#  Partner's Name:

#  Partner's UT EID:

#  Course Name: CS 313E 

#  Unique Number: 

#  Date Created:

#  Date Last Modified:

import sys

# Populate a 2-D list with numbers from 1 to n2
# This function must take as input an integer. You may assume that
# n >= 1 and n is odd. This function must return a 2-D list (a list of
# lists of integers) representing the square.
# Example 1: make_square(1) should return [[1]]
# Example 2: make_square(3) should return [[4, 9, 2], [3, 5, 7], [8, 1, 6]]
def make_square ( n ):

# Print the magic square in a neat format where the numbers
# are right justified. This is a helper function.
# This function must take as input a 2-D list of integers
# This function does not return any value
# Example: Calling print_square (make_square(3)) should print the output
# 4 9 2
# 3 5 7
# 8 1 6
def print_square ( magic_square ):

# Check that the 2-D list generated is indeed a magic square
# This function must take as input a 2-D list, and return a boolean
# This is a helper function.
# Example 1: check_square([[1, 2], [3, 4]]) should return False
# Example 2: check_square([[4, 9, 2], [3, 5, 7], [8, 1, 6]]) should return True
def check_square ( magic_square ):

# Input: square is a 2-D list and n is an integer
# Output: returns an integer that is the sum of the
#         numbers adjacent to n in the magic square
#         if n is outside the range return 0
def sum_adjacent_numbers (square, n):

def main():
  # read the input file from stdin

  # create the magic square

  # print the sum of the adjacent numbers 

# This line above main is for grading purposes. It will not affect how
# your code will run while you develop and test it.
# DO NOT REMOVE THE LINE ABOVE MAIN
if __name__ == "__main__":
  main()

The function check_square() checks that the sum of all the rows have the same value. It checks that the sum of all the columns have the same value. It sums the two main diagonals and checks that they have the same value. For a magic square of size n, the sum is n * (n2 + 1) / 2.

If you are working with a partner then only one of you will submit the code that you worked on together. In the header make sure that you have your name and your partner's name. On Gradescope submit your code as group submission making sure that you list your partner's name. If you are working alone then remove the fields that has the partner's name and UT EID.

Use the Canvas system to submit your MagicSquare.py file. We should receive your work by 11 PM on Friday, 10 Sep 2021. There will be substantial penalties if you do not adhere to the guidelines. Remember Python is case sensitive. The name of your file must match exactly what we have specified.

References

  1. Magic Square Article in Wolfram MathWorld
  2. Article on Magic Square in Wikipedia