File Magicsquareddtxt Author Cmpt 145 Testing Program 3×3 Magic Square Detection Design Do Q43891923



![Output: False Reason: First row does not sum to 15 # 3. Inputs: [[5, 5, 5], [3, 2, 1], [5, 5, 5]] Output: False Reason: Secon](https://media.cheggcdn.com/media/495/4956cfdb-f06e-4fd2-8c53-403dd7eabe77/php78mpWC.png)
![# 3. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value middle row I](https://media.cheggcdn.com/media/b25/b25bb2fd-8f65-40c0-9354-2e614c16b340/phpf8pgu4.png)
![# Test cases for Algorithm 2 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: Magic Square Inputs: [[1, 9, 6],](https://media.cheggcdn.com/media/f58/f584e5e0-a2d5-4ee2-bf9b-12008994334d/php0lDIua.png)


![for i in range(0, n): colSum = 0 for j in range (0, n) : colSum += int (matrix[j][i]) 37 38 39- 40 41 # check if every column](https://media.cheggcdn.com/media/6da/6dac5668-d51f-4b9d-9e0d-d20ae825c3fe/phpv284po.png)
# File: MagicSquareDD.txt # Author: CMPT 145 # do testing of the program ##################################################################### # 3×3 Magic Square Detection # Design Document # Application’s Purpose: To determine if a given arrangement of 9 numbers is a magic square or not. ** Note: this program does not try to create a magic square! # Magic Square Definition: – 3×3 grid contains integers 1 through 9 every integer in 1-9 must appear exactly once all rows, columns, and diagonals of the grid add up to 15 E.g. Magic not Magic 8 16 19 6 3 5 7 49 2 5 37 4 8 2 # High level behaviour (main) Ask for the equence of numbers (“square”) Check whether it is magic! Program should display “YES” if it’s magic, “NO” otherwise ##################################################################### ###################################################################### Algorithms in Bottom-to-Top Order ###################################################################### # Algorithm 2.4: check_diagonals(square) Inputs: a 3×3 list of integers Return: True if all the diagonals sum to 15 23 #3 False otherwise 1. check the sum of the values from upward diagonal 2. check the sum of the values from downward diagonal 3. if any one of the checks resulted in an incorrect sum, return False otherwise, return True #3 #3 # Test cases for Algorithm 2.4 # 1. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: Both diagonal entries sum to 15 Inputs: [[1, 9, 6], [5, 3, 7], [4, 8, 2]] Output: False Reason: Both diagonal entries do not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 0]] Output: False Reason: Down-right diagonal does not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [0, 5, 5]] Output: False Reason: Up-right diagonal does not sum to 15 # 3. # 4. # Algorithm 2.3: check_columns (square) Inputs: a 3×3 list of integers #3 Return: True if all the columns sum to 15 False otherwise 1. check the sum of the values from first column 2. check the sum of the values from second column #3 # 3. check the sum of the values from third column # # # # # ## # # # # # # # 4. if any one of the checks resulted in an incorrect sum, return False otherwise, return True # Test cases for Algorithm 2.3 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All three columns sum to 15 # 1. # 2. Inputs: [[5, 5, 5], [5, 5, 5], [0, 5, 5]] Output: False Reason: First column does not sum to 15 # 3. Inputs: [[5, 5, 5], [5, 5, 5], [5, 0, 5]] Output: False 23 Reason: Second column does not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 0]] Output: False # 4. %23 Reason: Third column does not sum to 15 # Algorithm 2.2: check_rows (square) Inputs: a 3×3 list of integers Return: True if all the rows sum to 15 False otherwise 1. check the sum of the values from first row 2. check the sum of the values from second row 3. check the sum of the values from third row # 4. if any one of the checks resulted in an incorrect sum, return False otherwise, return True # Test cases for Algorithm 2.2 # 1. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All three rows sum to 15 Inputs: [[1, 2, 31, [5, 5. 51, [5, 5, 511 # 2. Output: False Reason: First row does not sum to 15 # 3. Inputs: [[5, 5, 5], [3, 2, 1], [5, 5, 5]] Output: False Reason: Second row does not sum to 15 # 4. Inputs: [[5, 5, 5], [5, 5, 5], [6, 2, 4]] Output: False Reason: Third row does not sum to 15 #### ####### # Algorithm 2.1: check_range(square) Inputs: a 3×3 list of integers Return: True if the square contains all the integers 1 False otherwise 1. Create a list of 9 boolean values, called seen Initialize every value in seen to False 2. for every value in the square seen[value-1] True 3. if any value in seen is still False, return False otherwise, return True # Known Bug: check_range () fails badly if square contains values out of range! # Test cases for Algorithm 2.4 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All entries in range are present # 2. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 4]] Output: False Reason: Entries in correct range, one missing value last row # 3. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value middle row # 1. # 3. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value middle row Inputs: [[4, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value first row # 4. 23 # 5. Inputs: [[8, -1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entry out of range first row Inputs: [[8, 1, 6], [1, 5, 0], [4, 9, 2]] Output: False Reason: Entry out of range second row # 6. # 7. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, -2]] Output: False Reason: Entry out of range last row # Algorithm 2: check_square(square) Inputs: a 3×3 list of integers Return: True if the square has all the properties of a magic square; False otherwise 1. To check the square has all integers 1 … 9 call function check_range() 2. To check 3 rows of the square sum to 15 call function check_rows () 3. To check 3 columns of the square sum to 15 call function check_columns () 4. To check 2 diagonals of the square sum to 15 call function check_diagonals () 5. If any of the checks above is False, return False # Test cases for Algorithm 2 Inputs: [[8, 1, 61, [3, 5. 71, [4, 9, 211 # 1. # # # # # # # # # # # # # # # # # # # # # # # # # # # # Test cases for Algorithm 2 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: Magic Square Inputs: [[1, 9, 6], [5, 3, 7], [4, 8, 2]] Output: False Reason: All values present, sums not okay Inputs: [[-8, 1, 6], [3, -5, 7], [4, 9, -2]] Output: False Reason: Values out of range Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 5]] Output: False Reason: All row, column, diagonal sums okay, values missing Inputs: [[6, 8, 1], [7, 3, 5], [2, 4, 9]] Output: False Reason: Range okay, row and column sums okay, diagonal sums not okay Inputs: [[8, 1, 6], [7, 5, 3], [4, 9, 2]] Output: False Reason: Range okay, row and diagonal sums okay, column sums not okay # 7. # 1. # 2. # 3. # 4. # 5. # 6. Inputs: [[4, 1, 2], [3, 5, 7], [8, 9, 6]] Output: False Reason: Range okay, column and diagonal sums okay, row sums not okay # Algorithm 1.1: get_square() Inputs: none Return: returns a 3×3 list of lists 1. Display a friendly prompt 2. Obtain 9 integers from the console 3. Puts them in a 3×3 list of lists # # # # # # ################# # Algorithm 1: main() Inputs: nothing Return: 0 1. Ask for the sequence of numbers using function get_square() 2. Check that it is magic by calling function check_square() 3. Display “YES” if it’s magic, “NO” otherwise ##: # End of Design Document %23 %23 %23 %23 %23 %23 main.py 8. 9- def isMagicSquare(matrix,n=3) : # calculate the sum of # the prime diagonal d1 = 0 11 12 13 14 for i in range(0, n) : d1 = int(d1) + int(matrix[i][i] ) # the secondary diagonal d2 = 0 for i in range(0, n) : d2 = int(d2)+ int(matrix[i][n-i-1] ) 15- 16 17 18 19 20 21 if(d1!=d2) : return False 22- 23 24 # For sums of Rows for i in range(0, n) : rowSum = 0; for j in range(0, n) : rowSum += int (matrix[i][j] ) 25 26 27 28 29 30 # check if every row sum is # equal to prime diagonal sum if (rowSum != d1) : 31 32 33- return False 34 35 # For sums of Columns for i in range(0, n): colSum = 0 36 37- 38 111 1 11L 112N 22 222 22 N2I for i in range(0, n): colSum = 0 for j in range (0, n) : colSum += int (matrix[j][i]) 37 38 39- 40 41 # check if every column sum is # equal to prime diagonal sum if (d1 != colSum) : 42 43 44 45 return False 46 47 return True 48 49 #Driver Function rows, cols = 3, 3 50 51 52 print(“Enter the entries row wise:”) # For user input 53 [input () for x in range (rows)] for y in range(cols)] 54 square = 55 # For printing the matrix 56 57 print(“The square to be checked is: “) 58 – for i in range (rows): for j in range(cols): print(square[i][j], end = print() 59 “) 60 61 62 63 #print yes if it is magic square and no if it is not a magic square 64 – if(isMagicSquare(square)): print(“Yes”) 65 66- else: print(“No”) 67 Show transcribed image text # File: MagicSquareDD.txt # Author: CMPT 145 # do testing of the program ##################################################################### # 3×3 Magic Square Detection # Design Document # Application’s Purpose: To determine if a given arrangement of 9 numbers is a magic square or not. ** Note: this program does not try to create a magic square! # Magic Square Definition: – 3×3 grid contains integers 1 through 9 every integer in 1-9 must appear exactly once all rows, columns, and diagonals of the grid add up to 15 E.g. Magic not Magic 8 16 19 6 3 5 7 49 2 5 37 4 8 2 # High level behaviour (main) Ask for the equence of numbers (“square”) Check whether it is magic! Program should display “YES” if it’s magic, “NO” otherwise ##################################################################### ###################################################################### Algorithms in Bottom-to-Top Order ######################################################################
# Algorithm 2.4: check_diagonals(square) Inputs: a 3×3 list of integers Return: True if all the diagonals sum to 15 23 #3 False otherwise 1. check the sum of the values from upward diagonal 2. check the sum of the values from downward diagonal 3. if any one of the checks resulted in an incorrect sum, return False otherwise, return True #3 #3 # Test cases for Algorithm 2.4 # 1. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: Both diagonal entries sum to 15 Inputs: [[1, 9, 6], [5, 3, 7], [4, 8, 2]] Output: False Reason: Both diagonal entries do not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 0]] Output: False Reason: Down-right diagonal does not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [0, 5, 5]] Output: False Reason: Up-right diagonal does not sum to 15 # 3. # 4. # Algorithm 2.3: check_columns (square) Inputs: a 3×3 list of integers #3 Return: True if all the columns sum to 15 False otherwise 1. check the sum of the values from first column 2. check the sum of the values from second column #3 # 3. check the sum of the values from third column # # # # # ## # # # # # #
# 4. if any one of the checks resulted in an incorrect sum, return False otherwise, return True # Test cases for Algorithm 2.3 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All three columns sum to 15 # 1. # 2. Inputs: [[5, 5, 5], [5, 5, 5], [0, 5, 5]] Output: False Reason: First column does not sum to 15 # 3. Inputs: [[5, 5, 5], [5, 5, 5], [5, 0, 5]] Output: False 23 Reason: Second column does not sum to 15 Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 0]] Output: False # 4. %23 Reason: Third column does not sum to 15 # Algorithm 2.2: check_rows (square) Inputs: a 3×3 list of integers Return: True if all the rows sum to 15 False otherwise 1. check the sum of the values from first row 2. check the sum of the values from second row 3. check the sum of the values from third row # 4. if any one of the checks resulted in an incorrect sum, return False otherwise, return True # Test cases for Algorithm 2.2 # 1. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All three rows sum to 15 Inputs: [[1, 2, 31, [5, 5. 51, [5, 5, 511 # 2.
Output: False Reason: First row does not sum to 15 # 3. Inputs: [[5, 5, 5], [3, 2, 1], [5, 5, 5]] Output: False Reason: Second row does not sum to 15 # 4. Inputs: [[5, 5, 5], [5, 5, 5], [6, 2, 4]] Output: False Reason: Third row does not sum to 15 #### ####### # Algorithm 2.1: check_range(square) Inputs: a 3×3 list of integers Return: True if the square contains all the integers 1 False otherwise 1. Create a list of 9 boolean values, called seen Initialize every value in seen to False 2. for every value in the square seen[value-1] True 3. if any value in seen is still False, return False otherwise, return True # Known Bug: check_range () fails badly if square contains values out of range! # Test cases for Algorithm 2.4 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: All entries in range are present # 2. Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 4]] Output: False Reason: Entries in correct range, one missing value last row # 3. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value middle row # 1.
# 3. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value middle row Inputs: [[4, 1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entries in correct range, one missing value first row # 4. 23 # 5. Inputs: [[8, -1, 6], [1, 5, 7], [4, 9, 2]] Output: False Reason: Entry out of range first row Inputs: [[8, 1, 6], [1, 5, 0], [4, 9, 2]] Output: False Reason: Entry out of range second row # 6. # 7. Inputs: [[8, 1, 6], [1, 5, 7], [4, 9, -2]] Output: False Reason: Entry out of range last row # Algorithm 2: check_square(square) Inputs: a 3×3 list of integers Return: True if the square has all the properties of a magic square; False otherwise 1. To check the square has all integers 1 … 9 call function check_range() 2. To check 3 rows of the square sum to 15 call function check_rows () 3. To check 3 columns of the square sum to 15 call function check_columns () 4. To check 2 diagonals of the square sum to 15 call function check_diagonals () 5. If any of the checks above is False, return False # Test cases for Algorithm 2 Inputs: [[8, 1, 61, [3, 5. 71, [4, 9, 211 # 1. # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Test cases for Algorithm 2 Inputs: [[8, 1, 6], [3, 5, 7], [4, 9, 2]] Output: True Reason: Magic Square Inputs: [[1, 9, 6], [5, 3, 7], [4, 8, 2]] Output: False Reason: All values present, sums not okay Inputs: [[-8, 1, 6], [3, -5, 7], [4, 9, -2]] Output: False Reason: Values out of range Inputs: [[5, 5, 5], [5, 5, 5], [5, 5, 5]] Output: False Reason: All row, column, diagonal sums okay, values missing Inputs: [[6, 8, 1], [7, 3, 5], [2, 4, 9]] Output: False Reason: Range okay, row and column sums okay, diagonal sums not okay Inputs: [[8, 1, 6], [7, 5, 3], [4, 9, 2]] Output: False Reason: Range okay, row and diagonal sums okay, column sums not okay # 7. # 1. # 2. # 3. # 4. # 5. # 6. Inputs: [[4, 1, 2], [3, 5, 7], [8, 9, 6]] Output: False Reason: Range okay, column and diagonal sums okay, row sums not okay # Algorithm 1.1: get_square() Inputs: none Return: returns a 3×3 list of lists 1. Display a friendly prompt 2. Obtain 9 integers from the console 3. Puts them in a 3×3 list of lists # # # # # #
################# # Algorithm 1: main() Inputs: nothing Return: 0 1. Ask for the sequence of numbers using function get_square() 2. Check that it is magic by calling function check_square() 3. Display “YES” if it’s magic, “NO” otherwise ##: # End of Design Document %23 %23 %23 %23 %23 %23
main.py 8. 9- def isMagicSquare(matrix,n=3) : # calculate the sum of # the prime diagonal d1 = 0 11 12 13 14 for i in range(0, n) : d1 = int(d1) + int(matrix[i][i] ) # the secondary diagonal d2 = 0 for i in range(0, n) : d2 = int(d2)+ int(matrix[i][n-i-1] ) 15- 16 17 18 19 20 21 if(d1!=d2) : return False 22- 23 24 # For sums of Rows for i in range(0, n) : rowSum = 0; for j in range(0, n) : rowSum += int (matrix[i][j] ) 25 26 27 28 29 30 # check if every row sum is # equal to prime diagonal sum if (rowSum != d1) : 31 32 33- return False 34 35 # For sums of Columns for i in range(0, n): colSum = 0 36 37- 38 111 1 11L 112N 22 222 22 N2I
for i in range(0, n): colSum = 0 for j in range (0, n) : colSum += int (matrix[j][i]) 37 38 39- 40 41 # check if every column sum is # equal to prime diagonal sum if (d1 != colSum) : 42 43 44 45 return False 46 47 return True 48 49 #Driver Function rows, cols = 3, 3 50 51 52 print(“Enter the entries row wise:”) # For user input 53 [input () for x in range (rows)] for y in range(cols)] 54 square = 55 # For printing the matrix 56 57 print(“The square to be checked is: “) 58 – for i in range (rows): for j in range(cols): print(square[i][j], end = print() 59 “) 60 61 62 63 #print yes if it is magic square and no if it is not a magic square 64 – if(isMagicSquare(square)): print(“Yes”) 65 66- else: print(“No”) 67
Expert Answer
Answer to # File: MagicSquareDD.txt # Author: CMPT 145 # do testing of the program ###############################################…
OR