Skip to content
Browse files

Write solutions to file

  • Loading branch information...
1 parent 742c85d commit 0032dedff1fef640fab3a08232006cb796cbe50f Andreas Pauley committed with Feb 25, 2011
Showing with 12 additions and 2 deletions.
  1. +12 −2 sudoku.py
View
14 sudoku.py
@@ -3,7 +3,7 @@
# A sudoku solver by Peter Norvig
# http://norvig.com/sudoku.html
-import time
+import os, time
def cross(A, B):
return [a+b for a in A for b in B]
@@ -146,8 +146,18 @@ 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 to_file(outfile, solutions):
+ grids = [to_string(s)+'\n' for s in solutions]
+ fp = file(outfile, 'w')
+ try:
+ fp.writelines(grids)
+ finally:
+ fp.close()
+
def solve_file(filename, sep='\n'):
- return solve_all(from_file(filename, sep), filename, None)
+ solutions = solve_all(from_file(filename, sep), filename, None)
+ outfile = os.path.splitext(filename)[0] + '.out'
+ to_file(outfile, solutions)
if __name__ == '__main__':
solve_file("easy50.txt", '========')

0 comments on commit 0032ded

Please sign in to comment.
Something went wrong with that request. Please try again.