Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Code needs to be in PYTHON please. Type out to avoid possible messy hand writing

ID: 3823039 • Letter: C

Question

Code needs to be in PYTHON please. Type out to avoid possible messy hand writing.

Sudoko Puzzle Solve

The rules for Sudoko are summarized in the following web site: http://www.counton.org/sudoku/rules-of-sudoku.php Your assignment is to construct a python code to solve the Generic Sudoko problem. The requirements of the assignment are as follows:

a) Input– To start your problem you will need to read in the Sudoko puzzle. Assume that there is a single puzzle per file. Each input file will have one puzzle per line expressed as 81 characters in row major order. The inputs of that line can either be the digits (1-9) or the ‘.’ for a blank line.

b) Given Input (above) you will need to create an internal representation of the Sudoko Puzzle.

c) Code an algorithm to solve a Sudoko Puzzle – try to do your own algorithm.

d) Output – Have the program output the solution to your Sudoko puzzle.

Try to do all of the above within the constraints of a Python Class. We might test out timings of the code for some simple Sudoko puzzles in class.

Explanation / Answer

import copy possible_moves = set([m for m in '123456789']) class Sudoku(object): def __init__(self, puzzle_str): self.rows = [set() for r in range(9)] self.cols = [set() for c in range(9)] self.boxes = [[set() for bc in range(3)] for br in range(3)] self.grid = [[c for c in line.strip()] for line in puzzle_str.strip().split(' ')] for r, row in enumerate(self.grid): for c, m in enumerate(row): self.move(r, c, m) def move(self, r, c, m): self.rows[r].add(m) self.cols[c].add(m) self.boxes[int(r/3)][int(c/3)].add(m) self.grid[r][c] = m def moves(self, r, c): return possible_moves - self.rows[r] - self.cols[c] - self.boxes[int(r/3)][int(c/3)] def __str__(self): return ' '.join(['.'.join([c for c in line]) for line in self.grid]) worst_best_move = ((10),(),()) def solve_sudoku(puzzle): best_move = worst_best_move found_move = True finished = True while found_move: finished = True found_move = False best_move = worst_best_move for r in range(0,9): for c in range(0,9): if puzzle.grid[r][c]=='0': possible_moves = puzzle.moves(r,c) num_moves = len(possible_moves) if num_moves==0: return None if num_moves==1: found_move=True puzzle.move(r, c, possible_moves.pop()) else: finished = False if num_moves
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote