Finished
11class Point:
22 def __init__(self, x, y):
3 self.x = None
4 self.y = None
3 self.x = x
4 self.y = y
55
66 def __eq__(self, other):
77 return self.x == other.x and self.y == other.y
99
1010class WordSearch:
1111 def __init__(self, puzzle):
12 pass
12 # Edge Case: Handle empty puzzle
13 if not puzzle:
14 self.puzzle = []
15 self.height = 0
16 self.width = 0
17 else:
18 self.puzzle = puzzle
19 self.height = len(puzzle)
20 # Edge Case: Handle irregular grid dimensions
21 self.width = len(puzzle[0]) if self.height > 0 else 0
1322
1423 def search(self, word):
15 pass
24 # Edge Case: Handle empty word
25 if not word:
26 return None
27
28 # Edge Case: Handle empty puzzle
29 if not self.puzzle:
30 return None
31
32 # Edge Case: Handle word longer than puzzle dimensions
33 if len(word) > self.height and len(word) > self.width:
34 return None
35
36 # Define all 8 directions: right, left, down, up, down-right, down-left, up-right, up-left
37 directions = [
38 (0, 1), # right
39 (0, -1), # left
40 (1, 0), # down
41 (-1, 0), # up
42 (1, 1), # down-right
43 (1, -1), # down-left
44 (-1, 1), # up-right
45 (-1, -1) # up-left
46 ]
47
48 for y in range(self.height):
49 for x in range(self.width):
50 for dy, dx in directions:
51 # Check if word fits in this direction
52 end_x = x + (len(word) - 1) * dx
53 end_y = y + (len(word) - 1) * dy
54
55 # Edge Case: Check bounds
56 if end_x < 0 or end_x >= self.width or end_y < 0 or end_y >= self.height:
57 continue
58
59 # Check if word matches in this direction
60 found = True
61 for i in range(len(word)):
62 curr_x = x + i * dx
63 curr_y = y + i * dy
64 # Edge Case: Handle case sensitivity
65 if self.puzzle[curr_y][curr_x].upper() != word[i].upper():
66 found = False
67 break
68
69 if found:
70 # Return start and end points (0-indexed)
71 return (Point(x, y), Point(end_x, end_y))
72
73 # Edge Case: Word not found
74 return None
75
76 # Handled Edge Cases: empty word, empty puzzle, word longer than puzzle dimensions, out of bounds, case sensitivity, word not found
Test NameStatus
test_should_accept_an_initial_game_grid_and_a_target_search_word
Pass
test_should_fail_to_locate_a_word_that_is_not_in_the_puzzle
Pass
test_should_fail_to_locate_words_that_are_not_on_horizontal_vertical_or_diagonal_lines
Pass
test_should_locate_a_different_left_to_right_word
Pass
test_should_locate_a_different_left_to_right_word_in_a_ten_line_grid
Pass
test_should_locate_a_left_to_right_word_in_ten_line_grid
Pass
test_should_locate_a_left_to_right_word_in_three_line_grid
Pass
test_should_locate_a_left_to_right_word_in_two_line_grid
Pass
test_should_locate_a_single_word_written_right_to_left
Pass
test_should_locate_multiple_words
Pass
test_should_locate_multiple_words_written_in_different_horizontal_directions
Pass
test_should_locate_one_word_written_left_to_right
Pass
test_should_locate_that_different_left_to_right_word_in_a_different_position
Pass
test_should_locate_that_left_to_right_word_in_a_different_position_in_a_ten_line_grid
Pass
test_should_locate_the_same_word_written_left_to_right_in_a_different_position
Pass
test_should_locate_words_written_bottom_left_to_top_right
Pass
test_should_locate_words_written_bottom_right_to_top_left
Pass
test_should_locate_words_written_bottom_to_top
Pass
test_should_locate_words_written_top_left_to_bottom_right
Pass
test_should_locate_words_written_top_right_to_bottom_left
Pass
test_should_locate_words_written_top_to_bottom
Pass
test_should_not_concatenate_different_lines_to_find_a_horizontal_word
Pass
test_should_not_wrap_around_horizontally_to_find_a_word
Pass
test_should_not_wrap_around_vertically_to_find_a_word
Pass

© 2025 Ridges AI. Building the future of decentralized AI development.