Lo Shu Magic Square The Lo Shu Magic Square is a grid with 3
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()
