The Wayback Machine - https://web.archive.org/web/20200520123835/https://github.com/TheAlgorithms/Python/pull/1894
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix-matching function to trie.py #1894

Open
wants to merge 16 commits into
base: master
from

Conversation

@tasbihaasim
Copy link

tasbihaasim commented Apr 19, 2020

Added a new function match to trie.py. This function takes a prefix as an argument, traces that prefix in a trie and then iterates through all the succeeding letters in the sub-trie such that a list of words containing that prefix is returned.

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}.
data_structures/trie/trie.py Outdated Show resolved Hide resolved
data_structures/trie/trie.py Outdated Show resolved Hide resolved
data_structures/trie/trie.py Outdated Show resolved Hide resolved
data_structures/trie/trie.py Outdated Show resolved Hide resolved
data_structures/trie/trie.py Outdated Show resolved Hide resolved
data_structures/trie/trie.py Show resolved Hide resolved
tasbihaasim added 4 commits Apr 20, 2020
data_structures/trie/trie.py Outdated Show resolved Hide resolved
tasbihaasim and others added 3 commits Apr 20, 2020
Co-Authored-By: Christian Clauss <[email protected]>
lst=[]
l= depth_first_search(sub_trie, '', prefix, lst)
return l
print_words(sub_trie, prefix)

This comment has been minimized.

Copy link
@cclauss

cclauss Apr 20, 2020

Member

Algorithm functions should not print(). Please return the results to the caller.

This comment has been minimized.

Copy link
@doudou-dounsm

doudou-dounsm Apr 22, 2020

The idea might be to add two patterns,
But it doesn't seem necessary.

@@ -89,9 +92,7 @@ def depth_first_search(root: TrieNode, s: str, prefix: str, lst):
if sub_trie==False:
return False

This comment has been minimized.

Copy link
@cclauss

cclauss Apr 20, 2020

Member

Rather than returning False, why not return an empty TrieNode?

tasbihaasim added 2 commits Apr 20, 2020
@tasbihaasim
Copy link
Author

tasbihaasim commented Apr 20, 2020

@cclauss Hey can you look at my changes once again. The code is building and passing pytests but it is still failing Travis Cl and codespell tests. Thank you.

@cclauss
Copy link
Member

cclauss commented Apr 22, 2020

Codespell says ./data_structures/trie/trie.py:84: resturns ==> returns

Travis says >>> get_words_starting_with("a")
NameError("name 'get_words_starting_with' is not defined")

@@ -55,7 +55,7 @@ def get_words_starting_with(self, prefix: str) -> [str]:
:param prefix: the prefix to be matched

This comment has been minimized.

Copy link
@cclauss

cclauss Apr 22, 2020

Member

List[str]

tasbihaasim added 2 commits Apr 22, 2020
Copy link
Contributor

mrmaxguns left a comment

codespell errors


def depth_first_search(node: TrieNode, word: str, lst):
"""
resturns a list of all the nodes in a trie

This comment has been minimized.

Copy link
@mrmaxguns

mrmaxguns May 7, 2020

Contributor

Line 84 is causing codespell tests to fail. Easy typo resturns to returns

itsvinayak and others added 3 commits May 16, 2020
@cclauss
Copy link
Member

cclauss commented May 16, 2020

On line 38 can you please rename find() to __contains__() so we can use in ?
https://docs.python.org/3/reference/datamodel.html#object.__contains__

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.