Skip to content

trie class and insert complete with all tests passing - #24

Open
vbenavente wants to merge 6 commits into
masterfrom
trie
Open

trie class and insert complete with all tests passing#24
vbenavente wants to merge 6 commits into
masterfrom
trie

Conversation

@vbenavente

Copy link
Copy Markdown
Owner

No description provided.

Comment thread src/trie.py
start = self.root
for letter in token:
start = start.setdefault(letter, OrderedDict())
start['#'] = '#'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Since you're using '#' as your end of word token throughout your code, it would be a great idea and correct python usage to set a global constant e.g. TERM_CHAR = '#'

Comment thread src/trie.py Outdated
return False
counter += 1
try:
return start['#'] == '#'

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This seems out of place here. It would be more explicit and assertive to do this check only once you've reached the very end of your token, so you check for sure once you exit the loop that the end-of-word char is sitting there.

Otherwise it seems you're taking an approach of "I know this is gonna work so I'll just leave it in there."

This will fail in a certain case. Suppose I insert the word "Python" into the tree. When you call contains("Pythonista"), it will return True though I have not inserted that word.

On that note, also test for a scenario like the above to make sure contains("Py") returns False.

Comment thread src/trie.py
pending_list.append((k, v))

def traversal(self, start=None):
import pdb; pdb.set_trace()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

We got a PDB on the loose here!

Comment thread src/test_trie.py
def test_contain_false_1():
"""Test if a trie contains a word."""
trie = Trie(['cow', 'coward', 'company'])
assert not trie.contains('co')

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a good test; interestingly, it is failing for many of the test cases I have made. Usually with longer strings. Try this when instantiating the Trie empty then pushing in each item. Also try with much longer strings e.g. "this_long_string" then check contains("this_long_strin")

Comment thread src/trie.py Outdated
try:
return start['#'] == '#'
except KeyError:
pass

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I spent some more time analyzing your algorithm. It has a problem where it incorrectly returns True when you check contains() on a stub of an inserted item.

e.g. when you insert "Python", contains("Py") should return False, but you're getting True.

I identified this line as the issue. Consider it very closely. What scenario is leading to this line being hit? What should you do instead?

Comment thread src/trie.py
if not start:
raise KeyError('Start point not found')
pending_list = []
for k, v in reversed(start.items()):

@WillWeatherford WillWeatherford Sep 30, 2016

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What's the idea here with reversing the keys and values? They are in arbitrary order in a dict, so it shouldn't make a difference.

Edit: I forgot that you're using an OrderedDict. Still though, this is a costly operation which shouldn't be needed.

Comment thread src/trie.py
for k, v in cur[1].items():
pending_list.append((k, v))

def traversal(self, start=None):

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I'll tell you guys now that you are over-complicating this traverse. Even though you're using an OrderedDict, it's OK it the traversal returns items in an arbitrary order.

Consider a recursive approach where you are simply concatenating together longer and longer strings, and finally yielding them whenever you identify that there is an end-of-token char present,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants