Skip to content

Instantly share code, notes, and snippets.

@kedarbellare
Created July 13, 2012 14:37
Show Gist options
  • Save kedarbellare/3105238 to your computer and use it in GitHub Desktop.
Save kedarbellare/3105238 to your computer and use it in GitHub Desktop.
Utility for sudoku
## Tests
import time, random
def from_file(filename, sep='\n'):
"Parse a file into a list of strings, separated by sep."
return file(filename).read().strip().split(sep)
def parse_grid(gridstr):
gridstr = gridstr.replace('\n', '')
return [[int(gridstr[i+9*j]) for i in range(9)] for j in range(9)]
def solve_all(solve_sudoku, grids, name='', showif=0.0):
times = []
for grid in grids:
start = time.clock()
assert solve_sudoku(parse_grid(grid)), "Could not solve grid: %s" % grid
t = time.clock() - start
if showif is not None and t > showif:
print "board: %s took %.2f s" % (grid, t)
times.append(t)
N = len(grids)
if N > 1:
print "%d boards took total %.2f s, avg %.2f s, (%.2f Hz), max %.2f s" % (N, sum(times), sum(times)/N, N/sum(times), max(times))
return True
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment