Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
"City's Happiness" solution in Clear category for City's Happiness by tigerhu3180
def most_crucial(net, users):
dict1={}
'''SAMPLE: most_crucial([
['A', 'B'],
['B', 'C'],
['C', 'D'],
['C', 'E']
],{
'A': 10,
'B': 20,
'C': 10,
'D': 20
})
'''
for i in users.keys():
# broken node:B => dict1=>{'B':20}
dict1[i]=users[i]
goodnet=[]
for j in net:
if i not in j:
goodnet.append(j)
# goodnet => [['C','D'],['C','E']]
# isolated => ['A']
isolated=set(users.keys())-set(i for i in str(goodnet) if i.isalpha())-set(i)
dict1[i]+=sum(users[i]**2 for i in isolated)
# goodnet = [['C','D'],['C','E']] => [['C','D','E']]
while goodnet:
list1=[set(goodnet.pop(0))]
while list1:
m=list1.pop(0)
for n in goodnet:
if len(m&set(n))>0:
m=m|set(n)
list1.append(m)
goodnet.remove(n)
break
# sum(['C','D','E'])
dict1[i]+=sum(users[n] for n in m)**2
return list(filter(lambda x:dict1[x]==min(dict1.values()),dict1.keys()))
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert most_crucial([
['A', 'B'],
['B', 'C']
],{
'A': 10,
'B': 10,
'C': 10
}) == ['B'], 'First'
assert most_crucial([
['A', 'B']
],{
'A': 20,
'B': 10
}) == ['A'], 'Second'
assert most_crucial([
['A', 'B'],
['A', 'C'],
['A', 'D'],
['A', 'E']
],{
'A': 0,
'B': 10,
'C': 10,
'D': 10,
'E': 10
}) == ['A'], 'Third'
assert most_crucial([
['A', 'B'],
['B', 'C'],
['C', 'D']
],{
'A': 10,
'B': 20,
'C': 10,
'D': 20
}) == ['B'], 'Forth'
print('Nobody expected that, but you did it! It is time to share it!')
July 1, 2018