-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
56 lines (42 loc) · 1.38 KB
/
utils.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
from numpy import (
ndarray,
save
)
def print_sudoku(sudoku: ndarray) -> str:
'''
Displays a sudoku in an easy to read format.
@args:
sudoku (ndarray) : The sudoku to print
@returns:
sudoku_str (string) : The easily readable sudoku
'''
sep = '\n+-------+-------+-------+'
print(sep)
# Iterate through rows
for row_num, row in enumerate(sudoku):
print("|", end=" ")
# Iterate through cols
for col_num, cell in enumerate(row):
# Make sure each value is easier to read
item = str(int(cell)).replace('-1', 'x').replace('0', ' ')
print(item, end=" ")
if (col_num + 1) % 3 == 0:
# reached edge of box
print("|", end=" ")
# Print separator after finished printing all cols
if (row_num + 1) % 3 == 0:
print(sep, end=" ")
# go to new line
print()
def save_sudoku(sudoku: ndarray, filename: str):
'''
Saves a sudoku or array of sudokus to a file.
@args:
sudoku (ndarray): The sudoku (or array of sudokus) to save
'''
try:
save_file_name = f"{filename}_solutions.npy"
np.save(save_file_name, solutions)
print(f"Saved to file: {save_file_name}")
except FileExistsError:
print(f"Could not save: file {save_file_name} already exists.")