I'm writing a basic sudoku solver. I have a simple method that looks for squareas with only 1 possible number and fills them in before trying to solve the puzzle.
the method look like this:
def fillinones(sq):
table = {1:1,2:2,4:3,8:4,16:5,32:6,64:7,128:8,256:9}
for i in range(0,80):
p=checkpossible(sq,i)
print "p: ",p
if p & ~(p & (p - 1)):
sq[i] = table[p]
checkpossible returns a number that has uses bits to represent the possible numbers that can go in a blank square. so 1:1,2:2,4:3,etc up to nine, which is what the table does.
so if only one number can fit into the square, p must be an exponent of 2. p & ~(p & (p - 1))
fails to differentiate non 2-exponent numbers. logically it seems to make since since base 2 32: 100000 and 31: 011111 so 100000 & 011111 : 0. and the table is supposed to match the exponent with the power. I'm guessing that how it works I couldn't find an example that didn't use strings for keys.