Skip to content

Add get_word_path function to word_search.py - #14511

Merged
cclauss merged 7 commits into
TheAlgorithms:masterfrom
kadambari25:add-word-path
Sep 11, 2026
Merged

Add get_word_path function to word_search.py#14511
cclauss merged 7 commits into
TheAlgorithms:masterfrom
kadambari25:add-word-path

Conversation

@kadambari25

Copy link
Copy Markdown
Contributor

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests?
  • Documentation change?

Implemented a new function get_word_path in backtracking/word_search.py.

This function returns the list of coordinates (row, column) representing the path of the given word in the board instead of just returning a boolean value.

  • Uses backtracking (DFS) approach
  • Returns list of tuples if the word is found
  • Returns None if the word does not exist

Fixes #14500

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.

@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed labels Apr 6, 2026
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Apr 6, 2026
@cclauss cclauss closed this Sep 4, 2026
@cclauss cclauss reopened this Sep 10, 2026
@algorithms-keeper algorithms-keeper Bot added tests are failing Do not merge until tests pass and removed tests are failing Do not merge until tests pass labels Sep 10, 2026
@cclauss

cclauss commented Sep 11, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev review please as a potential solution to #14500

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

Reviewed against #14500. The algorithm is correct — I ran it on the classic board [["A","B","C","E"],["S","F","C","S"],["A","D","E","E"]]:

  • get_word_path(board, "ABCCED")[(0,0),(0,1),(0,2),(1,2),(2,2),(2,1)] ✅ (valid adjacent, no cell reused)
  • get_word_path(board, "SEE")[(1,3),(2,3),(2,2)]
  • get_word_path(board, "ABCB")None ✅ (correctly refuses to revisit B)
  • non-present word → None

Two things needed before this can merge, both to match the house style already set by word_exists directly above it:

  1. Doctests are missing. The issue promised "comprehensive doctests," and TA requires a doctest for every new function (CI runs them). Please mirror word_exists's doctest block — a couple of found-path cases plus a not-found None case.

  2. No input validation. word_exists validates board/word and raises ValueError with clear messages; get_word_path currently raises a bare IndexError on an empty word (word[0]) or empty board (board[0]). Since the two functions sit in the same file, the cleanest fix is to factor that validation into a small helper both call, or reuse the same guards, so the doctests can assert the same ValueError behavior.

Nit: the nested backtrack helper has no type hints — worth adding for consistency (r: int, c: int, index: int, path: list[tuple[int, int]], visited: set[tuple[int, int]]).

(The DIRECTORY.md Hamiltonian line in the diff is just the auto-generated sync — physics/hamiltonian.py already exists on main — nothing to do there.)

Nice, correct core; just needs the doctests + validation to be consistent with word_exists.

@cclauss cclauss added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Sep 11, 2026
Added a new validation function to check the board and word parameters, ensuring proper input types and values. Updated existing function docstrings for clarity and consistency.
@algorithms-keeper algorithms-keeper Bot removed the awaiting changes A maintainer has requested changes to this PR label Sep 11, 2026
@algorithms-keeper algorithms-keeper Bot added awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Sep 11, 2026
Refactor the validate_board_and_word function call for better readability.
@algorithms-keeper algorithms-keeper Bot removed tests are failing Do not merge until tests pass labels Sep 11, 2026
@algorithms-keeper algorithms-keeper Bot removed the awaiting reviews This PR is ready to be reviewed label Sep 11, 2026
@cclauss
cclauss merged commit afce564 into TheAlgorithms:master Sep 11, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement This PR modified some existing files

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Add get_word_path function to word_search.py

5 participants