Skip to content

Commit 06ba817

Browse files
committed
simplify method and reformat code to PEP8 style
1 parent 1d19fd8 commit 06ba817

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

trees/trie.py

+25-15
Original file line numberDiff line numberDiff line change
@@ -5,64 +5,74 @@
55
start_with_prefix(prefix)
66
"""
77
# HELPERS #
8+
9+
810
def _get_child_branches(tr):
911
return tr[1:]
1012

13+
1114
def _get_child_branch(tr, c):
15+
"""
16+
Find matching branch with the character
17+
"""
1218
for branch in _get_child_branches(tr):
1319
if branch[0] == c:
1420
return branch
21+
1522
return None
1623

24+
1725
def _retrive_branch(k, trie_list):
18-
if k == "":
26+
if not k:
1927
return None
2028
tr = trie_list
2129
for c in k:
2230
child_branch = _get_child_branch(tr, c)
2331
if not child_branch:
2432
return None
2533
tr = child_branch
34+
2635
return tr
2736

37+
2838
def _is_trie_bucket(bucket):
2939
if len(bucket) != 2:
3040
return False
31-
if type(bucket[1]) is tuple:
32-
return True
41+
42+
return type(bucket[1]) is tuple
43+
3344

3445
def _get_bucket_key(bucket):
3546
if not _is_trie_bucket(bucket):
3647
return None
37-
return bucket[1][0]
48+
49+
return bucket[1][0]
3850

3951
# HAS_KEY #
52+
53+
4054
def has_key(k, tr):
41-
if k == "":
42-
return None
43-
key_tuple = _retrive_branch(k, tr)
44-
if not key_tuple:
45-
return False
46-
return True
55+
return _retrive_branch(k, tr) is not None
4756

4857
# RETRIE_VAL
58+
59+
4960
def retrie_val(k, tr):
50-
if k == "":
51-
return None
5261
key_tuple = _retrive_branch(k, tr)
5362
if not key_tuple:
5463
return None
64+
5565
return key_tuple[1]
5666

5767

5868
def insert_key(key, v, trie_list):
59-
if key == "" or has_key(key, trie_list):
69+
if not key or has_key(key, trie_list):
6070
return
6171

6272
tr = trie_list
6373
for char in key:
6474
branch = _get_child_branch(tr, char)
65-
if branch == None:
75+
if not branch:
6676
new_branch = [char]
6777
tr.append(new_branch)
6878
tr = new_branch
@@ -137,7 +147,7 @@ def start_with_prefix(prefix, trie):
137147
Washington
138148
West Virginia
139149
Wisconsin
140-
Wyoming"""
150+
Wyoming"""
141151
states_list = [w.strip().lower() for w in states.splitlines() if w]
142152
for state in states_list:
143153
insert_key(state, True, trie)

0 commit comments

Comments
 (0)