Skip to content

Commit 23a6b5a

Browse files
committed
Fixed queens solution
1 parent eda67ae commit 23a6b5a

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

src/queens.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@
44

55

66
def queens_without_collisions(queens_positions=[]):
7+
8+
def has_collition(point_1, point_2):
9+
x1, y1 = point_1[0], point_1[1]
10+
x2, y2 = point_2[0], point_2[1]
11+
# check if the points are:
12+
# 1 - horizontally aligned
13+
# 2 - vertically aligned
14+
# 3 - form a 45 degree angle
15+
return x1 == x2 or y1 == y2 or abs(x1 - x2) == abs(y1 - y2)
16+
717
queens_positions = [literal_eval(pos) for pos in queens_positions]
818
for i in range(0, len(queens_positions)):
919
for j in range(i + 1, len(queens_positions)):
10-
i_pos = queens_positions[i]
11-
j_pos = queens_positions[j]
12-
if i_pos[0] == j_pos[0] or i_pos[1] == j_pos[1]:
13-
return str(i_pos).replace(' ', '')
20+
point_1 = queens_positions[i]
21+
point_2 = queens_positions[j]
22+
if has_collition(point_1, point_2):
23+
return str(point_1).replace(' ', '')
1424
return str(True)
1525

1626

@@ -32,4 +42,8 @@ def queens_without_collisions(queens_positions=[]):
3242
Output = "True"
3343
assert queens_without_collisions(Input) == Output
3444

45+
Input = ["(4,1)", "(3,2)"]
46+
Output = "(4,1)"
47+
assert queens_without_collisions(Input) == Output
48+
3549
print("OK!")

0 commit comments

Comments
 (0)