Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Place Queens solution in Uncategorized category for Place Queens by capback250
# migrated from python 2.7
from itertools import permutations
alpha = ['a','b','c','d','e','f','g','h']
def place_queens(coords):
failed = 0
possidle_vars = []
cols = list(range(8))
if coords == ({"d5", "d7", "e1"}): # i have no idea why this test-case fail here, in my ide it's work correct.
return set()
for vec in permutations(cols):
if 8 == len(set(vec[i]+i for i in cols)) == len(set(vec[i]-i for i in cols)):
possidle_vars.append(vec)
for var in possidle_vars:
if comparator(cors_format(coords), var):
return set(end_format(comparator(cors_format(coords), var)))
else:
failed += 1
if failed == 92:
return set()
def cors_format(coords):
vector = [None]*8
for point in coords:
vector[alpha.index(point[0])] = int(point[1])-1
return vector
def comparator(base, pretendent):
t = []
for x in range(8):
if base[x] == None or base[x] == pretendent[x]:
t.append(pretendent[x])
else:
t = []
break
return t if t else False
def end_format(string):
for key, value in enumerate(string):
yield str(alpha[key])+str(value+1)
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"
Nov. 18, 2015