Skip to content

Commit 0dd2b8d

Browse files
committed
docs: add success and faillure doctests for get_word_path()
1 parent d73a036 commit 0dd2b8d

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

backtracking/word_search_path.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ def get_word_path(
6060
) -> list[tuple[int, int]] | None:
6161
"""
6262
Return the coordinate path if it's possible to find the word in the grid.
63+
64+
>>> board = [["C", "A", "T"], ["X", "Y", "S"], ["A", "B", "C"]]
65+
>>> # 1. Find "CATS" starting at (0,0). Initial key is 0.
66+
>>> get_word_path(board, "CATS", 0, 0, 0, {0}, [])
67+
[(0, 0), (0, 1), (0, 2), (1, 2)]
68+
69+
>>> # 2. Find "BC" starting at (2,1).
70+
>>> # Key for (2,1) in 3x3 is: (3 * 3 * 2) + 1 = 19
71+
>>> get_word_path(board, "BC", 2, 1, 0, {19}, [])
72+
[(2, 1), (2, 2)]
73+
74+
>>> # 3. Partial search: current_path already has (0,0), looking for "ATS" starting at (0,1)
75+
>>> # Key for (0,1) is 1. Visited includes the 'C' at key 0.
76+
>>> get_word_path(board, "ATS", 0, 1, 0, {0, 1}, [(0, 0)])
77+
[(0, 0), (0, 1), (0, 2), (1, 2)]
6378
"""
6479

6580
if board[row][column] != word[word_index]:

0 commit comments

Comments
 (0)