def test_convertToSets(self): problem = sudoku.read_sudoku("./data/data1.txt") #read the puzzle sets = sudoku.read_sudoku("./data/convertToSetsTest.txt") #read the coverted sets emptySets = sudoku.read_sudoku("./data/emptySets.txt") self.assertEqual(emptySets, sudoku.convertToSets([[0] * 9] * 9)) self.assertEqual([sets[0]], sudoku.convertToSets([problem[0]])) self.assertEqual(sets, sudoku.convertToSets(problem))
def test_isSolved(self): problem = sudoku.read_sudoku("./data/data1.txt") #read the puzzle result = sudoku.read_sudoku("./data/result.txt") #read the result emptySets = sudoku.read_sudoku("./data/emptySets.txt") self.assertFalse(sudoku.isSolved(emptySets)) self.assertFalse(sudoku.isSolved(sudoku.convertToSets(problem))) self.assertTrue(sudoku.isSolved(sudoku.convertToSets(result)))
def test_eliminate(self): loc0 = (0,1) eliminate = sudoku.read_sudoku("./data/eliminateTest.txt") #read the eliminate result problem = sudoku.read_sudoku("./data/data1.txt") #read the puzzle problemSets = sudoku.convertToSets(problem) listOfLocation = sudoku.getRowLocations(loc0[0]) + sudoku.getColumnLocations(loc0[1]) \ + sudoku.getBoxLocations(loc0) self.assertEqual(12, sudoku.eliminate(problemSets, loc0, listOfLocation)) self.assertEqual(eliminate, problemSets)
def test_solve(self): result = sudoku.read_sudoku("./data/result.txt") #read the result problem = sudoku.read_sudoku("./data/data1.txt") #read the puzzle problemSets = sudoku.convertToSets(problem) self.assertTrue(sudoku.solve(problemSets)) self.assertEqual(result, sudoku.convertToInts(problemSets))
if __name__ == '__main__': if len(sys.argv) != 3: print("Usage: {} sudoku1 sudoku2".format(sys.argv[0]), file=sys.stderr) print() print( "checks if two sudokus match. That is, check that every cell matches, or ignore if the cell is 0." ) exit(1) try: # Open the two files f1 = open(sys.argv[1], 'r') f2 = open(sys.argv[2], 'r') # Read the puzzles s1 = read_sudoku(f1) s2 = read_sudoku(f2) # Close the files f1.close() f2.close() # Check every cell for i in range(len(s1)): # Ignore 0s if s1[i] == 0 or s2[i] == 0: continue # Check for equality if s1[i] != s2[i]: print("False") exit(0)