Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for City's Happiness by tom-tom
def most_crucial(net, users):
def get_happy(crushed):
links = [{a, b} for a, b in net if crushed not in [a, b]]
nodes = users.keys() - {crushed}
result = 0
while nodes:
segment = {nodes.pop()}
segment_n = 0
while segment_n < len(segment):
segment_n = len(segment)
for link in links:
if link & segment:
segment.update(link)
nodes -= segment
result += sum(users[node] for node in segment) ** 2
return result + users[crushed]
happy = {user: get_happy(user) for user in users}
min_value = min(happy.values(), default=None)
return [user for user, value in happy.items() if value == min_value]
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!')
Aug. 24, 2017
Comments: