/usr/lib/python3/dist-packages/pyuca/trie.py is in python3-pyuca 1.1-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | class Node:
__slots__ = ("value", "children")
def __init__(self):
self.value = None
self.children = None
class Trie:
def __init__(self):
self.root = Node()
def add(self, key, value):
curr_node = self.root
for part in key:
if curr_node.children is None:
curr_node.children = {}
curr_node = curr_node.children.setdefault(part, Node())
curr_node.value = value
def find_prefix(self, key):
curr_node = self.root
success_index = 0
success_value = None
for i, part in enumerate(key):
if curr_node.children is None or part not in curr_node.children:
break
curr_node = curr_node.children[part]
if curr_node.value:
success_index = i + 1
success_value = curr_node.value
return key[:success_index], success_value, key[success_index:]
|