City's Happiness

City's Happiness

Simple+
English RU

Citizens of GridLand are sending emails to each other all the time. They send everything - what they just ate, a funny picture, questions or thoughts that are bothering them right now. All the citizens are happy because they have such a wonderful network that keeps them connected.

The main goal of the Mayor is to control the city's happiness. The city's happiness is a sum of all citizens' happiness. And the happiness of each citizen is equal to the number of citizens (always including oneself) that one can send emails to.

In the mission you need to find the most important district(s) in the city network from the point of letter exchanging. If several districts have the maximal importance, find all of them.

So, how to find this district? Let's look at the example below. You are given a city network of four districts: "A", "B", "C", "D", which are connected by postal service in such a way: "A" with "B", "B" with "C", "C" with "D" and have respective number of users.

To evaluate the importance of a district, you need to calculate the total happiness of the city without this district - the smaller the happiness become, the more important this district is.

  • If a district is considered as excluded from the city network, its users may send letters only to themselves (for example, 10 users send to themselves -> 10 letters -> happiness of the district == 10).
  • If a district is not excluded, but has no connections with other districts, its users may send letters to anyone inside the district, including themselves (10 uses send to 10 -> 100 letters -> happiness == 100).
  • And finally, users of connected districts of course may sent letters to anyone inside this particular connected network (10 + 20 users send to 10 + 20 -> 30*30 letters -> connected network happiness == 900).

The total happiness of the city in each case (with every single district excluded one by one) is a sum of happiness of networks after excluding.

Input: Two arguments: the network structure (as list of connections (list of strings (str)) between the districts), users on each district (as dictionary (dict) where keys are the district names as string (str) and values are the amounts of users as integer (int)).

Output: List of the most crucial districts as strings (str).

Examples:

most_crucial([
    ['A', 'B'],
    ['B', 'C']
],{
    'A': 10,
    'B': 10,
    'C': 10
}) == ['B']

most_crucial([
    ['A', 'B']
],{
    'A': 20,
    'B': 10
}) == ['A']

most_crucial([
    ['A', 'B'],
    ['A', 'C'],
    ['A', 'D'],
    ['A', 'E']
],{
    'A': 0,
    'B': 10,
    'C': 10,
    'D': 10,
    'E': 10
}) == ['A']

most_crucial([
    ['A', 'B'],
    ['B', 'C'],
    ['C', 'D']
],{
    'A': 10,
    'B': 20,
    'C': 10,
    'D': 20
}) == ['B']
40
Settings
Code:
Other:
Invalid hot key. Each hot key should be unique and valid
Hot keys:
CheckiO Extensions

CheckiO Extensions allow you to use local files to solve missions. More info in a blog post.

In order to install CheckiO client you'll need installed Python (version at least 3.8)

Install CheckiO Client first:

pip3 install checkio_client

Configure your tool

checkio --domain=py config --key=

Sync solutions into your local folder

checkio sync

(in beta testing) Launch local server so your browser can use it and sync solution between local file end extension on the fly. (doesn't work for safari)

checkio serv -d

Alternatevly, you can install Chrome extension or FF addon

checkio install-plugin
checkio install-plugin --ff
checkio install-plugin --chromium

Read more here about other functionality that the checkio client provides. Feel free to submit an issue in case of any difficulties.

Pair Programming (Beta-version)

Welcome to Pair Programming! Engage in real-time collaboration on coding projects by starting a session and sharing the provided unique URL with friends or colleagues. This feature is perfect for joint project development, debugging, or learning new skills together. Simply click 'Start Session' to begin your collaborative coding journey!

Waiting for Pair Programming to start...

You are trying to join a pair programming session that has not started yet.

Please wait for the session creator to join.

Waiting for Pair Programming to reconnect...

It looks like the creator of the pair programming session closed the editor window.

It might happen accidentally, so that you can wait for reconnection.

×
 
 
<< <
> >>
exec show

Whats Next?

Free accounts will see Best CheckiO solutions with some delay.
Best Solutions will be opened in a moment
Become Awesome and Don't wait
The next stage is ""
Will be activated in
View More Solutions Random Review Solutions Go to the next mission