|
| 1 | +""" |
| 2 | +
|
| 3 | +Knight Moves |
| 4 | +Given the position of a knight on a chessboard, return the number of valid squares the knight can move to. |
| 5 | +
|
| 6 | +A standard chessboard is 8x8, with columns labeled A through H (left to right) and rows labeled 1 through 8 (bottom to top). It looks like this: |
| 7 | +
|
| 8 | +A8 B8 C8 D8 E8 F8 G8 H8 |
| 9 | +A7 B7 C7 D7 E7 F7 G7 H7 |
| 10 | +A6 B6 C6 D6 E6 F6 G6 H6 |
| 11 | +A5 B5 C5 D5 E5 F5 G5 H5 |
| 12 | +A4 B4 C4 D4 E4 F4 G4 H4 |
| 13 | +A3 B3 C3 D3 E3 F3 G3 H3 |
| 14 | +A2 B2 C2 D2 E2 F2 G2 H2 |
| 15 | +A1 B1 C1 D1 E1 F1 G1 H1 |
| 16 | +A knight moves in an "L" shape: two squares in one direction (horizontal or vertical), and one square in the perpendicular direction. |
| 17 | +
|
| 18 | +This means a knight can move to up to eight possible positions, but fewer when near the edges of the board. For example, if a knight was at A1, it could only move to B3 or C2. |
| 19 | +""" |
| 20 | + |
| 21 | +import unittest |
| 22 | + |
| 23 | +class KnightMovesTest(unittest.TestCase): |
| 24 | + |
| 25 | + def test1(self): |
| 26 | + self.assertEqual(knight_moves("A1"), 2) |
| 27 | + |
| 28 | + def test2(self): |
| 29 | + self.assertEqual(knight_moves("D4"), 8) |
| 30 | + |
| 31 | + def test3(self): |
| 32 | + self.assertEqual(knight_moves("G6"), 6) |
| 33 | + |
| 34 | + def test4(self): |
| 35 | + self.assertEqual(knight_moves("B8"), 3) |
| 36 | + |
| 37 | + def test5(self): |
| 38 | + self.assertEqual(knight_moves("H3"), 4) |
| 39 | + |
| 40 | + |
| 41 | +def knight_moves(position): |
| 42 | + |
| 43 | + col = ord(position[0].upper()) - ord('A') + 1 |
| 44 | + row = int(position[1]) |
| 45 | + |
| 46 | + |
| 47 | + moves = [ |
| 48 | + (2, 1) ,(2, -1), (-2, 1), (-2, -1), |
| 49 | + (1, -2), (1, 2), (-1, 2), (-1, -2) |
| 50 | + ] |
| 51 | + valid_moves = 0 |
| 52 | + |
| 53 | + for dx, dy in moves: |
| 54 | + new_col = col + dx |
| 55 | + new_row = row + dy |
| 56 | + if 1 <= new_col <= 8 and 1 <= new_row <= 8: |
| 57 | + valid_moves += 1 |
| 58 | + |
| 59 | + return valid_moves |
| 60 | + |
| 61 | + |
| 62 | +""" |
| 63 | +
|
| 64 | +=> Convert chess notation -> numeric coordinates. |
| 65 | +=> apply knight's 8 possible moves. |
| 66 | +=> count only those withn the 8x8 board. |
| 67 | +
|
| 68 | +
|
| 69 | +we can also extend it for show the valid squares rather than just showing only the count |
| 70 | +
|
| 71 | +
|
| 72 | +""" |
| 73 | + |
| 74 | + |
| 75 | +def knight_moves_positions(position): |
| 76 | + |
| 77 | + col = ord(position[0].upper()) - ord('A') + 1 |
| 78 | + row = int(position[1]) |
| 79 | + |
| 80 | + moves = [ |
| 81 | + (2, 1), (2, -1), (-2, 1), (-2, -1), |
| 82 | + (1, -2), (1, 2), (-1, 2), (-1, -2) |
| 83 | + ] |
| 84 | + |
| 85 | + valid_positions = [] |
| 86 | + for dx, dy in moves: |
| 87 | + new_col = col + dx |
| 88 | + new_row = row + dy |
| 89 | + |
| 90 | + if 1 <= new_col <= 8 and 1 <= new_row <= 8: |
| 91 | + square = chr(ord('A') + new_col - 1) + str(new_row) |
| 92 | + valid_positions.append(square) |
| 93 | + |
| 94 | + |
| 95 | + return valid_positions |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + print(knight_moves("D6")) |
| 99 | + |
| 100 | + print(knight_moves_positions("D6")) |
| 101 | + unittest.main() |
0 commit comments