def get_square(j,i): y = j - (j%3) x = i - (i%3) return [puzzle[b][a] for b in range(y,y+3) for a in range(x,x+3) if puzzle[b][a] != 0] def get_square_coords(j,i): y = j - (j%3) x = i - (i%3) return [(b,a) for b in range(y,y+3) for a in range(x,x+3)] def possible(j, i, puz): if puz[j][i] != 0: return [puz[j][i]] else: hor = [p for p in puz[j] if p != 0] vert = [q[i] for q in puz if q[i] != 0] sq = get_square(j,i) return [n for n in possibles[j][i] if n not in hor and n not in vert and n not in sq] def possible_groupings(): #lines for l in range(9): L = {x:[] for x in range(1,10)} p = possibles[l] for i in range(len(p)): for n in p[i]: L[n] = L[n] + [i] T = [f for f in L if len(L[f]) == 1] for t in T: possibles[l] = map(lambda x: [t] if t in x else x, possibles[l]) for i in range(2,5): T = [f for f in L if len(L[f]) == i] if len(T) >= i: T2 = [L[t] for t in T] for t2 in T2: if T2.count(t2) == i: t = [T[a] for a in range(len(T)) if T2[a] == t2] possibles[l] = map(lambda x: [m for m in t] if all(map(lambda y: y in x, t)) else x, possibles[l]) #columns for c in range(9): C = {x:[] for x in range(1,10)} for i in range(9): for n in possibles[i][c]: C[n] = C[n] + [i] T = [f for f in C if len(C[f]) == 1] for t in T: for i in range(9): if t in possibles[i][c]: possibles[i][c] = [t] for j in range(2,5): T = [f for f in C if len(C[f]) == j] if len(T) >= j: T2 = [C[t] for t in T] for t2 in T2: if T2.count(t2) == j: t = [T[a] for a in range(len(T)) if T2[a] == t2] for i in range(9): if all(map(lambda y: y in possibles[i][c], t)): possibles[i][c] = [] for m in t: possibles[i][c].append(m) #squares for s in range(0,9,3): S = {x:[] for x in range(1,10)} for i in range(0,9,3): square_poss = [] gsc = get_square_coords(s,i) for p in gsc: square_poss.append(possibles[p[0]][p[1]]) for j in range(len(square_poss)): for n in square_poss[j]: S[n] = S[n] + [gsc[j]] T = [f for f in S if len(S[f]) == 1] for t in T: for p in gsc: if t in possibles[p[0]][p[1]]: possibles[p[0]][p[1]] = [t] for j in range(2,5): T = [f for f in S if len(S[f]) == j] if len(T) >= j: T2 = [S[t] for t in T] for t2 in T2: if T2.count(t2) == j: t = [T[a] for a in range(len(T)) if T2[a] == t2] for p in t2: possibles[p[0]][p[1]] = [] for m in t: possibles[p[0]][p[1]].append(m) def phantom_lines(): for s in range(0,9,3): for t in range(0,9,3): gsc = get_square_coords(s,t) for i in range(1,10): g = filter(lambda x: i in possibles[x[0]][x[1]],gsc) if all(map(lambda x: x[0] == g[0][0],g)) and len(g) > 0: for p in range(len(possibles[g[0][0]])): if p < t or p > t+2: if i in possibles[g[0][0]][p] and len(possibles[g[0][0]][p]) > 1: possibles[g[0][0]][p].remove(i) if all(map(lambda x: x[1] == g[0][1],g)) and len(g) > 0: for p in range(9): if p < s or p > s+2: if i in possibles[p][g[0][1]] and len(possibles[p][g[0][1]]) > 1: possibles[p][g[0][1]].remove(i) def collapse(): #lines for p in range(len(possibles)): for i in range(2,5): size_i = filter(lambda x: len(x) == i, possibles[p]) for s in size_i: if size_i.count(s) == i: for j in range(len(possibles[p])): if len(possibles[p][j]) > i: for n in s: try: possibles[p][j].remove(n) except ValueError: pass def print_puzzle(): for i in puzzle: print i print '\n' puzzle = [ [2,0,0,8,0,0,0,0,7], [0,4,0,5,0,0,0,0,0], [0,3,0,0,0,0,0,2,8], [8,0,1,9,3,0,0,0,0], [0,0,0,0,5,0,0,0,0], [0,0,0,0,7,8,9,0,2], [9,6,0,0,0,0,0,1,0], [0,0,0,0,0,4,0,7,0], [5,0,0,0,0,1,0,0,3] ] possibles = [[[x for x in range(1,10)] for y in range(9)] for z in range(9)] old_possibles = [] step = 1 while old_possibles != possibles: old_possibles = [[a for a in i] for i in possibles] for j in range(9): for i in range(9): if len(possibles[j][i]) > 1: possibles[j][i] = possible(j, i, puzzle) for j in range(9): for i in range(9): if puzzle[j][i] == 0 and len(possibles[j][i]) == 1: puzzle[j][i] = possibles[j][i][0] possible_groupings() for j in range(9): for i in range(9): if puzzle[j][i] == 0 and len(possibles[j][i]) == 1: puzzle[j][i] = possibles[j][i][0] phantom_lines() for j in range(9): for i in range(9): if puzzle[j][i] == 0 and len(possibles[j][i]) == 1: puzzle[j][i] = possibles[j][i][0] collapse() for j in range(9): for i in range(9): if puzzle[j][i] == 0 and len(possibles[j][i]) == 1: puzzle[j][i] = possibles[j][i][0] step = step + 1 print_puzzle()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question