def task1(): grids = [0]*3 grids[0] = '3.6.7...........518.........1.4.5...7.....6.....2......2.....4.....8.3.....5.....' grids[1] = '85...24..72......9..4.........1.7..23.5...9...4...........8..7..17..........36.4.' grids[2] = '003020600900305001001806400008102900700000008006708200002609500800203009005010300' corner_case = {'G7': '1', 'G6': '3', 'G5': '5', 'G4': '6', 'G3': '2', 'G2': '9', 'G1': '8', 'G9': '4', 'G8': '7', 'C9': '2', 'C8': '1', 'C3': '5', 'C2': '8', 'C1': '6', 'C7': '169', 'C6': '4', 'C5': '3', 'C4': '7', 'A9': '5', 'A8': '3', 'F1': '', 'F2': '4', 'F3': '6', 'F4': '5', 'F5': '1', 'F6': '2', 'F7': '3', 'F8': '9', 'F9': '8', 'B4': '1', 'B5': '6', 'B6': '5', 'B7': '', 'B1': '2', 'B2': '3', 'B3': '9', 'B8': '4', 'B9': '7', 'I9': '3', 'I8': '5', 'I1': '1', 'I3': '4', 'I2': '6', 'I5': '7', 'I4': '8', 'I7': '2', 'I6': '9', 'A1': '4', 'A3': '7', 'A2': '1', 'E9': '7', 'A4': '9', 'A7': '8', 'A6': '', 'E5': '8', 'E4': '3', 'E7': '4', 'E6': '6', 'E1': '9', 'E3': '1', 'E2': '5', 'E8': '2', 'A5': '2', 'H8': '8', 'H9': '6', 'H2': '7', 'H3': '3', 'H1': '5', 'H6': '1', 'H7': '9', 'H4': '2', 'H5': '4', 'D8': '6', 'D9': '1', 'D6': '7', 'D7': '5', 'D4': '4', 'D5': '9', 'D2': '2', 'D3': '8', 'D1': ''} marks_for_parts = [0,0,0] for grid in grids: prob = SudokuSearchProblem(parse_grid(grid)) # Test Get Start State Function if prob.getStartState() == parse_grid(grid): marks_for_parts[0] += 1.0 / 6 # Test is Goal State Function if not prob.isGoalState(parse_grid(grids[1])): marks_for_parts[1] += 1.0/4 if prob.isGoalState(parse_grid(grids[2])): marks_for_parts[1] += 1.0/4 # Test the getSuccessors Function curr_vals = parse_grid(grids[1]) succs = prob.getSuccessors(curr_vals) if len(succs) > 1 and all(succ[0] == assign(curr_vals.copy(), succ[1][0], succ[1][1]) for succ in succs): marks_for_parts[2] += 1 if len(prob.getSuccessors(corner_case)) == 0: marks_for_parts[2] += 1 marks_for_parts_str = [str(x) for x in marks_for_parts] return ' + '.join(marks_for_parts_str) + " = " + str(sum(marks_for_parts)), 3
def mediumSort(grid): values = sudoku.parse_grid(grid) # Screen puzzles with clumped initial squares for i in range(9): row_sum = 0 col_sum = 0 for j in range(9): if grid[i*9 + j] != '.': row_sum += 1 if grid[i + j*9] != '.': col_sum += 1 if row_sum > 7 or row_sum < 2 or col_sum > 7 or col_sum < 2: return False # Limit initial squares givens = 0 for i in range(len(grid)): if grid[i] != '.': givens += 1 if givens > 33: return False # Ensure at least 8 of 9 numbers initially revealed, to avoid guessing initials = set(list(grid)) initials.remove('.') if not len(initials) >= 8: return False # Proxy difficult by number of squares solved by basic techniques solved = 0 for s in values: if len(values[s]) == 1: solved += 1 if solved < 81 and solved < 61: return False # Check for multiple solutions solutions = [] for i in range(30): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) if len(set(solutions)) > 1: return False return True
from sudoku import printboard, parse_grid hardestsudokuinworld=""" 850002400 720000009 004000000 000107002 305000900 040000000 000080070 017000000 000036040 """ printboard(parse_grid(hardestsudokuinworld)) #choose 3 from 36 (going across then down, find first pair) fails #choose 6 printboard(parse_grid(""" 850602400 720000009 004000000 000107002 305000900 040000000 000080070 017000000 000036040 """))
def get(self): hard_samples = [] h_singles = [] for i in range(10): total = Puzzle.all().filter('level =', 'Hard').order('-number').get() number = random.randint(1, total.number) hard = Puzzle.all().filter('level =', 'Hard').filter('number =', number).get() values = sudoku.parse_grid(hard.start) solved = 0 for s in values: if len(values[s]) == 1: solved += 1 h_singles.append(solved) solutions = [] for i in range(99): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) hard_samples.append(len(set(solutions))) extreme_samples = [] ex_singles = [] for i in range(10): total = Puzzle.all().filter('level =', 'Extreme').order('-number').get() number = random.randint(1, total.number) extreme = Puzzle.all().filter('level =', 'Extreme').filter('number =', number).get() values = sudoku.parse_grid(extreme.start) solved = 0 for s in values: if len(values[s]) == 1: solved += 1 ex_singles.append(solved) solutions = [] for i in range(99): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) extreme_samples.append(len(set(solutions))) template_values = { 'h_singles' : h_singles, 'ex_singles' : ex_singles, 'hard_samples' : hard_samples, 'extreme_samples' : extreme_samples } template = jinja_environment.get_template('sample.html') self.response.out.write(template.render(template_values))
def get(self): easy = ['8..5.2.6..2...358.....1.7427....125.5.......7.187....6682.9.....318...9..5.4.6..8', '3..76...2..9.5..6...2.9...42.74...36..16.92..69...54.75...4.3...1..3.7..7...12..9', '8.4..5.2...6.8....75192.38..4.6.....9.8.4.1.2.....2.7..15.69238....5.7...2.4..9.5', '4.37..65....9.4.71..1..543.3...9..26...6.1...67..8...4.863..7..23.1.6....57..81.3', '...76..9.....917.2.....46...13.8.24.8.6.4.3.5.54.7.86...79.....6.152.....2..38...', '2..6..34.....5...93..72.861..3.627.....8.7.....613.4..785.93..61...7.....69..1..7', '..54.3...8.6..243.37.......56....7.974.591.239.3....18.......96.826..1.7...1.72..'] medium = ['.7..6.34...25.9..7..1..4...7.81....4.........5....71.2...4..6..3..6.85...56.9..1.', '.524.....1..9..8..8...63.179.....6....81467....6.....448.61...9..1..7..6.....415.', '9.2........4.5.78.........36..2...9521.9.3.7849...7..61.........83.9.2........8.1', '..2.4...5.4.325..6.5...79.4.....247....5.4....987.....7.48...6.3..456.1.5...7.3..', '4...9..8..5.4.....98.1....78..67..2..2.....1..1..59..61....4.59.....2.3..3..6...4', '.8..6.43...4..1.8..934......3..287..6.......2..751..6......589..7.1..5...25.4..7.', '31.2..5............24.61.3784...91......8......73...6447.69.35............5..4.18'] hard = ['3....9.86..6.18.4.....6.5..5.....62.2..531..4.87.....5..5.2.....2.97.3..79.3....2', '.62..5...4...71.861.......26..89.2..82.....95..1.27..83.......459.34...7...7..96.', '.89..3.2...5.....4...1.95..95.62.3..6.......8..7.15.62..38.4...4.....8...9.7..64.'] extreme = ['2.1.8...5.3.7........1...9...5.4.73.4.75.16.9.86.9.5...4...2........6.7.6...7.2.8', '375..1....8..2.5.3..193....8.....1.7...3.2...1.3.....4....543..5.9.6..8....1..456', '8.735....5......8..91.2..4.4.....5...569.142...3.....6.4..6.13..7......4....489.7'] e_singles = [] e_solutions = [] for p in easy: grid = sudoku.parse_grid(p) solved = 0 for s in grid: if len(grid[s]) == 1: solved += 1 e_singles.append(solved) solutions = [] for i in range(10): list_values = [] dict_values = sudoku.searchMultiple(grid) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) e_solutions.append(len(set(solutions))) m_singles = [] m_solutions = [] for p in medium: grid = sudoku.parse_grid(p) solved = 0 for s in grid: if len(grid[s]) == 1: solved += 1 m_singles.append(solved) solutions = [] for i in range(10): list_values = [] dict_values = sudoku.searchMultiple(grid) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) m_solutions.append(len(set(solutions))) h_singles = [] h_solutions = [] for p in hard: grid = sudoku.parse_grid(p) solved = 0 for s in grid: if len(grid[s]) == 1: solved += 1 h_singles.append(solved) solutions = [] for i in range(30): list_values = [] dict_values = sudoku.searchMultiple(grid) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) h_solutions.append(len(set(solutions))) ex_singles = [] ex_solutions = [] for p in extreme: grid = sudoku.parse_grid(p) solved = 0 for s in grid: if len(grid[s]) == 1: solved += 1 ex_singles.append(solved) solutions = [] for i in range(30): list_values = [] dict_values = sudoku.searchMultiple(grid) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) ex_solutions.append(len(set(solutions))) template_values = { 'e_singles' : e_singles, 'm_singles' : m_singles, 'h_singles' : h_singles, 'ex_singles' : ex_singles, 'e_solutions' : e_solutions, 'm_solutions' : m_solutions, 'h_solutions' : h_solutions, 'ex_solutions' : ex_solutions } template = jinja_environment.get_template('measure.html') self.response.out.write(template.render(template_values))
def get(self): multiple = [] for i in range(10): initial, solution = generator.generate("Easy") solutions = [] values = sudoku.parse_grid(initial) for i in range(10): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) multiple.append(len(set(solutions))) for i in range(10): initial, solution = generator.generate("Medium") solutions = [] values = sudoku.parse_grid(initial) for i in range(10): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) multiple.append(len(set(solutions))) for i in range(10): initial, solution = generator.generate("Hard") solutions = [] values = sudoku.parse_grid(initial) for i in range(10): list_values = [] dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) multiple.append(len(set(solutions))) for i in range(10): initial, solution = generator.generate("Extreme") solutions = [] values = sudoku.parse_grid(initial) for i in range(10): list_values = [] values = sudoku.parse_grid(initial) dict_values = sudoku.searchMultiple(values) for s in sudoku.squares: list_values.append(dict_values[s]) solution = "".join(list_values) solutions.append(solution) multiple.append(len(set(solutions))) template_values = { 'multiple' : multiple } template = jinja_environment.get_template('values.html') self.response.out.write(template.render(template_values))
def solve(grid): return search_DFS(parse_grid(grid)) def search_DFS(values):