[548210]: / openomics_web / utils / str_utils.py

Download this file

44 lines (35 with data), 984 Bytes

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
COUNT = "_count"
def make_trie(words):
"""
Args:
words:
"""
root = {}
for word in words:
current_dict = root
for letter in word:
current_dict = current_dict.setdefault(letter, {})
if COUNT not in current_dict:
current_dict[COUNT] = 1
else:
current_dict[COUNT] += 1
return root
def longest_common_prefix(strs):
"""
Args:
strs:
"""
def traverse_trie(dictionary, prefix):
if len(prefix) > 100:
return prefix
if len(dictionary.keys()) == 1 and COUNT not in dictionary.keys():
letter = list(dictionary.keys())[0]
return traverse_trie(dictionary[letter], prefix + letter)
else:
return prefix
trie = make_trie(strs)
lcp_branches = []
for branch in trie:
branch_lcp = traverse_trie(trie[branch], branch)
lcp_branches.append(branch_lcp)
return lcp_branches