题解 | #识别有效的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=' ')
    
全部评论

相关推荐

01-05 09:14
同济大学 Java
心碎一号线:我要是9✌🏻我就选保研,保研了大四再找实习,实习之后,如果觉得自己不适合互联网工作模式,还能有其他选择,如果实习后决定了走互联网,也能提升学历提高竞争力
点赞 评论 收藏
分享
2025-12-31 19:23
已编辑
门头沟学院 Java
ssob是已读不回的,字节是压根不敢投的,简历是反反复复改了N遍的,八股是永远背不完的😅😅😅扯远了,道心破碎了,把简历发出来让大伙先看看笑话。再说正事。寒假日常实习还是很难找,连个面试都难约,我不是个例,这是网上普遍反映。不报希望了,趁着2、3月前赶紧做些什么才是。扔几个碎碎念:1.这破简历还能怎么改?写到什么程度才能过实习岗筛选?广大牛友来锐评一下2.火速辅修go,是否可行目前看来是学习成本最小的。首先,很多go实习岗位已经明确要求掌握gin等技术栈,拿java简历投go的时代已经过去了。其次,很多后端的东西,MySQL、Redis这些都是通用的,不用重新学。所以这个问题就具体为:2.1&nbsp;java&amp;go混血简历怎么写第一个项目,仿大麦的微服务,不太好改。因为有用到Redisson、AOP、SpringAI这些java强相关的东西,包装成go需要替换这些方案。第二个,点评魔改。应该可以包装成go,github上也有人用go重写过。2.2&nbsp;java&amp;go通用的轮子RPC直接pass了,太烂大街了。不知道动态线程池能不能做。反正项目上新有风险,不一定来得及,非必要就不开新的项目。补充:别跟我扯RAG了,这玩意已经成新的烂大街了,详见我上一篇的吐槽。3.认真学微调prompt什么的这个半步踩进算法了已经。八股和场景题完全就是另一套,没两三个月搞不定的。约等于换方向
简历中的项目经历要怎么写
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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