Skip to content

Conversation

@BrianLusina
Copy link
Owner

@BrianLusina BrianLusina commented Nov 21, 2025

Describe your change:

This adds a function is_prefix_of_word that enables searching for the
index of a search_word in a given sentence and return the index if it
exists. The index is 1-based.

This modifies the TrieNode allowing callers to use the TrieNode to
build a trie from the node and perform prefix searches a well as
modifies the Trie class as well adding modifications and optimizations
that match the changes in the TrieNode class.

Also note that this change includes an index to the TrieNode to
track the earliest index of a given character in a sentence. That is,
what is the earliest that a word has been seen, This allows returning
multiple words in the case of using the Trie search functionality.

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

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. To ease review, please open separate PRs for separate algorithms.
  • 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 have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Summary by CodeRabbit

  • New Features

    • Added prefix-detection function to report the 1-based position of a word when a search term is a prefix.
  • Documentation

    • Added README for the new prefix feature and updated image asset paths in problem docs.
    • Added directory listing entry for the new prefix problem.
  • Tests

    • Added unit tests for the prefix feature and an additional test case for an existing algorithm.
  • Style

    • Minor comment wording tweak.

✏️ Tip: You can customize this high-level summary in your review settings.

BrianLusina and others added 3 commits November 21, 2025 10:22
This adds a function `is_prefix_of_word` that enables searching for the
index of a `search_word` in a given sentence and return the index if it
exists. The index is 1-based.

This modifies the `TrieNode` allowing callers to use the `TrieNode` to
build a trie from the node and perform prefix searches a well as
modifies the `Trie` class as well adding modifications and optimizations
that match the changes in the `TrieNode` class.

Also note that this change includes an `index` to the `TrieNode` to
track the earliest index of a given character in a sentence. That is,
what is the earliest that a word has been seen, This allows returning
multiple words in the case of using the `Trie` `search` functionality.
@BrianLusina BrianLusina self-assigned this Nov 21, 2025
@BrianLusina BrianLusina added enhancement Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates Trees Trie labels Nov 21, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 21, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Adds a new "Is Prefix" feature under pystrings with implementation, tests, and README; enhances the Trie and TrieNode to track and return word indices; updates a README image path, adds a happy number test, and a minor comment wording fix.

Changes

Cohort / File(s) Summary
Trie core changes
datastructures/trees/trie/trie.py, datastructures/trees/trie/trie_node.py
Trie/TrieNode extended with an optional index field and index-aware insert logic; new search_prefix on TrieNode; insert signature updated to accept index: Optional[int]; __repr__ added; DFS/prefix assembly logic adjusted.
New Is Prefix feature
pystrings/is_prefix/__init__.py, pystrings/is_prefix/README.md, pystrings/is_prefix/test_is_prefix_of_word.py
New is_prefix_of_word(sentence, search_word) implemented using the enhanced Trie; README and tests added covering positive, negative, and boundary cases; returns 1-based index or -1.
Directory listing
DIRECTORY.md
Added "Is Prefix" subsection under Pystrings.
README asset paths
algorithms/fast_and_slow/happy_number/README.md
Updated example and solution image links to centralized images/... paths.
Tests & minor comment
algorithms/fast_and_slow/happy_number/test_happy_number.py, datastructures/streams/stream_checker/__init__.py
Added test_10 to HappyNumberTestCase2 asserting is_happy_number_2(20) is False; changed comment wording from "dead end" to "dead-end".

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant API as is_prefix_of_word
    participant Trie as Trie/TrieNode

    Caller->>API: is_prefix_of_word(sentence, search_word)
    API->>Trie: create root TrieNode

    loop insert each word with 1-based index
        API->>Trie: insert(word, index)
        Trie->>Trie: traverse chars, set node.index = min(node.index, index)
        Trie->>Trie: mark end_of_word
    end

    API->>Trie: search_prefix(search_word)
    alt path exists for all chars
        Trie-->>API: return node.index (or -1 if unset)
    else missing char
        Trie-->>API: return -1
    end

    API-->>Caller: return index or -1
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

  • Pay attention to index propagation/min semantics in trie_node.py insert.
  • Validate search_prefix return values and edge cases (empty search word, no index set).
  • Verify trie.py DFS/prefix assembly changes do not alter other Trie behavior or tests.

Possibly related PRs

Poem

🐰 I hop through nodes with tiny feet and cheer,
I plant each word index so answers appear,
A prefix found, I thump my little rhyme,
From sentence to node — I count, I find, I time,
Hop, search, return — a rabbit's coding cheer!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 42.86% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: adding an 'is prefix of word' feature that combines strings and trie data structures.
Description check ✅ Passed The description follows the template structure with filled-in sections and all checklist items addressed. It clearly explains the changes including the new function, TrieNode modifications, and index tracking feature.

📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cbf1727 and 3b41d91.

📒 Files selected for processing (2)
  • datastructures/trees/trie/trie.py (3 hunks)
  • datastructures/trees/trie/trie_node.py (2 hunks)

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
pystrings/is_prefix/test_is_prefix_of_word.py (1)

5-54: LGTM! Comprehensive test coverage.

The test suite covers the key scenarios:

  • Prefix matching at different positions (beginning, middle, end)
  • No match cases returning -1
  • Multiple words sharing a prefix (verifies earliest index is returned)
  • Proper 1-based indexing

Consider adding edge case tests for completeness (though current coverage is good):

  • Empty sentence or search_word
  • search_word equals an entire word (not just prefix)
  • Single-word sentence
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 484b865 and cbf1727.

⛔ Files ignored due to path filters (18)
  • algorithms/fast_and_slow/happy_number/images/example/example_1_1.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/example/example_1_2.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/example/example_1_3.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/example/example_2_1.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/example/example_2_2.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/example/example_2_3.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_1.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_2.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_3.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_4.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_5.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_6.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_7.png is excluded by !**/*.png
  • algorithms/fast_and_slow/happy_number/images/solution/solution_example_8.png is excluded by !**/*.png
  • pystrings/is_prefix/images/examples/is_prefix_example_1.png is excluded by !**/*.png
  • pystrings/is_prefix/images/examples/is_prefix_example_2.png is excluded by !**/*.png
  • pystrings/is_prefix/images/examples/is_prefix_example_3.png is excluded by !**/*.png
  • pystrings/is_prefix/images/examples/is_prefix_example_4.png is excluded by !**/*.png
📒 Files selected for processing (9)
  • DIRECTORY.md (1 hunks)
  • algorithms/fast_and_slow/happy_number/README.md (1 hunks)
  • algorithms/fast_and_slow/happy_number/test_happy_number.py (1 hunks)
  • datastructures/streams/stream_checker/__init__.py (1 hunks)
  • datastructures/trees/trie/trie.py (3 hunks)
  • datastructures/trees/trie/trie_node.py (2 hunks)
  • pystrings/is_prefix/README.md (1 hunks)
  • pystrings/is_prefix/__init__.py (1 hunks)
  • pystrings/is_prefix/test_is_prefix_of_word.py (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (5)
pystrings/is_prefix/__init__.py (2)
datastructures/trees/trie/trie_node.py (3)
  • TrieNode (5-62)
  • insert (26-41)
  • search_prefix (43-62)
datastructures/trees/trie/trie.py (1)
  • insert (9-49)
pystrings/is_prefix/test_is_prefix_of_word.py (1)
pystrings/is_prefix/__init__.py (1)
  • is_prefix_of_word (4-23)
datastructures/trees/trie/trie.py (1)
datastructures/trees/trie/trie_node.py (2)
  • TrieNode (5-62)
  • insert (26-41)
algorithms/fast_and_slow/happy_number/test_happy_number.py (1)
algorithms/fast_and_slow/happy_number/__init__.py (1)
  • is_happy_number_2 (39-102)
datastructures/trees/trie/trie_node.py (1)
datastructures/trees/trie/trie.py (1)
  • insert (9-49)
🪛 LanguageTool
algorithms/fast_and_slow/happy_number/README.md

[style] ~33-~33: Using many exclamation marks might seem excessive (in this case: 8 exclamation marks for a text that’s 1193 characters long)
Context: ...images/solution/solution_example_1.png) Solution Example 2 Solution Example 3 Solution Example 4 Solution Example 5 Solution Example 6 ![Solution Example 7](images/solution/sol...

(EN_EXCESSIVE_EXCLAMATION)

🪛 markdownlint-cli2 (0.18.1)
DIRECTORY.md

710-710: Unordered list indentation
Expected: 0; Actual: 2

(MD007, ul-indent)


711-711: Unordered list indentation
Expected: 2; Actual: 4

(MD007, ul-indent)

🔇 Additional comments (9)
algorithms/fast_and_slow/happy_number/test_happy_number.py (1)

116-120: Test looks correct but appears unrelated to PR objectives.

The test correctly verifies that 20 is not a happy number (it cycles: 20→4→16→37→58→89→145→42→20). However, this change seems unrelated to the PR objectives, which describe adding is_prefix_of_word functionality and Trie modifications.

algorithms/fast_and_slow/happy_number/README.md (1)

18-40: Image path reorganization looks good but appears unrelated to PR objectives.

The image paths have been appropriately updated to use an organized directory structure (images/example/ and images/solution/). However, like the test file changes, this documentation update seems unrelated to the PR objectives about adding is_prefix_of_word functionality and Trie modifications.

Note: The static analysis hint about excessive exclamation marks is a false positive—these are Markdown image syntax (![alt](path)), not punctuation.

datastructures/streams/stream_checker/__init__.py (1)

61-61: LGTM! Minor grammar improvement.

The hyphenation of "dead-end" as a compound adjective is grammatically correct and improves readability.

DIRECTORY.md (1)

710-711: LGTM! Documentation update for new feature.

The new entries correctly document the "Is Prefix" feature and follow the existing structure and indentation pattern used throughout the file.

pystrings/is_prefix/README.md (1)

1-24: LGTM! Clear and comprehensive documentation.

The README provides a well-structured problem description with constraints and examples. The explanation of prefix definition and return value semantics (1-based indexing, earliest match, -1 for no match) aligns well with the implementation.

pystrings/is_prefix/__init__.py (1)

4-23: LGTM! Clean and correct implementation.

The function correctly:

  • Builds a Trie from sentence words with 1-based indices (line 19)
  • Delegates prefix search to the TrieNode implementation
  • Returns the appropriate index or -1

The implementation is straightforward and matches the documented behavior.

datastructures/trees/trie/trie_node.py (2)

1-1: LGTM! Clean addition of index tracking.

The new index attribute with proper typing and inclusion in __repr__ provides good foundation for tracking word positions in the Trie.

Also applies to: 21-21, 24-24


26-41: LGTM! Correct index tracking implementation.

The insert method properly maintains the minimum index along each character path using curr.index or float("inf") to handle the None case. This ensures that when multiple words share a prefix, the earliest word's index is preserved at each node.

datastructures/trees/trie/trie.py (1)

88-89: LGTM! Helpful debugging addition.

The __repr__ method provides a useful string representation that will recursively show the trie structure via the TrieNode's __repr__ method.

@BrianLusina BrianLusina merged commit f74b0b9 into main Nov 22, 2025
4 of 7 checks passed
@BrianLusina BrianLusina deleted the feat/is-prefix-of-word branch November 22, 2025 08:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Algorithm Algorithm Problem Datastructures Datastructures Documentation Documentation Updates enhancement Trees Trie

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants