#!/usr/bin/python # TODO: Make solve function faster!!! Have it check for singles, doubles, # triples, and quads both naked and hidden from random import random def rand(lst): "returns a random element in list or integer" if type(lst)==type([]): return lst[int(random()*len(lst))] elif type(lst)==type(0): return int(random()*lst) else: raise Exception,"don't know what do do with type %s!!!"%type(lst) def reorder(lst): "reorders a list to a random order" ret=[] for item in lst: ret.insert(rand(len(ret)),item) return ret def row(row,puzzle): return puzzle[row*9:row*9+9] def col(col,puzzle): ret=[] for i in range(9): ret.append(row(i,puzzle)[col]) return ret def box(box,puzzle): x=box%3 if box<3: y=0 elif box<6: y=1 else: y=2 ret=[] for i in range(3): ret.extend(row(y*3+i,puzzle)[x*3:x*3+3]) return ret def remaining(wcb): ret=[] for i in range(1,10): if not i in wcb: ret.append(i) return reorder(ret) # does not significantly slow program # and allows for generation of random puzzles def coordToBox(x,y): box=0 if x<3: pass elif x<6: box+=1 else: box+=2 if y<3: pass elif y<6: box+=3 else: box+=6 return box def coordToLinear(x,y): return y*9+x def linearToCoord(index): y=8 for i in range(9): if index