Skip to content

Commit e3b9a17

Browse files
author
Alexander Tometzki
committed
Improve word search so words can be backwords and overlapping.
1 parent d741235 commit e3b9a17

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Chapter3/word_search.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ def generate_domain(word: str, grid: Grid) -> List[List[Tuple[str, GridLocation]
5454
# diagonal towards bottom right
5555
if row + length <= height:
5656
domain.append([(l, GridLocation(r, col + (r - row))) for l, r in zip(word, rows)])
57-
domain.append([(l, GridLocation(r, col + (r - row))) for l, r in zip(word, rows)])
57+
domain.append([(l, GridLocation(r, col + (r - row))) for l, r in zip(reverse_string(word), rows)])
5858
if row + length <= height:
5959
# top to bottom
6060
domain.append([(l, GridLocation(r, col)) for l, r in zip(word, rows)])
61+
domain.append([(l, GridLocation(r, col)) for l, r in zip(reverse_string(word), rows)])
6162
# diagonal towards bottom left
6263
if col - length >= 0:
64+
domain.append([(l, GridLocation(r, col - (r - row))) for l, r in zip(word, rows)])
6365
domain.append([(l, GridLocation(r, col - (r - row))) for l, r in zip(reverse_string(word), rows)])
6466

6567
return domain
@@ -83,7 +85,7 @@ def satisfied(self, assignment: Dict[str, List[Tuple[str, GridLocation]]]) -> bo
8385
return True
8486

8587
if __name__ == "__main__":
86-
grid: Grid = generate_grid(9, 9)
88+
grid: Grid = generate_grid(4, 9)
8789
words: List[str] = ["MATTHEW", "JOE", "MARY", "SARAH", "SALLY"]
8890
locations: Dict[str, List[List[Tuple[str, GridLocation]]]] = {}
8991
for word in words:

0 commit comments

Comments
 (0)