gurwindersingh commited on
Commit
87b01f5
·
1 Parent(s): b2a1451

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -45
app.py CHANGED
@@ -89,63 +89,63 @@ def read_cells(cell,model):
89
 
90
  #This function finds the next box to solve
91
 
92
- def next_box(quiz):
93
- for row in range(9):
94
- for col in range(9):
95
- if quiz[row][col] == 0:
96
- return (row, col)
97
- return False
98
 
99
  #Function to fill in the possible values by evaluating rows collumns and smaller cells
100
 
101
- def possible (quiz,row, col, n):
102
- #global quiz
103
- for i in range (0,9):
104
- if quiz[row][i] == n and row != i:
105
- return False
106
- for i in range (0,9):
107
- if quiz[i][col] == n and col != i:
 
 
 
108
  return False
109
 
110
- row0 = (row)//3
111
- col0 = (col)//3
112
- for i in range(row0*3, row0*3 + 3):
113
- for j in range(col0*3, col0*3 + 3):
114
- if quiz[i][j]==n and (i,j) != (row, col):
 
 
 
 
115
  return False
 
116
  return True
117
 
118
- #Recursion function to loop over untill a valid answer is found.
119
 
120
- def solve(quiz):
121
- val = next_box(quiz)
122
- if val is False:
 
 
 
123
  return True
124
  else:
125
- row, col = val
126
- for n in range(1,10): #n is the possible solution
127
- if possible(quiz,row, col, n):
128
- quiz[row][col]=n
129
- if solve(quiz):
130
- return True
131
- else:
132
- quiz[row][col]=0
133
- return
 
 
 
 
134
 
135
- def Solved(quiz):
136
- for row in range(9):
137
- if row % 3 == 0 and row != 0:
138
- print("....................")
139
-
140
- for col in range(9):
141
- if col % 3 == 0 and col != 0:
142
- print("|", end=" ")
143
-
144
- if col == 8:
145
- print(quiz[row][col])
146
- else:
147
- print(str(quiz[row][col]) + " ", end="")
148
-
149
  def main():
150
  st.title('Sudoku Solver')
151
 
 
89
 
90
  #This function finds the next box to solve
91
 
92
+ def find_empty(board):
93
+ for i in range(len(board)):
94
+ for j in range(len(board)):
95
+ if board[i][j] == 0:
96
+ return i,j
97
+ return None
98
 
99
  #Function to fill in the possible values by evaluating rows collumns and smaller cells
100
 
101
+ def valid(board, num, pos):
102
+ # check row
103
+ for i in range(len(board)):
104
+ if board[pos[0]][i]==num and pos[1]!=i:
105
+ return False
106
+
107
+
108
+ # check column
109
+ for i in range(len(board)):
110
+ if board[i][pos[1]]==num and pos[0]!=i:
111
  return False
112
 
113
+
114
+
115
+ # check 3X3 cube
116
+ box_x=pos[0]//3
117
+ box_y=pos[1]//3
118
+
119
+ for i in range(box_x*3,box_x+3):
120
+ for j in range(box_y*3,box_y+3):
121
+ if board[i][j]==num and [i,j]!=pos:
122
  return False
123
+
124
  return True
125
 
 
126
 
127
+
128
+ #function to loop over untill a valid answer is found.
129
+
130
+ def solve(board):
131
+ find=find_empty(board)
132
+ if not find:
133
  return True
134
  else:
135
+ row,col=find
136
+
137
+
138
+ for i in range(1,10):
139
+ if valid(board,i,[row,col]):
140
+ board[row][col]=i
141
+
142
+ if solve(board):
143
+ return True
144
+
145
+ board[row][col]=0
146
+ return False
147
+
148
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  def main():
150
  st.title('Sudoku Solver')
151