Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
D
dict-trie
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Laros
dict-trie
Commits
35176088
Commit
35176088
authored
Aug 05, 2017
by
Laros
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added counting in iterators.
parent
70c13294
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
18 additions
and
4 deletions
+18
-4
dict_trie/dict_trie.py
dict_trie/dict_trie.py
+11
-4
tests/test_trie.py
tests/test_trie.py
+7
-0
No files found.
dict_trie/dict_trie.py
View file @
35176088
...
...
@@ -67,20 +67,24 @@ def _remove(node, word, count):
return
result
def
_iterate
(
path
,
node
):
def
_iterate
(
path
,
node
,
unique
):
"""Convert a trie into a list.
:arg str path: Path taken so far to reach the current node.
:arg dict node: Current node.
:arg bool unique: Do not list multiplicities.
:returns iter: All words in the trie.
"""
if
''
in
node
:
if
not
unique
:
for
_
in
range
(
1
,
node
[
''
][
'count'
]):
yield
path
yield
path
for
char
in
node
:
if
char
:
for
result
in
_iterate
(
path
+
char
,
node
[
char
]):
for
result
in
_iterate
(
path
+
char
,
node
[
char
]
,
unique
):
yield
result
...
...
@@ -95,7 +99,7 @@ def _fill(node, alphabet, length):
{alphabet}.
"""
if
not
length
:
node
[
''
]
=
{}
node
[
''
]
=
{
'count'
:
1
}
return
for
char
in
alphabet
:
...
...
@@ -198,7 +202,10 @@ class Trie(object):
return
''
in
_find
(
self
.
root
,
word
)
def
__iter__
(
self
):
return
_iterate
(
''
,
self
.
root
)
return
_iterate
(
''
,
self
.
root
,
True
)
def
list
(
self
,
unique
=
True
):
return
_iterate
(
''
,
self
.
root
,
unique
)
def
add
(
self
,
word
,
count
=
1
):
_add
(
self
.
root
,
word
,
count
)
...
...
tests/test_trie.py
View file @
35176088
...
...
@@ -110,6 +110,13 @@ class TestTrie(object):
def
test_iter
(
self
):
assert
list
(
self
.
_trie
)
==
[
'abc'
,
'abd'
,
'te'
,
'test'
]
def
test_list
(
self
):
assert
list
(
self
.
_trie
.
list
())
==
list
(
self
.
_trie
)
def
test_list_non_unique
(
self
):
assert
list
(
self
.
_trie
.
list
(
False
))
==
[
'abc'
,
'abd'
,
'abd'
,
'te'
,
'test'
]
def
test_fill
(
self
):
trie
=
Trie
()
trie
.
fill
((
'a'
,
'b'
),
3
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment