Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import console
- def columns(Numbers, emptyIndex):
- columnList=[[]]
- for i in range(0,9):
- columnList+=[Numbers[int(9*i)+int(emptyIndex%9)]]
- return columnList
- def rows(Numbers, emptyIndex):
- rowsList=[[]]
- rowsList=[Numbers[emptyIndex-emptyIndex%9:(emptyIndex-emptyIndex%9)+9]]
- return rowsList
- def blocks(Numbers, emptyIndex):
- block1=[0,1,2,9,10,11,18,19,20]
- block2=[3,4,5,12,13,14,21,22,23]
- block3=[6,7,8,15,16,17,24,25,26]
- block4=[27,28,29,36,37,38,45,46,47]
- block5=[30,31,32,39,40,41,48,49,50]
- block6=[33,34,35,42,43,44,51,52,53]
- block7=[54,55,56,63,64,65,72,73,74]
- block8=[57,58,59,66,67,68,75,76,77]
- block9=[60,61,62,69,70,71,78,79,80]
- if emptyIndex in block1:
- return [Numbers[i]for i in block1]
- if emptyIndex in block2:
- return [Numbers[i]for i in block2]
- if emptyIndex in block3:
- return [Numbers[i]for i in block3]
- if emptyIndex in block4:
- return [Numbers[i]for i in block4]
- if emptyIndex in block5:
- return [Numbers[i]for i in block5]
- if emptyIndex in block6:
- return [Numbers[i]for i in block6]
- if emptyIndex in block7:
- return [Numbers[i]for i in block7]
- if emptyIndex in block8:
- return [Numbers[i]for i in block8]
- if emptyIndex in block9:
- return [Numbers[i]for i in block9]
- return None
- def sudokuSolve(Numbers):
- notin=[]
- for i in range(0,80):
- if Numbers[i]==[""]:
- for j in range(1,9):
- if j not in columns(Numbers,i) and j not in rows(Numbers,i) and j not in blocks(Numbers,i):
- notin+=[j]
- notin=set(notin)
- notin=list(notin)
- if len(notin)==1:
- Numbers[i]=notin[0]
- print "Number found: ", notin[0]
- notin=[]
- else:
- Numbers[i]=notin
- print("Multiple possibilities: ", notin)
- notin=[]
- while any(isinstance(x, list) for x in Numbers) or [""] in Numbers:
- for i in range(0,80):
- if Numbers[i]==[""] or any(isinstance(x,list)for x in Numbers):
- for j in range(1,9):
- if j not in columns(Numbers,i) and j not in rows(Numbers,i) and j not in blocks(Numbers,i):
- notin+=[j]
- notin=set(notin)
- notin=list(notin)
- if len(notin)==1:
- Numbers[i]=notin[0]
- print "Number found: ", Numbers[i]
- notin=[]
- else:
- Numbers[i]=notin
- print"Multiple possibilities: ", notin
- notin=[]
- return Numbers
- print sudokuSolve([[""],2,[""],5,[""],1,4,[""],3,1,[""],3,[""],6,4,[""],[""],7,[""],[""],4,3,[""],[""],[""],5,1,4,[""],[""],[""],[""],[""],5,1,6,[""],6,[""],[""],1,3,[""],4,[""],7,1,[""],[""],4,5,3,[""],2,[""],9,2,1,[""],6,[""],[""],[""],8,7,6,4,3,[""],1,2,[""],[""],[""],[""],[""],7,[""],6,[""],[""]])
- """[5,3,"","",7,"","","","",6,"","",1,9,5,"","","","",9,8,"","","","",6,"",8,"","","",6,"","","",3,4,"","",8,"",3,"","",1,7,"","","",2,"","","",6,"",6,"","","","",2,8,"","","","",4,1,9,"","",5,"","","","",8,"","",7,9])
- ["","","","","","",6,8,"","","","","",7,3,"","",9,3,"",9,"","","","",4,5,4,9,"","","","","","",8,"",3,"",5,"",9,"",2,"","","","","","","",3,6,9,6,"","","","",3,"",8,7,"","",6,8,"","","","","",2,8,"","","","","","",""])
- list(raw_input("Sudoku Solver: Input all numbers in the unsolved sudoku with spacers, commas, and square brackets (yes really)")))"""
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement