题解 | #识别有效的IP地址和掩码并进行分类统计#

识别有效的IP地址和掩码并进行分类统计

http://www.nowcoder.com/practice/de538edd6f7e4bc3a5689723a7435682

难点在于下面两个部分:

  1. 输入的IP地址和子网掩码是一体的,其中有一个错了,那就是错了,这时候就不要在计算IP是不是A,B,C,D,E类以及是不是私有IP地址了。
  2. 第二个难点在于判断子网掩码是否正确,我在这里使用了简单粗暴的方式“循环”。除此之外还有一种方式return ((((mark ^ (mark - 1)) >> 1) ^ mark) == -1)
def mask_func(MASK):
    if (0 < MASK[0] <= 255) and (0 < MASK[0] <= 255) and (0 < MASK[0] <= 255) and (0 < MASK[0] <= 255):
        if 0 == MASK[0] and 0 == MASK[1] and 0 == MASK[2] and 0 == MASK[3]:
            return 0
        elif 255 == MASK[0] and 255 == MASK[1] and 255 == MASK[2] and 255 == MASK[3]:
            return 0
        flag = 1
        bin_mask = ''
        for m in MASK:
            fill = 8 - len(bin(m)[2:])
            bin_mask += fill * '0' + bin(m)[2:]
        for m in str(bin_mask)[2:]:
            if flag:
                if m == '0':
                    flag = 0
            else:
                if m == '1':
                    return 0
        else:
            return 1
    else:
        return 0

count_list = [0 for x in range(7)]
while True:
    try:
        tmp = input().split('~')
    except:
        break
    ip_list = tmp[0].split('.')
    mask_list = tmp[1].split('.')
    IP = []
    MASK = []
    for ip in ip_list:
        if ip.isalnum():
            IP.append(int(ip))
        else:
            IP.append(-1)
    for mask in mask_list:
        if mask.isalnum():
            MASK.append(int(mask))
        else:
            MASK.append(-1)
            
    if (1 <= IP[0] <= 126) and (0 <= IP[1] <= 255) and (0 <= IP[2] <= 255) and (0 <= IP[3] <= 255):
        if mask_func(MASK):
            count_list[0] += 1
            if (10 == IP[0]):
                count_list[6] += 1
        else:
            count_list[5] += 1
    elif (128 <= IP[0] <= 191) and (0 <= IP[1] <= 255) and (0 <= IP[2] <= 255) and (0 <= IP[3] <= 255):
        if mask_func(MASK):
            count_list[1] += 1
            if (172 == IP[0]) and (16 <= IP[1] <= 31):
                count_list[6] += 1
        else:
            count_list[5] += 1
    elif (192 <= IP[0] <= 223) and (0 <= IP[1] <= 255) and (0 <= IP[2] <= 255) and (0 <= IP[3] <= 255):
        if mask_func(MASK):
            count_list[2] += 1
            if (192 == IP[0]) and (168 == IP[1]):
                count_list[6] += 1
        else:
            count_list[5] += 1
    elif (224 <= IP[0] <= 239) and (0 <= IP[1] <= 255) and (0 <= IP[2] <= 255) and (0 <= IP[3] <= 255):
        if mask_func(MASK):
            count_list[3] += 1
        else:
            count_list[5] += 1
    elif (240 <= IP[0] <= 255) and (0 <= IP[1] <= 255) and (0 <= IP[2] <= 255) and (0 <= IP[3] <= 255):
        if mask_func(MASK):
            count_list[4] += 1
        else:
            count_list[5] += 1
    elif (0 == IP[0]) or (127 == IP[0]):
        ...
    else:
        count_list[5] += 1
for i in count_list:
    print(i,end=' ')
        
全部评论

相关推荐

华为 ai软件开发 薪资20k x (14-16),职级13A,5%公积金,c/cpp
点赞 评论 收藏
分享
牛客5655:其他公司的面试(事)吗
点赞 评论 收藏
分享
喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务