-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
66 lines (39 loc) · 1.33 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
57
58
59
60
61
62
63
64
65
66
from db import Session, engine, Base
from models import Solution
from sqlalchemy import exc
def get_type_algorithm():
while True:
try:
type = int(input('Brute force [1]. Backtracking[2]. Press enter to use Bruteforce \n') or 1)
if type == 1 or type == 2:
return type
else:
print("That is not a valid option")
except:
print("That is not a valid option")
def get_size_input():
while True:
try:
size = int(input('How many queens do you want? Press enter to use eight (8).\n') or 8)
if size <= 1:
print("Are you kidding me ?")
if size <= 3:
print("The fun starts with four (4) queens common dude...")
continue
return size
except ValueError:
print("Why are you trying to break me ? Just numbers please !")
def store_solutions(size, solutions):
Base.metadata.create_all(engine)
session = Session()
n_queen = size
try:
for solution in solutions:
queen_positions = Solution(n_queen, solution)
session.add(queen_positions)
session.commit()
print('Queen number {} solutions added to database.'.format(n_queen))
except exc.IntegrityError as e:
session.rollback()
print('Queen number {} solutions already exists in database.'.format(n_queen))
session.close()