rubend18 commited on
Commit
745f025
·
1 Parent(s): 36bd9bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def print_board(tablero):
2
+ for i in range(len(tablero)):
3
+ if i % 3 == 0 and i != 0:
4
+ print("------+-------+------")
5
+
6
+ for j in range(len(tablero[0])):
7
+ if j % 3 == 0 and j != 0:
8
+ print("| ", end="")
9
+
10
+ if j == 8:
11
+ print(tablero[i][j])
12
+ else:
13
+ print(str(tablero[i][j]) + " ", end="")
14
+
15
+ def resolver(tablero):
16
+ buscar = buscar_vacio(tablero)
17
+ if not buscar:
18
+ return True
19
+ else:
20
+ fila, columna = buscar
21
+
22
+ for i in range(1,10):
23
+ if es_valido(tablero, i, (fila, columna)):
24
+ tablero[fila][columna] = i
25
+
26
+ if resolver(tablero):
27
+ return True
28
+
29
+ tablero[fila][columna] = 0
30
+
31
+ return False
32
+
33
+ def es_valido(tablero, num, pos):
34
+ # Comprobar fila
35
+ for i in range(len(tablero[0])):
36
+ if tablero[pos[0]][i] == num and pos[1] != i:
37
+ return False
38
+
39
+ # Comprobar columna
40
+ for i in range(len(tablero)):
41
+ if tablero[i][pos[1]] == num and pos[0] != i:
42
+ return False
43
+
44
+ # Comprobar caja
45
+ caja_x = pos[1] // 3
46
+ caja_y = pos[0] // 3
47
+
48
+ for i in range(caja_y*3, caja_y*3 + 3):
49
+ for j in range(caja_x * 3, caja_x*3 + 3):
50
+ if tablero[i][j] == num and (i,j) != pos:
51
+ return False
52
+
53
+ return True
54
+
55
+ def buscar_vacio(tablero):
56
+ for i in range(len(tablero)):
57
+ for j in range(len(tablero[0])):
58
+ if tablero[i][j] == 0:
59
+ return (i, j) # fila, columna
60
+
61
+ return None
62
+
63
+ # ********************************************************************************
64
+ # INTERFAZ
65
+ # ********************************************************************************
66
+
67
+ import gradio as gr
68
+
69
+ def function(cadena):
70
+ tablero = []
71
+ for i in range(0, 81, 9):
72
+ row = [int(num) for num in cadena[i:i+9]]
73
+ tablero.append(row)
74
+ resolver(tablero)
75
+ return print_board(tablero)
76
+
77
+ demo = gr.Interface(
78
+ fn=function,
79
+ inputs=gr.Textbox(lines=2, label="Sudoku", placeholder="Ingrese la cadena del sudoku"),
80
+ outputs=gr.Textbox(lines=10, label="Resultado", placeholder="Resultado..."),
81
+ examples=[["780400120600075009000601078007040260001050930904060005070300012120007400049206007"]]
82
+ )
83
+
84
+ demo.launch(inline=False)