-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules.py
43 lines (38 loc) · 1.17 KB
/
Rules.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
38
39
40
41
42
43
# FUNZIONE CHE RITORNA TUTTI I CAMMINI DALLA RADICE ALLE FOGLIE ==> RULES
def all_rules(tr):
rules = []
paths = []
find_rules(tr, rules, paths, 0, False, None)
return paths
def find_rules(tr, path, paths, pathLen, append, value=None):
if tr is None:
return
if append:
if len(path) > pathLen:
path[pathLen] = value
else:
path.append(value)
pathLen = pathLen + 1
if not isinstance(tr, dict):
# trovata la foglia
path.append(tr)
pathLen = pathLen + 1
path_rule = save_rule(path, pathLen)
paths.append(path_rule)
path.pop()
else:
for key in tr.keys():
value = key
find_rules(tr[key], path, paths, pathLen, True, value)
if path:
path.pop()
else:
for key in tr.keys():
value = key
find_rules(tr[key], path, paths, pathLen, True, value)
# SALVA IL ROOT-TO-LEAF PATH
def save_rule(ints, end):
nodes = []
for i in ints[0: end]:
nodes.append(i)
return nodes