/
get_sudoku.py
70 lines (50 loc) · 1.63 KB
/
get_sudoku.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
import json
import random
import numpy as np
"""
files=dict(foo='bar')
"""
def check_difficult(level: int) -> str:
my_dict = {
"60": "muy_facil",
"20": "facil",
"14": "normal",
"10": "dificil",
"-20": "muy dificil"
}
return my_dict[level]
def make_a_save(sudoku_info):
with open("sudoku.json", "w") as file:
file.write(json.dumps(sudoku_info, indent=2))
def get_sudoku() -> tuple:
"""
Make a get request that brings a random sudoku from database
"""
random_sudoku = "".join([str(random.randint(0, 9)) for k in range(13)])
resb = requests.get(
f"https://www.sudoku-online.org/getsudoku.php?{random_sudoku}", files=dict(nivel=14))
sudoku = json.loads(resb.content.decode('utf-8'))
initial_sudoku = sudoku["sudoku"].replace(".", "0")
difficulty = check_difficult(sudoku["nivel"])
sudoku_solved = sudoku["solucion"]
# make_a_save(sudoku)
return initial_sudoku, sudoku_solved, difficulty
def make_a_sudoku():
"""
Transform a sudoku string into a matrix 9x9
and return it
"""
initial_sudoku, solution, difficulty = get_sudoku()
sudokus = [initial_sudoku, solution]
my_sudokus = []
for i, sudoku in enumerate(sudokus):
final_sudoku = []
x = len(sudoku)
n = 9
for index in range(0, x, n):
final_sudoku.append(list(sudoku[index: index + n]))
for i, sub_list in enumerate(final_sudoku):
final_sudoku[i] = [int(k) for k in sub_list]
my_sudokus.append(np.array(final_sudoku))
return my_sudokus[0], my_sudokus[1], difficulty