Sunday, April 28, 2024
HomePythonEasy Sudoku solver in python

Easy Sudoku solver in python


Hello there pythonistas! Everyone knows that Sudoku is a superb sport. A few of us even wager on this sport however do you know that you should use python to make a Sudoku solver? On this submit I’m going to share with you a Sudoku solver written in python.

Any longer you’ll win all Sudoku challenges. Nonetheless let me inform you that I’m not the unique author of this script. I stumbled over this script on stack-overflow. So with out losing any time let me share the script with you:

import sys

def same_row(i,j): return (i/9 == j/9)
def same_col(i,j): return (i-j) % 9 == 0
def same_block(i,j): return (i/27 == j/27 and that ipercent9/3 == jpercent9/3)

def r(a):
  i = a.discover('0')
  if i == -1:
    sys.exit(a)

  excluded_numbers = set()
  for j in vary(81):
    if same_row(i,j) or same_col(i,j) or same_block(i,j):
      excluded_numbers.add(a[j])

  for m in '123456789':
    if m not in excluded_numbers:
      r(a[:i]+m+a[i+1:])

if __name__ == '__main__':
  if len(sys.argv) == 2 and len(sys.argv[1]) == 81:
    r(sys.argv[1])
  else:
    print 'Utilization: python sudoku.py puzzle'
    print '  the place puzzle is an 81 character string 
             representing the puzzle learn left-to-right,
             top-to-bottom, and 0 is a clean'

Hey there wait! let me share with you a shorter obfuscated model of the identical Sudoku fixing script. Nonetheless this brief model of Sudoku solver is rather a lot slower however I feel that I ought to share it with you simply to indicate you that even in python obfuscated code may be written. So right here is the shorter obfuscated model:

def r(a):i=a.discover('0');~i or exit(a);[m
in[(i-j)%9*(i/9^j/9)*(i/27^j/27|i%9/3^j%9/3)or a[j]for
j in vary(81)]or r(a[:i]+m+a[i+1:])for m in'%d'%5**18]
from sys import*;r(argv[1])

When you favored this submit about Sudoku solver then don’t overlook to share this on twitter and fb. If you wish to get common updates then don’t overlook to comply with our weblog.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments