In this part of the lab, you will make sure that your magic square really is a magic square by calculating the sums of the numbers in each row, column, and diagonal.
In order to test your code, you'll also call a function to read magic squares from a user-specified file.
def sum_row_values(matrix, row_number): """ Sums the values of all entries in the row given by "row_number" Parameters: matrix: a two dimensional (square) list row_number: an integer value in the range 0 and len(matrix)-1 Returns: the sum of all values of the row indicated by "row_number" """ # Your code goes here
sum_of_row_0 = sum_row_values(your_variable, 0) print() print('The sum of values in row 0 is', sum_of_row_0)
The output should look like this:
Enter an odd integer to build a magic square: 5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The sum of values in row 0 is 65
Here is another example:
Enter an odd integer to build a magic square: 7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The sum of values in row 0 is 175
Now modify your main function to print the sum of each row.
Sample input/output:
Enter an odd integer to build a magic square: 7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The sum of values in row 0 is 175
The sum of values in row 1 is 175
The sum of values in row 2 is 175
The sum of values in row 3 is 175
The sum of values in row 4 is 175
The sum of values in row 5 is 175
The sum of values in row 6 is 175
def sum_col_values(matrix, col_number): """ Sums the values of all entries in the column given by "col_number" matrix: a two dimensional (square) array col_number: an integer value in the range 0 and len(matrix)-1 Returns the sum of all values in the column indicated by "col_number" """
Enter an odd integer to build a magic square: 5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The sum of values in column 0 is 65
The sum of values in column 1 is 65
The sum of values in column 2 is 65
The sum of values in column 3 is 65
The sum of values in column 4 is 65
Another example:
Enter an odd integer to build a magic square: 9
47 58 69 80 1 12 23 34 45
57 68 79 9 11 22 33 44 46
67 78 8 10 21 32 43 54 56
77 7 18 20 31 42 53 55 66
6 17 19 30 41 52 63 65 76
16 27 29 40 51 62 64 75 5
26 28 39 50 61 72 74 4 15
36 38 49 60 71 73 3 14 25
37 48 59 70 81 2 13 24 35
The sum of values in column 0 is 369
The sum of values in column 1 is 369
The sum of values in column 2 is 369
The sum of values in column 3 is 369
The sum of values in column 4 is 369
The sum of values in column 5 is 369
The sum of values in column 6 is 369
The sum of values in column 7 is 369
The sum of values in column 8 is 369
def sum_top_left_bottom_right_diagonal(matrix): """ Calculates the sum of the values at matrix[0][0], matrix[1][1], etc. matrix: a two dimensional (square) matrix return value: the sum of values of the top-left to bottom-right diagonal """
Add code to main to test this function before you proceed.
def sum_top_right_bottom_left_diagonal(matrix): """ Calculates the sum of the values at matrix[0][len(matrix)-1], matrix[1][len(matrix)-2], etc. matrix: a two dimensional (square) matrix return value: the sum of values of the top-right to bottom-left diagonal """
This diagonal is harder than the previous one, but if you carefully study the indices of this diagonal of a few of the matrices above, you should see the pattern. Test this function before you proceed.
def is_magic_square(matrix): """ returns True if the two dimensional array that "matrix" points to is a magic square. Otherwise, it returns False. matrix: a two dimensional (square) array. return value: True or False. """ # calculate the sum of the values of the top-left to # bottom-right diagonal. Call it tlbr_sum. # calculate the sum of the values of the top-right to # bottom-left diagonal. Call it trbl_sum. # if tlbr_sum is not equal to trbl_sum, return False. Otherwise, # proceed. # calculate the sum of each row of the matrix and compare it # with tlbr_sum. If the two sums are not equal, return False. # Otherwise, proceed. # calculate the sum of each column of the matrix and compare it # with tlbr_sum. If the two sums are not equal, return False. # Otherwise, proceed. # return True.
Enter an odd integer to build a magic square: 7
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The above square is a magic square.
The above square is NOT a magic square.if the rows, columns, and diagonals do NOT all add to the same amount.
def read_magic_square(filename): """ Read values from a file into a 2D list Parameter: filename: the name of the file Returns a 2D list of integer values read. """ infile = open(filename, "rt") square = [] # start with an empty list for line in infile: # read text from file row = [] numbers = line.split() # Loop through the list of numbers. # Append each number to the row. for num in numbers: row.append(num) if len(row) > 0: # Don't count blank lines square.append(row) # Append the row to the 2D list return square
Enter an odd integer to build a magic square: 7 30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20 The above square is a magic square. Enter the name of a file containing a matrix of numbers: ms1.txt
Enter an odd integer to build a magic square: 7 30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20 The above square is a magic square. Enter the name of a file containing a matrix of numbers: ms1.txt 1 2 3 4 5 6 7 8 9
TypeError: unsupported operand type(s) for +=: 'int' and 'str'Fix your read_magic_square function to eliminate this error by making sure that the elements of your list are all integers. You should only need to change one line.
Enter an odd integer to build a magic square: 7 30 39 48 1 10 19 28 38 47 7 9 18 27 29 46 6 8 17 26 35 37 5 14 16 25 34 36 45 13 15 24 33 42 44 4 21 23 32 41 43 3 12 22 31 40 49 2 11 20 The above square is a magic square. Enter the name of a file containing a matrix of numbers: ms1.txt 1 2 3 4 5 6 7 8 9 The above square is NOT a magic square.