forked from kpbochenek/empireofcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
place_queens.py
49 lines (41 loc) · 1.74 KB
/
place_queens.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
def place_queens(placed):
return set()
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
from itertools import combinations
COLS = "abcdefgh"
ROWS = "12345678"
THREATS = {c + r: set(
[c + ROWS[k] for k in range(8)] +
[COLS[k] + r for k in range(8)] +
[COLS[k] + ROWS[i - j + k] for k in range(8) if 0 <= i - j + k < 8] +
[COLS[k] + ROWS[- k + i + j] for k in range(8) if 0 <= - k + i + j < 8])
for i, r in enumerate(ROWS) for j, c in enumerate(COLS)}
def check_coordinate(coor):
c, r = coor
return c in COLS and r in ROWS
def checker(func, placed, is_possible):
user_set = func(placed.copy())
if not all(isinstance(c, str) and len(c) == 2 and check_coordinate(c) for c in user_set):
print("Wrong Coordinates")
return False
threats = []
for f, s in combinations(user_set.union(placed), 2):
if s in THREATS[f]:
threats.append([f, s])
if not is_possible:
if user_set:
print("Hm, how did you place them?")
return False
else:
return True
if not all(p in user_set for p in placed):
print("You forgot about placed queens.")
return False
if is_possible and threats:
print("I see some problems in this placement.")
return False
return True
assert checker(place_queens, {"b2", "c4", "d6", "e8"}, True), "1st Example"
assert checker(place_queens, {"b2", "c4", "d6", "e8", "a7", "g5"}, False), "2nd Example"
print("Code's finished? Earn rewards by clicking 'Check' to review your tests!")