fork(7) download
  1. #sudoku 9*9
  2. # the sudoku list will contain sub-lists which contain rows in the form [[row 1], [row 2],..,[row 9]]
  3. sud=[]
  4. # creating the array
  5. for i in range(9):
  6. sud.append([])
  7. for k in range(9):
  8. sud[i].append('#')
  9. h=0
  10. while True:
  11. a=raw_input('Give me the known in exactly this form say for row 1: 1##2#7### , type done when you finish. \n >>>')
  12. if a.lower()=='done':
  13. break
  14. try:
  15. for i,j in enumerate(a):
  16. if j !='#':
  17. sud[h][i]=int(a[i])
  18.  
  19. except:
  20. print' wrong format , type like 1,2=5'
  21. h+=1
  22. #printing the sudoku table
  23. row=range(9)
  24. colmn=range(9)
  25. def prt_sud():
  26. for i in range(9):
  27. print ''
  28. if i in [0,3,6]:
  29. print'|-------|-------|-------|'
  30. for k in range(9):
  31. if k in [0,3,6]:
  32. print '|',
  33. print sud[i][k],
  34. if k==8:
  35. print '|',
  36. if i ==8:
  37. print '\n|-------|-------|-------|',
  38. #the given sudoku
  39. print '_____(The Given sudoku)_____'
  40. prt_sud()
  41. print '\n','_____________________________'
  42. def row_pos():
  43. #calculating numbers exist in each row
  44. for i in range (9):
  45. row[i]=[]
  46. for j in range(9):
  47. if sud[i][j]=='#' or type(sud[i][j])== list:
  48.  
  49. pass
  50. else:
  51. row[i].append(sud[i][j])
  52.  
  53. return row
  54. def col_pos():
  55. # calculating the numbers exist in each column
  56. for i in range(9):
  57. colmn[i]=[]
  58. for j in range(9):
  59. if sud[j][i]=='#'or type(sud[j][i])== list:
  60.  
  61. pass
  62. else:
  63. colmn[i].append(sud[j][i])
  64.  
  65. return colmn
  66. def rc_poss():
  67. # calculating possisbilities for each cell by looking in the corresponding row and colomn
  68. for i in range(9):
  69. for j in range(9):
  70. a=0
  71. if type(sud[i][j])==list:
  72. sud[i][j]='#'
  73. if sud[i][j]!='#' :
  74. continue
  75. else:
  76. sud[i][j]=[]
  77. a=col_pos()[j]+row_pos()[i]
  78. for l in range (1,10):
  79. if l in a:
  80. continue
  81. else:
  82. sud[i][j].append(l)
  83.  
  84. return chk_list()
  85. def chk_list():
  86. count=0
  87. cntint=0
  88. #chek for elements in sud that are lists of 1 possisbility convert them into int.
  89. for i in range(9):
  90. for j in range(9):
  91. if type(sud[i][j])==list:
  92. if len(sud[i][j])==1:
  93. sud[i][j]=sud[i][j][0]
  94. count+=1
  95. if count>0:
  96. return True
  97. else: return False
  98. def end():
  99. cntint=0
  100. for i in sud:
  101. for j in i:
  102. if type(j)== int:
  103. cntint+=1
  104. if cntint == 81:
  105. return -1
  106. def sud_square():
  107. sud_s=range(9)
  108. for i in range(9):
  109. sud_s[i]=[]
  110. for j in range(3):
  111. for k in range(3):
  112. sud_s[i].append(sud[j+(i/3)*3][k+(i%3)*3])
  113. return sud_s
  114. def sudsubsqr_search():
  115. for i in range(9):
  116. for j in range(9):
  117. if type(sud[i][j])==int:
  118. continue
  119. for l in sud[i][j]:
  120. for k in range(1,9):
  121. if (i%3)*3+(j%3)+k >8:
  122. if l==sud_square()[(i/3)*3+j/3][(i%3)*3+(j%3)+k-8]:
  123. sud[i][j].remove(l)
  124. return chk_list()
  125.  
  126. else:
  127. if l==sud_square()[(i/3)*3+j/3][(i%3)*3+(j%3)+k]:
  128. sud[i][j].remove(l)
  129. return chk_list()
  130. def solve_sud():
  131. while True:
  132. while rc_poss():
  133. if end()==-1:
  134. print 'Vola!'
  135. return prt_sud()
  136. else:
  137. while not sudsubsqr_search():
  138. if end()==-1:
  139. print 'Vola!'
  140. return prt_sud()
  141. else:
  142. if end()==-1:
  143. print 'Vola!'
  144. return prt_sud()
  145. solve_sud()
  146.  
Success #stdin #stdout 0.18s 7696KB
stdin
9#2#7#1#6
#375#1#4#
##62####3
##48##3##
3#5###4#1
##1##37##
2####56##
#1#6#492#
4#3#9#5#8
done

stdout
Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>Give me the known in exactly this form  say for row 1: 1##2#7### , type done when you finish. 
 >>>_____(The Given sudoku)_____

|-------|-------|-------|
| 9 # 2 | # 7 # | 1 # 6 | 
| # 3 7 | 5 # 1 | # 4 # | 
| # # 6 | 2 # # | # # 3 | 
|-------|-------|-------|
| # # 4 | 8 # # | 3 # # | 
| 3 # 5 | # # # | 4 # 1 | 
| # # 1 | # # 3 | 7 # # | 
|-------|-------|-------|
| 2 # # | # # 5 | 6 # # | 
| # 1 # | 6 # 4 | 9 2 # | 
| 4 # 3 | # 9 # | 5 # 8 | 
|-------|-------|-------| 
_____________________________
Vola!

|-------|-------|-------|
| 9 4 2 | 3 7 8 | 1 5 6 | 
| 8 3 7 | 5 6 1 | 2 4 9 | 
| 1 5 6 | 2 4 9 | 8 7 3 | 
|-------|-------|-------|
| 7 2 4 | 8 1 6 | 3 9 5 | 
| 3 8 5 | 9 2 7 | 4 6 1 | 
| 6 9 1 | 4 5 3 | 7 8 2 | 
|-------|-------|-------|
| 2 7 9 | 1 8 5 | 6 3 4 | 
| 5 1 8 | 6 3 4 | 9 2 7 | 
| 4 6 3 | 7 9 2 | 5 1 8 | 
|-------|-------|-------|