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

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

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

python3,re

'''
使用正则拿到ip和掩码
注意的点:
1.ip和掩码要同时考虑,是and的关系,一个有错就要加到error里面
2.我这里验证掩码的时候先转换成了二进制,注意是ob开头,注意要替换,而且还要补0
思路大概就是判断‘01’在不在掩码字符串里面
ps:这道题的筛选很粗糙,其实还可以更细致的筛选,题目用例没有作要求
'''

import re

def validate_hidden(res_ht):
    binary_ls = []
    for hidden_code in res_ht:
        binary = bin(int(hidden_code)).replace('0b','')
        if len(binary) < 8:
            binary += '0'*(8-len(binary)) + binary
            binary_ls.append(binary)
        else:
            binary_ls.append(binary)
    hidden_str = ''.join(binary_ls)
    if '01' in hidden_str:
        return False
            
    elif  '1' not in hidden_str or '0' not in hidden_str:
        return False
    return True

count_dic = {
    'A':0,
    'B':0,
    'C':0,
    'D':0,
    'E':0,
    'error':0,
    'private':0
}
while True:
    try:
        ip,hidden = input().split('~')
        res_hid = re.match('^(\d+).(\d+).(\d+).(\d+)$',hidden)
        res = re.match('^(\d+).(\d+).(\d+).(\d+)$',ip)
        if res_hid and res:
            pass
        else:
            count_dic['error'] += 1
            break
        
        res_t = res.groups()
        res_ht = res_hid.groups()

        hidden_result = validate_hidden(res_ht)
        if  1<=int(res_t[0])<=126:
            if hidden_result:
                if res_t[0] == '10':
                    count_dic['private'] += 1
                count_dic['A'] += 1
            else:
                count_dic['error'] += 1
        elif 128<=int(res_t[0])<=191:
            if hidden_result:
                if res_t[0] == '172' and 16<=int(res_t[1])<=31:
                    count_dic['private'] += 1
                count_dic['B'] += 1
            else:
                count_dic['error'] += 1
        elif 192<=int(res_t[0])<=223:
            if hidden_result:
                if res_t[0] == '192' and res_t[1]=='168':
                    count_dic['private'] += 1
                count_dic['C'] += 1
            else:
                count_dic['error'] += 1
        elif 224<=int(res_t[0])<=239:
            if hidden_result:
                count_dic['D'] += 1
            else:
                count_dic['error'] += 1
        elif 240<=int(res_t[0])<=255:
            if hidden_result:
                count_dic['E'] += 1
            else:
                count_dic['error'] += 1
        elif res_t[0]=='0' or res_t[0]=='127':
            continue
        else:
            count_dic['error'] += 1
       
    except:
        break

for value in count_dic.values():
    print(value,end=' ')
    
全部评论

相关推荐

昨天 18:50
已编辑
长沙学院 Java
个人背景:学院二本计科专业&nbsp;大二开始实习个人经历:安克创新&nbsp;、理想汽车、字节跳动碎碎念:我做事只有三分钟热度。看到进了大厂的同学,我会羡慕,也会跟着努力上进;但遇到好看的小说,我又会放下手头的事沉迷其中,之前的坚持也就中断了。我有些自卑,总觉得自己学历和外貌都不够好。之前偶然在网上受到关注,我就喜欢上了上网,因为这里有很多人认可我。但我也很在意别人的评价,偶尔看到嘲讽的言论,会触发我的自卑情绪,让我感到愤怒。有时候我会强硬地回怼,有时候又会懦弱地选择无视。我也有虚荣心。不管是拿到安克、理想还是字节的机会,我在分享的时候都会带着这份心思。我会特意强调自己学历不好,是为了衬托出过程的艰难,以此显得自己更厉害。我知道,人往往会炫耀自己缺少的东西,来掩盖内心的空洞。我总想着走捷径,不太喜欢踏踏实实地做事。找实习的时候,我花了更多时间在研究面试技巧上,而不是提升专业能力。我会反复听面试录音分析技巧,看面试教程学习怎么和不同的面试官沟通,还会每天自言自语练习语言表达,同学都觉得我有点奇怪。我的实习生涯里,侥幸和运气占了很大一部分。我总在想,如果有一天我失去了这份幸运,这些特质可能会让我一蹶不振。ps:&nbsp;很多人会问我学习路线和经验&nbsp;但是就像我上面说的&nbsp;我的实习过程靠的很多是关键节点的运气&nbsp;技术上面我可能不如很多人&nbsp;&nbsp;所以请大家理性求助和理性参考我的回答&nbsp;附上我的投递记录
我的offer在哪里...:从去年看到现在,飞升哥就是榜样
我的求职进度条
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务