Source

Simple Sudoku solver in python

Hi there pythonistas! We all know that Sudoku is a great game. Some of us even bet on this game but did you know that you can use python to make a Sudoku solver? In this post I am going to share with you a Sudoku solver written in python.

From now on you will win all Sudoku challenges. However let me tell you that I am not the original writer of this script. I stumbled over this script on stack-overflow. So without wasting 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 i%9/3 == j%9/3)

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

  excluded_numbers = set()
  for j in range(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 'Usage: python sudoku.py puzzle'
    print '  where puzzle is an 81 character string 
             representing the puzzle read left-to-right,
             top-to-bottom, and 0 is a blank'

Hey there wait! let me share with you a shorter obfuscated version of the same Sudoku solving script. However this short version of Sudoku solver is a lot slower but I think that I should share it with you just to show you that even in python obfuscated code can be written. So here is the shorter obfuscated version:

def r(a):i=a.find('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 range(81)]or r(a[:i]+m+a[i+1:])for m in'%d'%5**18]
from sys import*;r(argv[1])

If you liked this post about Sudoku solver then don’t forget to share this on twitter and facebook. If you want to get regular updates then don’t forget to follow our blog.

Newsletter

×

If you liked what you read then I am sure you will enjoy a newsletter of the content I create. I send it out every other month. It contains new stuff that I make, links I find interesting on the web, and occasional discount coupons for my book. Join the 5000+ other people who receive my newsletter:

I send out the newsletter once every other month. No spam, I promise + you can unsubscribe at anytime

✍️ Comments

Daniel

Did you ever tried this? Cant figure out, how this should solve a sudoku

AB
In reply to Daniel

Daniel, I tried this and the code works. You can copy-paste this into a python editor and save it, then run it. When you call it, you need to include an 81 character string on the command line. This string represents the sudoku puzzle, with 0’s in place of the empty cells. So you’d call it like this: python sudoku.py 81-char-string

Alternatively, I modified the script and included it on my GitHub account. My version takes a filename as input on the command line. The file contains the sudoku puzzle. Each line in the file represents one row in the puzzle, with 0’s in the empty cells. You can find the code here - https://github.com/ariesunique/sudoku. The txt files are the puzzles. The file called simple_sudoku_solver.py is the code found on this page.

AB

Thanks for sharing this. This post inspired me to make my own attempt as a sudoku solver. I wrote my code before looking at this so I was surprised to see how short this code is. The solution is very elegant however it relies a lot on recursion so it’s a bit time-intensive.

EJ

I entered the code verbatum and run with puzzle string to get an error on line 16 “name i is not defined” How do I correct this? Note: I am very new to python (and scripting in general).

EJ

Also, thank you for sharing the code.

Twisted Code

Where exactly did you find this and can you provide an explanation of how it works? I’m not interested in using the code; I just want to understand and learn from it. When I want a program to solve a common problem or puzzle, I usually just write one myself, assuming it’s not too complicated. If I really need the program and can’t figure out how to write it myself, and then, and only then, will I rely on someone else’s code

Say something

Send me an email when someone comments on this post.

Thank you!

Your comment has been submitted and will be published once it has been approved. 😊

OK