#!/usr/bin/python2.4 import cgi import cgitb; cgitb.enable() import os, sys from sudo import * import time print "content-type: text/plain\n" startTime = time.time() def solve_string(string, sep='\n', action=lambda x: x): "Parse a string into a sequence of 81-char descriptions and solve them." results = [action(search(parse_grid(grid))) for grid in string.strip().split(sep)] # print "## Got %d out of %d" % ( # sum((r is not False) for r in results), len(results)) return results def printboard_and_line(values): "Used for debugging." width = 1+max(len(values[s]) for s in squares) line = '\n' + '+'.join(['-'*(width*3)]*3) for r in rows: print ''.join(values[r+c].center(width)+(c in '36' and '|' or '') for c in cols) + (r in 'CF' and line or '') print out = ''; for r in rows: out += ''.join(values[r+c] for c in cols) print out return form = cgi.FieldStorage() if not form.has_key("sudoku"): print "Error: Please fill in the sudoku field." else: sudokuString = form["sudoku"].value solve_string(sudokuString,'|', action=printboard_and_line) t = time.time() - startTime #print "%.2f msec" % (t*1000) print "%.4f msec" % (t*1000) #print t