-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinttrie.py
37 lines (34 loc) · 1.06 KB
/
inttrie.py
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
import math
class IntDecimalStorage:
def __init__(self, number):
self.number = number
def __hash__(self):
return hash(self.number)
def __getitem__(self, index):
if index >= len(self):
raise IndexError("IntDecimalStorage index out of range")
return (self.number / (10 ** index) ) % 10 # TODO make it char
def __len__(self):
return int(math.log10(self.number))+1
class IntDecimalTrie:
def __init__(self):
self.data = {}
def insert(self, iterable, value):
parent = self.data
for i in iterable:
if i in parent:
parent = parent[i]
continue
parent[i] = {'_value': None}
parent = parent[i]
parent['_value'] = value
def contains(self, iterable):
parent = self.data
for i in iterable:
if i in parent:
parent = parent[i]
continue
return False
return parent['_value'] != None
def __str__(self):
return str(self.data)