-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSyncCore.py
57 lines (45 loc) · 1.64 KB
/
SyncCore.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import json
from typing import List, Dict
def load_json(path):
res = {}
try:
with open(path, "r", encoding="utf-8") as f:
res = json.load(f)
except IOError:
print("Error: file not found. PATH:" + path)
else:
return res
def text_process(text):
return text.title().replace("\"", "").replace("'S", "s").replace("'", "").replace("-", "").replace(" ", "")
def get_sub_attr(sub_map: Dict[str, Dict[float, int]],
sub_stats: List[Dict[str, any]], attr_map: Dict[str, str]) -> List[int]:
def cal_sub_attr(val_map: Dict[float, int], target: float) -> List[int]:
all_solve = combination_sum(list(val_map.keys()), target)
id_list = []
for sub in all_solve[-1]:
id_list.append(val_map[sub])
return id_list
res = []
for sub_stat in sub_stats:
if not sub_stat["key"] is None:
res.extend(cal_sub_attr(sub_map[attr_map[sub_stat["key"]]], sub_stat["value"]))
return res
# https://leetcode.com/problems/combination-sum/
def combination_sum(candidates: List[float], target: float) -> List[List[float]]:
def dfs(candidates, begin, size, path, res, target):
if target == 0:
res.append(path)
return
for index in range(begin, size):
residue = target - candidates[index]
if residue < 0:
break
dfs(candidates, index, size, path + [candidates[index]], res, residue)
size = len(candidates)
if size == 0:
return []
candidates.sort()
path = []
res = []
dfs(candidates, 0, size, path, res, target)
return res