Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3

Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-8. The Lo Shu Magic Square has the following properties: bullet The grid contains the numbers 1 through 9 exactly. bullet The sum of each row, each column, and each diagonal all up to the same number. This is shown in Figure 7-9. In a program you can simulate a magic square using a two-dimensional list. Write a function that accepts a two-dimensional list an argument and determines whether the list is a Lo Shu magic Square. Test the function in a program.

Solution

Here is the code for you:

#!/usr/bin/python
def checkLst(lst):
for l in range(1, 10):
if l not in lst:
return False
return True
def isValid(grid):
for row in grid:
if checkLst(row) == False:
return False
for column in range(0, 9):
currentColumn.append(grid[row][column])
if checkLst(currentColumn) == False:
return False
startRow = 0
startCurrent = 0
for boxNumber in range(0, 9):
currentBox = []
for row in range(startBox, startBox + 3):
for column in range(startColumn, startColumn + 3):
currentBox.append(gred[row][column])
if checkLst(currentBox) == False:
return False
startColumn += 3
if startColumn == 9:
starColumn = 0
startRow += 3
return True
def main():
fileName = [\"input.txt\"]
for file in fileName:
grids = [] #store solution
f = open(file)
for line in f:
line = line.split()
grid = list(map(int, line)) # string to integer
grids.append(grid)
for grid in grids: #each line
if isValid(grid):
print(\"Valid\")
else:
print(\"Invalid\")
main()

 Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3 rows and 3 columns, shown in Figure 7-8. The Lo Shu Magic Square has the following properties: bul

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site