63.9k views
2 votes
An IP address is divided into 4 sections where each section contains 1-3 digits. An example of an IP address would be 192.168.0.1. Now IP addresses have 2 parts; One is the network part while the other one is the host part. If the network part is 1, we get a special address from the given IP address where the 1st section will remain as it is and the rest of the 3 sections will become 0. If the network part is 2, we get a special address from the given IP address where the first 2 sections will remain as it is and the rest of the 2 sections will become 0. The same goes for other values of the network part. For simplicity, we will consider the network part can be 1 or 2 or 3. Let's see an example:

IP address: 192.168.1.10
If network part = 1, then the special address = 192.0.0.0.
If network part = 2, then the special address = 192.168.0.0.
If network bits = 3, then the special address = 192.168.1.0.

All IP addresses starting like the special address will fall under the same special network. You will be given the value of the network part and a list of IP addresses as input. Your task will be to create a dictionary where the keys will be the unique special addresses and the corresponding values will be the list of IP addresses that fall under the same special network.

Input format: Value of the network part List of IP addresses separated by ","

Sample Input 1:
1, 192.168.1.1, 192.168.1.2, 192.168.1.3, 192.168.1.4, 192.168.1.5
Sample Output 1:
{
192.0.0.0: [192.168.1.1, 192.168.1.2, 192.168.1.3],
192.0.0.0: [192.168.1.4, 192.168.1.5],
192.0.0.0: []
}

Sample Input 2:
2, 192.168.1.6, 192.168.1.7, 192.168.1.8, 192.168.1.9, 192.168.1.10
Sample Output 2:
{
192.168.0.0: [192.168.1.6, 192.168.1.7],
192.168.0.0: [192.168.1.8, 192.168.1.9],
192.168.0.0: [192.168.1.10]

1 Answer

1 vote

Final answer:

An IP address is divided into 4 sections: the network part and the host part. The network part determines a special address by setting certain sections to 0. The task is to create a dictionary with the special addresses as keys and the IP addresses falling under the same special network as values.

Step-by-step explanation:

An IP address is divided into 4 sections, where each section contains 1-3 digits. The network part of the IP address determines the special address. If the network part is 1, the special address will have the first section remaining the same and the rest of the sections becoming 0. If the network part is 2, the first 2 sections remain the same and the rest become 0. The same applies for other values of the network part.

For example, if the IP address is 192.168.1.10 and the network part is 1, the special address would be 192.0.0.0. If the network part is 2, the special address would be 192.168.0.0. If the network part is 3, the special address would be 192.168.1.0.

The task is to create a dictionary where the keys are the unique special addresses and the values are the list of IP addresses that fall under the same special network.

For the given input:

Sample Input 1: 1, 192.168.1.1, 192.168.1.2, 192.168.1.3, 192.168.1.4, 192.168.1.5

Sample Output 1: { 192.0.0.0: [192.168.1.1, 192.168.1.2, 192.168.1.3], 192.0.0.0: [192.168.1.4, 192.168.1.5], 192.0.0.0: [] }

Sample Input 2: 2, 192.168.1.6, 192.168.1.7, 192.168.1.8, 192.168.1.9, 192.168.1.10

Sample Output 2: { 192.168.0.0: [192.168.1.6, 192.168.1.7], 192.168.0.0: [192.168.1.8, 192.168.1.9], 192.168.0.0: [192.168.1.10] }