def test_logical(): f = open('input1') p = sudoku(inputfile=f) s = p.solve_logical() soln = sudoku(inputfile=open('soln1')) print soln print s eq_(s, soln)
def build_puzzle(blanks=45): g = s.sudoku() p, x = g.create_puzzle(num_blanks=blanks) pl = p.to_list() xl = x.to_list() js = {'to_solve': pl, 'solved': xl} return jsonify(js)
def test_solve(self): input = ".........16.....578..6.3..43..5.4..1.2..9..6...8...5....7...2.....7.6......341..." output = [[7, 9, 3, 4, 1, 5, 6, 8, 2], [1, 6, 4, 2, 8, 9, 3, 5, 7], [8, 5, 2, 6, 7, 3, 9, 1, 4], [3, 7, 9, 5, 6, 4, 8, 2, 1], [5, 2, 1, 8, 9, 7, 4, 6, 3], [6, 4, 8, 1, 3, 2, 5, 7, 9], [4, 1, 7, 9, 5, 8, 2, 3, 6], [9, 3, 5, 7, 2, 6, 1, 4, 8], [2, 8, 6, 3, 4, 1, 7, 9, 5]] self.assertEqual(sudoku.sudoku(input), output)
def main(): #open file fv = open("input.txt","r") #keep track of sudoku number being solved i = 1 #iterate through file for each line for x in fv: print("Sudoku #" + str(i)) i +=1 #check if sudoku is valid if len(x) >= 82 and len(x[:-1]) == 81: board = x[:81] print_board(string_to_board(board)) s = sudoku(board) solved = ac3(s) #use the ac3 algorithm # print(s.variables) # print(s.domains) # print(s.constraints) # print(s.neighbors) #if solved by ac3, print board if solved and s.solved(): string_board = "" for var in s.variables: string_board += str(s.domains[var][0]) print("Solved using AC3!") print_board(string_to_board(string_board)) else: string_board = "" for var in s.variables: if len(s.domains[var]) == 1: string_board += str(s.domains[var][0]) else: print(var, end=" ") print(s.domains[var]) #print("using AC3") #print_board(string_to_board(string_board)) """ else: #if not solved by ac3, use backtracking print("Not solved by AC3. Now using backtracking algorithm...", end = " ") if backtrack_solve(board): print("Solved using backtracking!") print_board(board) else: print("Not solvable") """ else: print("Sudoku is invalid")
def test_duplicate_nums_in_row_is_false(): grid = [[".", "1", ".", "1", ".", ".", ".", ".", "."], [".", ".", "6", ".", ".", ".", ".", ".", "."], [".", ".", ".", ".", ".", ".", ".", ".", "."], [".", ".", "1", ".", ".", ".", ".", ".", "."], [".", "6", "7", ".", ".", ".", ".", ".", "9"], [".", ".", ".", ".", ".", ".", "8", "1", "."], [".", "3", ".", ".", ".", ".", ".", ".", "6"], [".", ".", ".", ".", ".", "7", ".", ".", "."], [".", ".", ".", "5", ".", ".", ".", "7", "."]] assert sudoku(grid) == False
def test_sudoku(self): self.assertEqual( sudoku([[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]]), [[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3, 4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7], [8, 5, 9, 7, 6, 1, 4, 2, 3], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 6, 1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8, 6, 1, 7, 9]])
def test(self): puzzle = [[5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9]] solution = [[5, 3, 4, 6, 7, 8, 9, 1, 2], [6, 7, 2, 1, 9, 5, 3, 4, 8], [1, 9, 8, 3, 4, 2, 5, 6, 7], [8, 5, 9, 7, 6, 1, 4, 2, 3], [4, 2, 6, 8, 5, 3, 7, 9, 1], [7, 1, 3, 9, 2, 4, 8, 5, 6], [9, 6, 1, 5, 3, 7, 2, 8, 4], [2, 8, 7, 4, 1, 9, 6, 3, 5], [3, 4, 5, 2, 8, 6, 1, 7, 9]] Test.assert_equals( sudoku(puzzle), solution, "Incorrect solution for the following puzzle: " + str(puzzle))
def test_row_box_col(): f = open('soln1') p = sudoku(inputfile=f) box00 = p.box_values(0,0) box22 = p.box_values(2,2) eq_(box00, [3, 1, 6, 4, 8, 2, 5, 7, 9]) eq_(box00, box22) box66 = p.box_values(6,6) eq_(box66, [3, 6, 1, 5, 4, 9, 7, 8, 2]) box88 = p.box_values(8,8) eq_(box66, box88) row0 = p.row_values(0) eq_(row0, [3, 1, 6, 8, 7, 9, 4, 2, 5]) col8 = p.col_values(8) eq_(col8, [5,7,3,4,8,6,1,9,2])
def test_SudokuEasy(self): solver = SAT_solver() board=[[None, None, None, 4, None, None, None, 5, None], [None, None, None, None, 1, None, 3, 6, None], [None, None, 8, None, None, 6, 9, 4, 7], [1, None, 2, None, None, None, None, 9, 5], [None, 9, None, 2, None, 1, None, None, None], [None, None, None, 5, 9, 3, None, None, None], [4, None, None, None, None, None, 1, 7, 9], [7, 2, None, 1, None, None, None, None, None], [None, 8, None, None, None, 9, None, 2, None]] formula = sudoku(board) solution = solver.solve(FlatCNF(formula)) self.assertTrue(solution[0]) self.assertEqual("true", formula.nnf().cnf().evaluate(solution[1]).__class__.__name__)
def test_SudokuCustom(self): solver = SAT_solver() board=[[4, 8, None, 1, 6, None, None, None, 7], [1, None, 7, 4, None, 3, 6, None, None], [3, None, None, 5, None, None, 4, 2, None], [None, 9, None, None, 3, 2, 7, None, 4], [None, None, None, None, None, None, None, None, None], [2, None, 4, 8, 1, None, None, 6, None], [None, 4, 1, None, None, 8, None, None, 6], [None, None, 6, 7, None, 1, 9, None, 3], [7, None, None, None, 9, 6, None, 4, None]] formula = sudoku(board) solution = solver.solve(formula,True) self.assertTrue(solution[0]) self.assertEqual("true", formula.nnf().cnf().evaluate(solution[1]).__class__.__name__)
def test_SudokuMedium(self): solver = SAT_solver() board=[[None, None, 9, None, 6, 4, None, None, 1], [None, None, None, None, 5, None, None, None, None], [4, 6, None, 1, None, 7, None, None, 8], [None, None, None, None, None, None, None, 9, None], [None, None, None, None, 3, None, None, 1, None], [3, None, None, None, None, None, None, 4, None], [None, 4, 8, None, None, None, 2, None, None], [2, None, 7, None, 4, 5, None, 8, 6], [5, None, None, None, None, None, None, None, None]] formula = sudoku(board) solution = solver.solve(FlatCNF(formula)) self.assertTrue(solution[0]) self.assertEqual("true", formula.nnf().cnf().evaluate(solution[1]).__class__.__name__)
def test_rand(self): from random import shuffle sp = [ [ '605720039400005100020100004090030706100809005204050080800003020002900001350067408', '615724839487395162923186574598432716136879245274651983849513627762948351351267498' ], [ '008030540300407900410008002043502060500000008060309410100800027005603004029070800', '978231546352467981416958372843512769591746238267389415134895627785623194629174853' ], [ '600108203020040090803005400504607009030000050700803102001700906080030020302904005', '645198273127346598893275461514627389238419657769853142451782936986531724372964815' ], [ '019060540000000000820974036001503800000000000002701600750138092000000000083040710', '319862547467315289825974136671593824538426971942781653756138492194257368283649715' ], [ '046000000902060008008400250000800070500702003010006000064003900300080102000000730', '146258397952367418738491256623849571589712643417536829264173985375984162891625734' ], [ '006020050002000194000100207607082019085070030000605400090013040001009000730008900', '416927853372856194859134267647382519985471632123695478598213746261749385734568921' ] ] shuffle(sp) def str_to_matrix(str): return [[int(c) for c in str[i:i + 9]] for i in range(0, len(str), 9)] def matrix_to_str(matrix): return ''.join(str(c) for row in matrix for c in row) for i, pair in enumerate(sp): puzzle, solution = pair self.assertEqual(matrix_to_str(sudoku(str_to_matrix(puzzle))), solution, 'Puzzle {}'.format(i))
#!/usr/bin/env python import sys import sudoku if __name__ == "__main__": if len(sys.argv) > 1: files = [open(filename) for filename in sys.argv[1:]] else: files = [sys.stdin] for file in files: print "=" * 80 sudoku_tab = [[ int(nb) if nb > '0' and nb <= '9' else 0 for nb in line.strip() ] for line in file] s = sudoku.sudoku(tab=sudoku_tab) print "=" * 80 s.init_availables() s.show_amounts() s.show_availables() s.show() #sys.exit(1) print "=" * 80 s.resolve() print "=" * 80 s.show_amounts() s.show_availables() s.show() print "%d actions, %d routines, %d missings" % (s.actions, s.routines, s.missings) print "=" * 80
def SetUp(self): self._sudoku = sudoku.sudoku()
def core(): g.pullinput() s = sudoku(g.m) if s.solve(): g.pushoutput(s)
#!/usr/bin/env python import sys import sudoku if __name__ == "__main__": if len(sys.argv) > 1: files = [open(filename) for filename in sys.argv[1:]] else: files = [sys.stdin] for file in files: print "=" * 80 sudoku_tab = [[int(nb) if nb > '0' and nb <= '9' else 0 for nb in line.strip()] for line in file] s = sudoku.sudoku(tab = sudoku_tab) print "=" * 80 s.init_availables() s.show_amounts() s.show_availables() s.show() #sys.exit(1) print "=" * 80 s.resolve() print "=" * 80 s.show_amounts() s.show_availables() s.show() print "%d actions, %d routines, %d missings" % (s.actions, s.routines, s.missings) print "=" * 80 del s
def test_verificar_submatriz_3(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.verificar_submatriz(5, 7, 5) self.assertTrue(resultado)
def test_verificar_columna_fila_3(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.verificar_columna_fila(3, 5, 4) self.assertTrue(resultado)
from sudoku import sudoku from time import time def read(): grid = [] for r in range(1, 10): row = input("row {}: ".format(r)) while len(row) != 9: print("invalid row") row = input("row {}: ".format(r)) grid.append([int(v) for v in row]) return grid if __name__ == "__main__": s = sudoku() s.grid = read() start = time() solved = s.solve() end = time() print(s) print("solved: {}".format(solved)) print("time: {}".format(end - start))
def main(): s = sudoku.sudoku() dfs = depth_first_search.depth_first_search(s) dfs.execute()
import copy from board_generator import board_generator obj = board_generator() mat = obj.get_board() ques_bool = [[False for x in range(9)] for y in range(9)] for i in range(len(mat)): for j in range(len(mat[0])): if(mat[i][j] != 0): ques_bool[i][j] = True s = sudoku.sudoku(mat) c = copy.deepcopy(s) c.solve() WIDTH = 600 HIGHT = 600 WHITE = (255,255,255) GREY = (195,191,191) YGREY = (206,195,96) DRED = (193,0,0) RED = (255,0,0) BLUE_CANV = (186,201,236) BLACK = (0,0,0) LBLACK = (63,63,68) LLBLACK = (82,81,97) LIGHT_BLUE = (96,216,232) GREEN = (0,255,0)
[1, None, 7, 4, None, 3, 6, None, None], [3, None, None, 5, None, None, 4, 2, None], [None, 9, None, None, 3, 2, 7, None, 4], [None, None, None, None, None, None, None, None, None], [2, None, 4, 8, 1, None, None, 6, None], [None, 4, 1, None, None, 8, None, None, 6], [None, None, 6, 7, None, 1, 9, None, 3], [7, None, None, None, 9, 6, None, 4, None], ] # print sudoku from board definition. print "Lab exercise:" print printSudoku(board) # construct logical formula from board definition. formula = sudoku(board) # solve formula using SAT solver(singlethreaded) result = solver.solve(formula) print "Solution:" # process and print result of sat solver. print printSudoku(processResult(result[1])) medium_board = [ [None, None, 5, None, 6, 3, 1, 2, None], [None, None, 9, None, None, 1, None, 5, None], [1, None, None, None, None, 8, 9, None, 6], [None, None, None, None, 5, None, 8, None, 2], [None, 5, None, None, None, None, None, 1, None], [6, None, 1, None, 9, None, None, None, None],
parser.add_argument('-solve', action='store_true', help='Call SatSolver and pretty print solution (need glucose_1.0 compiled in ./gucose_1.0)') parser.add_argument('-test', action='store_true', help='Make test') parser.add_argument('-t','--timeout', action='store', type=int, default="0", help='Test Timeout in milliseconds (default: nolimit)') parser.add_argument('--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() # Get sudoku file and properties f = open(args.sudokufile) order, m = map(int, f.readline().split()[1:3]) n = order / m sudoku_list = [f.readline().split() for i in range(order)] # Encode Sudoku to Dimacs CNF Formula initial_time = time.clock() s=sudoku.sudoku(args.sudokufile) if args.encoding == "extended": cnf, decoding_map = s.extended_translate_to_CNF() elif args.encoding == "optimized": cnf, decoding_map = s.optimized_translate_to_CNF() else: print "Invalid Encoding Option." sys.exit() cnf.print_dimacs(args.output) encoding_time = time.clock() - initial_time if args.test: print "Encoding Time:", encoding_time def print_solution(solution): counter = 0 print "|", for var in map(int,solution):
def test(): f = open('input1') p = sudoku(inputfile=f) s = p.solve_brute_force() soln = sudoku(inputfile=open('soln1')) eq_(s, soln)
def test_sudoku_mostrar(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') game.mostar() self.assertTrue(game)
''' Created on Aug 24, 2011 @author: Amparo Luna y Rodrigo Zurek ''' import sudoku if __name__ == '__main__': msudoku=sudoku.sudoku() msudoku.readSudoku() msudoku.cycleSudoku()
def test_verificar_submatriz_1(self): game = sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.verificar_submatriz(7, 2, 6) self.assertFalse(resultado)
import numpy as np from sudoku import sudoku #board = np.load('./test-sudokus/evil2.npy') board = np.zeros((9, 9), 'int') my_sudoku = sudoku(board=board) my_sudoku.solve() print('Filled board:') my_sudoku.print_board()
def test_run_sudoku_3(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.run_sudoku(8, 6, 4) self.assertFalse(resultado)
''' Created on 2010. 6. 19. @author: user ''' from sudoku import sudoku f = open('sudoku.txt', 'r') sum=0 for x in range(50): f.readline() map = [] for y in range(9): map.append(f.readline().strip()) result = sudoku(map) result.check() result.check(1) result.check(2) sum+=int(result.simple_text()[0:3]) print sum
# Convert the cell to float 32 type for digit recognizer cellf32 = np.array(cell).astype(np.float32).reshape( 1, KNN_DIM * KNN_DIM) ret, result, neighbours, dist = dr.test(cellf32) #print(y, x, result[0]) board[y][x] = int(result[0]) # Dump cells to a file to serve as test vectors for digit recognizer if (DUMP_KNN_INPUT == 1): numKnnInputs += 1 cell.astype('int8').tofile(fdDumpKnnInput) print("Printing the board") for row in board: print(row) sudoku = sudokuSolver.sudoku(hCellDim=3, vCellDim=3) if (sudoku.solveSudoku(board)): print("Sodoku solved") for row in board: print(row) else: print("Sodoku is not solvable") if (DUMP_KNN_INPUT == 1): fdDumpKnnInput.seek(4) fdDumpKnnInput.write(struct.pack('i', numKnnInputs)) fdDumpKnnInput.close() # Sudoku - 1 # digits = [4, 1, 2, 9, 7, 5, 2, 3, 8, 7, 8, 6, 1, 3, 6, 2, 1, 5, 4, 3, 7, 3, 6, 8, 6, 2, 3, 7, 1, 4, 8, 9, 6, 5, 1, 7] # Sudoku - 2 # digits = [6, 2, 7, 1, 8, 3, 7, 1, 7, 8, 4, 6, 9, 2, 2, 8, 3, 1, 6, 6, 1, 3]
import sys sys.path.append('./game') from tictactoe import ticTacToe from connect4 import connect4 from sudoku import sudoku if __name__ == '__main__': print("\t Bienvenue dans l'antre de Joseph, à quoi voulez-vous jouer?: ") print("1- Tictactoe") print("2- Connect 4") print("3- Sudoku") while True: choix = input('Votre choix: ') if len(choix) == 1 and ord(choix) >= 49 and ord(choix) <= 51: if int(choix) == 1: ticTacToe() break elif int(choix) == 2: connect4() break elif int(choix) == 3: sudoku() break print("Mauvaise Saisie") print('\t Joseph a hâte de vous revoir.', end="\n\n\n")
def __init__(self): self.interfaz = sudoku( '53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79' )
def test_ingresar_3(self): game = sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.ingresar(-4, 8, 4) self.assertFalse(resultado)
print "sudoku" board=[[4, 8, None, 1, 6, None, None, None, 7], [1, None, 7, 4, None, 3, 6, None, None], [3, None, None, 5, None, None, 4, 2, None], [None, 9, None, None, 3, 2, 7, None, 4], [None, None, None, None, None, None, None, None, None], [2, None, 4, 8, 1, None, None, 6, None], [None, 4, 1, None, None, 8, None, None, 6], [None, None, 6, 7, None, 1, 9, None, 3], [7, None, None, None, 9, 6, None, 4, None]] print printSudoku(board) import sys formula = sudoku(board) # sys.stderr.write( unicode(sudoku(board)[0].nnf().cnf().simplify()).encode("utf-8") ) result = solver.solve(formula) print printSudoku(processResult(result[1])) print "=================================================" print "=================================================" print "SAT Solver - mt - Testing" print "=================================================" solver = SAT_solver() for i in globals().keys(): if i[:4] == "expr": print i, ":", unicode(globals()[i]), "->", solver.solve(globals()[i],True)
def test_valores_tablero_3(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.valores_tablero(8, 6) self.assertTrue(resultado)
Created on Thu May 9 04:19:14 2019 @author: araman """ import sudoku """ sudokuInst = sudoku.sudoku(hCellDim=3, vCellDim=2) board = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 5, 0, 1], [2, 0, 0, 0, 0, 6], [6, 0, 0, 0, 0, 3], [4, 0, 5, 3, 0, 0], [0, 0, 0, 0, 0, 0]] """ sudokuInst = sudoku.sudoku(hCellDim=3, vCellDim=3) """ board = [[6, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 0, 0, 0, 4, 0, 0, 0], [0, 7, 1, 0, 6, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 8, 0, 6], [0, 5, 9, 0, 0, 0, 0, 0, 4], [0, 2, 0, 0, 0, 0, 0, 0, 0], [0, 0, 8, 6, 0, 7, 5, 3, 0], [9, 0, 0, 4, 0, 0, 0, 0, 2], [3, 0, 0, 0, 0, 0, 0, 0, 9]] """ board = [[4, 0, 1, 2, 9, 0, 0, 7, 5], [2, 0, 0, 3, 0, 0, 8, 0, 0], [0, 7, 0, 0, 8, 0, 0, 0, 6], [0, 0, 0, 1, 0, 3, 0, 6, 2], [1, 0, 5, 0, 0, 0, 4, 0, 3], [7, 3, 0, 6, 0, 8, 0, 0, 0], [6, 0, 0, 0, 2, 0, 0, 3, 0], [0, 0, 7, 0, 0, 1, 0, 0, 4],
def test_verificar_columna_fila_1(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.verificar_columna_fila(0, 0, 5) self.assertFalse(resultado)
from sudoku import sudoku if __name__ == '__main__': sudokuGame = sudoku() sudokuGame.run()
def test2(): f = open('input2') p = sudoku(inputfile=f) p.solve_brute_force()
def test_ingresar_1(self): game=sudoku('53xx7xxxx6xx195xxxx98xxxx6x8xxx6xxx34xx8x3xx17xxx2xxx6x6xxxx28xxxx419xx5xxxx8xx79') resultado = game.ingresar(1, 3, 1) self.assertTrue(resultado)
action='store', type=int, default="0", help='Test Timeout in milliseconds (default: nolimit)') parser.add_argument('--version', action='version', version='%(prog)s 1.0') args = parser.parse_args() # Get sudoku file and properties f = open(args.sudokufile) order, m = map(int, f.readline().split()[1:3]) n = order / m sudoku_list = [f.readline().split() for i in range(order)] # Encode Sudoku to Dimacs CNF Formula initial_time = time.clock() s = sudoku.sudoku(args.sudokufile) if args.encoding == "extended": cnf, decoding_map = s.extended_translate_to_CNF() elif args.encoding == "optimized": cnf, decoding_map = s.optimized_translate_to_CNF() else: print "Invalid Encoding Option." sys.exit() cnf.print_dimacs(args.output) encoding_time = time.clock() - initial_time if args.test: print "Encoding Time:", encoding_time def print_solution(solution): counter = 0 print "|",