-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
60 lines (52 loc) · 1.94 KB
/
server.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
from flask import Flask, request, jsonify, send_from_directory, render_template
from backtrack import find_all_solutions
from time import time
from sqlite3 import connect
app = Flask(__name__)
def to_list_sudoku(dictionary_sudoku):
list_sudoku = [[] for _ in range(9)]
try:
for i in range(9):
for j in range(9):
square = int(dictionary_sudoku[str(i)+str(j)])
if square not in range(10):
return False
list_sudoku[i].append(square)
except:
return False
return list_sudoku
def to_string_sudoku(list_sudoku):
string = ""
for row in list_sudoku:
for cell in row:
string += str(cell)
return string
def check_timeout(unix_time):
return time() - unix_time > 10 # Check if 10 seconds have passed since the problem was POSTed.
@app.route('/solver', methods=['POST'])
def add_message():
from copy import deepcopy
content = request.json
sudoku = to_list_sudoku(content)
if type(sudoku) == list:
problem = deepcopy(sudoku)
solutions, timed_out, time_spent = find_all_solutions(sudoku, check_timeout)
if not timed_out and len(solutions) == 1:
database = connect("times.db")
key = to_string_sudoku(problem)
database.execute("INSERT or IGNORE INTO times VALUES(?, ?)", (key, time_spent))
database.commit()
return jsonify((solutions, timed_out))
return jsonify(False)
@app.route('/problems')
def get_problems():
database = connect("times.db")
return jsonify(database.execute("SELECT * FROM times ORDER BY time DESC").fetchall())
@app.route("/")
def home():
return send_from_directory('static', "index.html")
@app.route('/<path:path>')
def send_static_page(path):
return send_from_directory('static', path)
if __name__ == '__main__':
app.run(host= '0.0.0.0',debug=True)