Commit 60d82dc3 authored by Jeroen F.J. Laros's avatar Jeroen F.J. Laros

Renaming.

parent ccdb9104
...@@ -28,10 +28,10 @@ class Trie(object): ...@@ -28,10 +28,10 @@ class Trie(object):
""" """
node = self.root node = self.root
for character in word: for char in word:
if character not in node: if char not in node:
return {} return {}
node = node[character] node = node[char]
return node return node
...@@ -41,13 +41,15 @@ class Trie(object): ...@@ -41,13 +41,15 @@ class Trie(object):
def add(self, word): def add(self, word):
""" """
Add a word to the trie. Add a word to the trie.
:arg str word: A word.
""" """
node = self.root node = self.root
for character in word: for char in word:
if character not in node: if char not in node:
node[character] = {} node[char] = {}
node = node[character] node = node[char]
node[''] = {} node[''] = {}
......
...@@ -11,7 +11,7 @@ class TestTrie(object): ...@@ -11,7 +11,7 @@ class TestTrie(object):
def setup(self): def setup(self):
self._trie = Trie(['abc', 'abd', 'test', 'te']) self._trie = Trie(['abc', 'abd', 'test', 'te'])
def test_full_trie(self): def test_root(self):
assert self._trie.root == { assert self._trie.root == {
'a': { 'a': {
'b': { 'b': {
...@@ -21,28 +21,34 @@ class TestTrie(object): ...@@ -21,28 +21,34 @@ class TestTrie(object):
'': {}, '': {},
's': {'t': {'': {}}}}}} 's': {'t': {'': {}}}}}}
def test_prefix_not_in_trie(self): def test_word_present(self):
assert 'ab' not in self._trie
def test_word_in_trie(self):
assert 'abc' in self._trie assert 'abc' in self._trie
def test_suffix_not_in_trie(self): def test_word_absent(self):
assert 'abcd' not in self._trie assert 'abx' not in self._trie
def test_empty_string_present(self):
assert '' in Trie([''])
def test_empty_string_absent(self):
assert '' not in self._trie
def test_prefix_absent_as_word(self):
assert 'ab' not in self._trie
def test_word_not_in_trie(self): def test_too_long_absent(self):
assert 'abe' not in self._trie assert 'abcd' not in self._trie
def test_has_prefix(self): def test_prefix_present(self):
assert self._trie.has_prefix('ab') assert self._trie.has_prefix('ab')
def test_prefix_absent(self):
assert not self._trie.has_prefix('ac')
def test_word_is_prefix(self): def test_word_is_prefix(self):
assert self._trie.has_prefix('abc') assert self._trie.has_prefix('abc')
def test_has_prefix_not(self): def test_too_long_prefix_absent(self):
assert not self._trie.has_prefix('ac')
def test_has_prefix_not_long(self):
assert not self._trie.has_prefix('abcd') assert not self._trie.has_prefix('abcd')
def test_prefix_order(self): def test_prefix_order(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment