Skip to content

Commit afc10ed

Browse files
committed
feat: knight challenge"
1 parent 89e9940 commit afc10ed

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

knight/knight.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
KNIGHT = 1
2+
K_MOVES_X = [(2, 1), (-2, 1), (2, -1), (-2, -1)]
3+
K_MOVES_Y = [(1, 2), (2, -1), (-2, 1), (-2, -1)]
4+
ALL_KNIGHTS_MOVES = K_MOVES_X + K_MOVES_Y
5+
6+
7+
def cannot_capture(board: list[list[int]]) -> bool:
8+
knight_positions = []
9+
for iy, y in enumerate(board):
10+
for ix, x in enumerate(board[iy]):
11+
if x == KNIGHT:
12+
knight_positions.append((ix, iy))
13+
14+
for knight in knight_positions:
15+
for move in ALL_KNIGHTS_MOVES:
16+
x, y = move
17+
kx, ky = knight
18+
try:
19+
if board[x + kx][y + ky] == KNIGHT:
20+
print(
21+
f"Knight on ({x}, {y}) can capture knight on ({kx}, {ky}). Skill issue ngl"
22+
)
23+
return False
24+
except IndexError:
25+
pass # cant move there probs cause its out of board
26+
return True
27+
28+
29+
tests = [
30+
[
31+
[0, 0, 0, 1, 0, 0, 0, 0],
32+
[0, 0, 0, 0, 0, 0, 0, 0],
33+
[0, 1, 0, 0, 0, 1, 0, 0],
34+
[0, 0, 0, 0, 1, 0, 1, 0],
35+
[0, 1, 0, 0, 0, 1, 0, 0],
36+
[0, 0, 0, 0, 0, 0, 0, 0],
37+
[0, 1, 0, 0, 0, 0, 0, 1],
38+
[0, 0, 0, 0, 1, 0, 0, 0],
39+
],
40+
[
41+
[1, 0, 1, 0, 1, 0, 1, 0],
42+
[0, 1, 0, 1, 0, 1, 0, 1],
43+
[0, 0, 0, 0, 1, 0, 1, 0],
44+
[0, 0, 1, 0, 0, 1, 0, 1],
45+
[1, 0, 0, 0, 1, 0, 1, 0],
46+
[0, 0, 0, 0, 0, 1, 0, 1],
47+
[1, 0, 0, 0, 1, 0, 1, 0],
48+
[0, 0, 0, 1, 0, 1, 0, 1],
49+
],
50+
]
51+
for test in tests:
52+
print(cannot_capture(test))

knight/task.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
Write a function that returns true if the knights are placed on a chessboard such that no knight can capture another knight. Here, 0s represent empty squares and 1s represent knights.
2+
Examples
3+
cannotCapture([
4+
[0, 0, 0, 1, 0, 0, 0, 0],
5+
[0, 0, 0, 0, 0, 0, 0, 0],
6+
[0, 1, 0, 0, 0, 1, 0, 0],
7+
[0, 0, 0, 0, 1, 0, 1, 0],
8+
[0, 1, 0, 0, 0, 1, 0, 0],
9+
[0, 0, 0, 0, 0, 0, 0, 0],
10+
[0, 1, 0, 0, 0, 0, 0, 1],
11+
[0, 0, 0, 0, 1, 0, 0, 0]
12+
]) ➞ true
13+
14+
cannotCapture([
15+
[1, 0, 1, 0, 1, 0, 1, 0],
16+
[0, 1, 0, 1, 0, 1, 0, 1],
17+
[0, 0, 0, 0, 1, 0, 1, 0],
18+
[0, 0, 1, 0, 0, 1, 0, 1],
19+
[1, 0, 0, 0, 1, 0, 1, 0],
20+
[0, 0, 0, 0, 0, 1, 0, 1],
21+
[1, 0, 0, 0, 1, 0, 1, 0],
22+
[0, 0, 0, 1, 0, 1, 0, 1]
23+
]) ➞ false
24+
25+
Notes
26+
27+
Knights can be present in any of the 64 squares.
28+
No other pieces except knights exist.

0 commit comments

Comments
 (0)