[Tutor] Fwd: Re: dynamical program to create subnets CIDR
DL Neil
PyTutor at danceswithmice.info
Mon Oct 7 18:43:52 EDT 2019
Please consider also that maintaining the order of the conversation
makes it easier to follow!
(comments below)
> On Monday, October 7, 2019, 02:54:23 PM EDT, Alan Gauld
> <alan.gauld at btinternet.com> wrote:
> On 07/10/2019 19:31, ose micah wrote:
>> Can anyone help with a program that could compare a list of IPs and
>> create the least possible CIDR block sets from these IPs.
>> for example.
>> I have a list say:
> ...
>> I want to reduce this list to:
>> 10.32.0.0/14
>> 202.238.149.0/19
>> 10.224.0.0/13
>> 10.54.63.0/13
>
> Sorry but I cannot see how you get to the second list
> from the first. I think you need to give us a clue what
> the high level algorithm is. Don't assume we know your
> problem domain. Most of us don't, we know Python.
>
> At a basic level I'd suggest investigating functions
> like str.split(), sort(), max/min(). But even that
> does not appear sufficient to meet your needs.
> Without understanding the criteria I can't guess the algorithm.
> -------- Forwarded Message --------
> Subject: Re: dynamical program to create subnets CIDR
> Date: Mon, 7 Oct 2019 19:35:11 +0000 (UTC)
> From: ose micah <osaosemwe at yahoo.com>
> To: alan.gauld at yahoo.co.uk
> Basically, the algorithm I have in mind is one that takes the first Ip
> in the list, and compares it to others in the list. It can definitely be
> improved on.??
> From the list of subnets, it pick the first subnet and compares it with
> others, such that if the first and second set of 3 digit are same in the
> subnet, it pushes this to a temp list, so that it combines all these
> subnets into one subnet, and overwrites the new subnet created (which is
> the least CIDR block of the subnets in the temp list) and the rest
> subnet to a file. If the number of subnets in the file is not less or
> equals to four, it continues the process comparing list of subnets.??
> Next if the after comparing the first and the second set of 3 digit, it
> has not yielded the result needed, it compares the first set of 3 digits
> in the IPs left. it create a list of those that are same, and generates
> a subnet of least CIDR block to the list left until the least of CIDR is
> 4 or less than 4.??
A dictionary is a very useful way of 'classifying' items, eg
people[ 'male' ] = 'Fred'
people[ 'female' ] = 'Wilma'
A set is a data structure for grouping more than one 'similar' item, eg
couple == { 'Fred', 'Wilma' }
Perhaps break the larger problem into two:
- which sub-nets are relevant?
- how should each sub-net be described?
Possibility:
- each time a new sub-net is 'discovered' use the higher two octets as
the key to a dictionary
- make the dictionary-entry's value a set, add-ing the lower octets as
its first value/member
- when the next item on the source-data list matches an existing sub-net
(key) in the dictionary (easy and fast comparison), add the lower octets
to the value-set
- afterwards, consider each sub-net in the dictionary in-turn, and
rationalise the CIDR addresses.
--
Regards =dn
More information about the Tutor
mailing list