Huawei computer examination - identifying a valid address and mask Ip implemented in Python and classification

To find a job that will be will be, um, Nima really hard ...

Start from scratch, python before but never learned very optimistic about the future with more and more people, after all too easy to use, and we can not be behind the times, take Huawei computer examination practiced hand it!

topic:

Please resolve the IP address and the corresponding mask for classification. Accordance with the requirements A / B / C / D / E class classification address, invalid address and mask separately categorized. 

All IP addresses are divided into A, B, C, D, E five 

Class A addresses 1.0.0.0 ~ 126.255.255.255; 

Class B addresses 128.0.0.0 ~ 191.255.255.255; 

Class C address 192.0.0.0 ~ 223.255.255.255; 

Class D address 224.0.0.0 ~ 239.255.255.255; 

Class E addresses 240.0.0.0 ~ 255.255.255.255 

 

Private IP range is: 

10.0.0.0~10.255.255.255 

172.16.0.0~172.31.255.255 

192.168.0.0~192.168.255.255 

 
The subnet mask in front of the binary 1 is continuous, and are all 0. (Example: 255.255.255.32 is an invalid mask
 

Enter a description:

Multi-line strings. Each IP address and mask, separated by ~.

Output Description:

Statistics A, B, C, D, E, the IP address error or an error mask, the private IP number, separated by a space between.

 
Ideas:
This question online many ways, the title is relatively simple, I chose one of the methods to achieve,
Ip >> check whether compliance;
>> check whether the mask compliance;
>> Ip and masks are on the case of compliance Ip classification;
>> continue to determine whether the private network Ip;
 
Code:
#!/usr/bin/python
# -*- coding: utf-8 -*
import sys
A=0
B=0
C=0
D=0
E=0
error=0
privateIp=0
lastcode=['254','252','248','240','224','192','128','0']
def CheckIp(ip):
    if len(ip) !=4 or '' in ip:
        return False
    else:
        for i in range(4):
            if int(ip[i]) < 0 or int(ip[i]) > 255:
                return False
            else:
                return True
def CheckMask(mask):
    if len(mask)!=4:
        return False
    else:
        if mask[0]=='255':
            if mask[1]=='255':
                if mask[2]=='255':
                    if mask[3] in lastcode:
                        return True
                    else:
                        return False
                elif mask[2] in lastcode and mask[3]=='0':
                     return True
                else:
                     return False
            elif mask[1] in lastcode and mask[2]==mask[3]=='0':
                return True
            else:
                return False
        elif mask[0] in lastcode and mask[1]==mask[2]==mask[3]=='0':
            return True
        else:
            return False
while True:
    Input= sys.stdin.readline().strip()
    if Input == '':
        break
    ipList=Input.split('~')[0]
    maskList=Input.split('~')[1]
    ip=ipList.split('.')
    mask=maskList.split('.')
    if CheckIp(ip) and CheckMask(mask):
        if 1<=int(ip[0])<=126:
            A+=1
        if 128<=int(ip[0])<=191:
            B+=1
        if 192<=int(ip[0])<=223:
            C+=1
        if 224<=int(ip[0])<=239:
            D+=1
        if 240<=int(ip[0])<=255:
            E+=1
        if int(ip[0])==10 or (int(ip[0])==172 and 16<=int(ip[1])<=31) or (int(ip[0])==192 and int(ip[1])==168):
            privateIp+=1
    else:
        error+=1
    print(Input)
print('{} {} {} {} {} {} {}'.format(A,B,C,D,E,privateIp,error))

to sum up:

Novice, so take a lot of detours, python functions, loops are not {} braces are organized, so if you are like me used to use c # or Java, it just started high probability I Nyima, because the code did not align possible the result would be completely different, so pay more attention under the bar, the other nothing, anyway, as long as the feeling of English can understand the point of writing ...

Guess you like

Origin www.cnblogs.com/Alwaysblue/p/12173475.html