Python HW Program LoSho Square Pickle Import The input file for this program is
ID: 656403 • Letter: P
Question
Python HW Program LoSho Square Pickle Import
The input file for this program is a pickled file (Square.dat) that stores a collection of 3 X 3 two dimensional arrays (list) of integers.
Your assignment is to write a Python program to process each two dimensional array to determine which arrays satisfy the condition of a Lo Shu magic square. The condition for a Lo Shu magic square is that the sum of all columns, rows, and diagonals equals the same value.
As a key component of your solution design your code to use modules (methods).
Create and name variables appropriately. Using your code, test and report which arrays are Lo Shu magic squares and which are not. Because of the large numberor two dimensional arrays in the input file, please note the restricted information which must be reported as a result of processing the input file.
Be sure to process all of the input file. When your program is complete, the following is expected as outcome.
Expected Program Output
1. Count and report the total number of two dimensional arrays in the file.
2. Count and report the total number of two dimensional arrays which satisfy the condition for Lo Shu Magic Squares.
3. While all arrays are to be processed, as output, only print every fifth two dimensional array and also every array that is designated to be a Lo Shu Magic Square. When printing a line of output, you should indicate Yes or No, designating a two dimensional array as a magic square ornot a magic square. A line of output may look as simple as something like:
[[4, 9, 2], [3, 5, 7], [8, 1, 6]] YES
[[5, 3, 8], [4, 2, 7], [6, 9, 1]] NO
[[5, 1, 7], [9, 4, 2], [6, 3, 8]] NO
_________________________________________________________________
INPUT FILE Square.dat contains these possible lusho squares but in pickle formate: the file may change and need to read a longer or shorter file.
[[2, 5, 9], [1, 3, 4], [6, 7, 8]]
[[2, 7, 6], [9, 5, 1], [4, 3, 8]]
[[2, 7, 9], [5, 6, 8], [4, 3, 1]]
[[2, 9, 4], [7, 5, 3], [6, 1, 8]]
[[2, 9, 1], [4, 8, 7], [6, 3, 5]]
[[3, 9, 8], [7, 2, 5], [6, 4, 1]]
[[4, 3, 8], [7, 6, 2], [5, 9, 1]]
[[4, 3, 8], [9, 5, 1], [2, 7, 6]]
[[4, 5, 8], [6, 3, 7], [1, 9, 2]]
[[4, 7, 8], [5, 9, 1], [6, 2, 3]]
[[6, 5, 7], [3, 1, 8], [2, 9, 4]]
[[6, 1, 8], [7, 5, 3], [2, 9, 4]]
[[6, 7, 2], [1, 5, 9], [8, 3, 4]]
[[6, 7, 2], [1, 8, 3], [4, 9, 5]]
__________________________________________________________________
Explanation / Answer
def checkSquare(square,n):
checks1=''
for x in square:
for y in range(3):
if sum(x) == sum (square[y][y] for y in range(3)):
if sum(x)== sum(x[y] for x in square):
checks1 = 'Yes'
else:
checks1= 'No'
print("Square",str(n), checks1)
return None
def main():
square1 = [[2, 5, 9], [1, 3, 4], [6, 7, 8]]
checkSquare(square1,1)
square2 = [[2, 7, 6], [9, 5, 1], [4, 3, 8]]
checkSquare(square2,2)
square3 = [[2, 7, 9], [5, 6, 8], [4, 3, 1]]
checkSquare(square3,3)
square4 = [[2, 9, 4], [7, 5, 3], [6, 1, 8]]
checkSquare(square4,4)
square5 = [[2, 9, 1], [4, 8, 7], [6, 3, 5]]
checkSquare(square5,5)
square6 = [[3, 9, 8], [7, 2, 5], [6, 4, 1]]
checkSquare(square6,6)
square7 = [[4, 3, 8], [7, 6, 2], [5, 9, 1]]
checkSquare(square7,7)
square8 = [[4, 3, 8], [9, 5, 1], [2, 7, 6]]
checkSquare(square8,8)
square9 = [[4, 5, 8], [6, 3, 7], [1, 9, 2]]
checkSquare(square1,9)
square10 = [[4, 7, 8], [5, 9, 1], [6, 2, 3]]
checkSquare(square2,10)
square11 = [[6, 5, 7], [3, 1, 8], [2, 9, 4]]
checkSquare(square3,11)
square12 = [[6, 1, 8], [7, 5, 3], [2, 9, 4]]
checkSquare(square4,12)
square13 = [[6, 7, 2], [1, 5, 9], [8, 3, 4]]
checkSquare(square5,13)
square14 = [[6, 7, 2], [1, 8, 3], [4, 9, 5]]
checkSquare(square6,14)
main()
Sample Output:
Square 1
Square 2 Yes
Square 3
Square 4 Yes
Square 5
Square 6
Square 7
Square 8 Yes
Square 9
Square 10 Yes
Square 11
Square 12 Yes
Square 13
Square 14
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.