-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07p2.py
68 lines (62 loc) · 2.07 KB
/
07p2.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
60
61
62
63
64
65
66
67
68
from fileinput import input
data = input("7.txt")
relations = {}
weights = {}
groupWeights = {}
levels = {}
every = []
def calculateLevels(key):
global relations
level = 0
for value in list(relations.values()):
if key in value:
level += 1
wantedKey = list(relations.keys())[list(relations.values()).index(value)]
#wantedKey = x for x,y in relations.items() if y==value
level += calculateLevels(wantedKey)
return level
def branchWeight(leaf):
global relations,weights
summed = weights[leaf]
if leaf not in list(relations.keys()):
return summed
for kid in list(relations[leaf]):
summed += branchWeight(kid)
return summed
for row in data:
splited = row.split(" ")
weights[splited[0]] = int(splited[1].strip()[1:-1])
if "->" in splited:
parent = splited[0]
kids = []
index = splited.index("->")
for i in range(index+1,len(splited)):
kids.append(splited[i].strip().strip(',').strip('\n'))
relations[parent] = tuple(kids)
for parent in list(relations.keys()):
groupWeights[parent] = branchWeight(parent)
maxLvl = 0
for kid in list(weights.keys()):
levels[kid] = calculateLevels(kid)
if levels[kid]>maxLvl:
maxLvl=levels[kid]
for i in range(0,maxLvl+1):
print("Level",i)
for leaf in list(relations.keys()):
if levels[leaf]==i:
allBws = []
bws = {}
for kid in list(relations[leaf]):
if kid not in list(relations):
bws[kid] = weights[kid]
allBws.append(weights[kid])
else:
bws[kid] = groupWeights[kid]
allBws.append(groupWeights[kid])
if(len(set(allBws))==1):
continue
culprit = list(bws.keys())[list(bws.values()).index(min(allBws,key=allBws.count))]
allBws.remove(bws[culprit])
delta = bws[culprit]-allBws[0]
print(culprit,relations[culprit])
print(weights[culprit]-delta)