-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.py
59 lines (44 loc) · 1.53 KB
/
solution.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
58
59
input = [x for x in open('data.txt', 'r').read().split("\n")]
# Building a tree
tree = {}
for x in input:
s = x.split(" -> ")
name_weight = s[0].split(" ")
name = name_weight[0]
weight = int(name_weight[1][1:-1])
if name not in tree:
tree[name] = {"parent": "", "weight": 0, "childlist": []}
tree[name]["weight"] = weight
if s.__len__()>1:
child = s[1].split(", ")
for c in child:
if c not in tree:
tree[c] = {"parent": "", "weight": 0, "childlist": []}
tree[c]["parent"] = name
tree[name]["childlist"].append(c)
def CalcTreeWeight(node):
w = tree[node]["weight"]
for child in tree[node]["childlist"]:
w += CalcTreeWeight(child)
return w
# First part
root = ""
for key,value in tree.items():
if value["parent"]=="":
root = key
print("First part: " + root)
break
# Second part
unbalanced = ""
def FindUnbalancedNode(node):
global unbalanced
ChildWeights = [CalcTreeWeight(x) for x in tree[node]["childlist"]]
if ChildWeights.__len__()>0:
occurrences = [ChildWeights.count(x) for x in ChildWeights]
if min(occurrences)!=max(occurrences):
unbalanced = tree[node]["childlist"][occurrences.index(min(occurrences))]
FindUnbalancedNode(unbalanced)
FindUnbalancedNode(root)
tmp = list(map(CalcTreeWeight, tree[tree[unbalanced]["parent"]]["childlist"]))
corrected = tree[unbalanced]["weight"] - (max(tmp) - min(tmp))
print("Second part: " + str(corrected))