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

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

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

import sys

def to_bin_8(in_dec):
    out_bin = str(in_dec % 2)
    for i in range(7):
        in_dec = int(in_dec / 2)
        out_bin = str(in_dec % 2) + out_bin
    return out_bin

def to_dec(in_bin):
    i = 1
    out_dec = 0
    for j in range(len(in_bin) - 1, -1, -1):
        out_dec += int(in_bin[j]) * i
        i *= 2
    return out_dec

def addr_to_num(in_addr):
    bin_addr = ""
    for part in in_addr.split("."):
        bin_addr += to_bin_8(int(part))
    return to_dec(bin_addr)

a = 0
b = 0
c = 0
d = 0
e = 0
invalid = 0
private = 0

private_bounds = [
    (addr_to_num("10.0.0.0"), addr_to_num("10.255.255.255")),
    (addr_to_num("172.16.0.0"), addr_to_num("172.31.255.255")),
    (addr_to_num("192.168.0.0"), addr_to_num("192.168.255.255"))
]
a_bounds = (addr_to_num("1.0.0.0"), addr_to_num("126.255.255.255"))
b_bounds = (addr_to_num("128.0.0.0"), addr_to_num("191.255.255.255"))
c_bounds = (addr_to_num("192.0.0.0"), addr_to_num("223.255.255.255"))
d_bounds = (addr_to_num("224.0.0.0"), addr_to_num("239.255.255.255"))
e_bounds = (addr_to_num("240.0.0.0"), addr_to_num("255.255.255.255"))
for line in sys.stdin:
    addr, mask = line.split("~")
    try:
        pass_this = False
        if addr.split(".")[0] == "0" or addr.split(".")[0] == "127":
            continue

        bin_mask = ""
        for part in mask.split("."):
            bin_mask += to_bin_8(int(part))
        if bin_mask[0] == "0" or bin_mask[-1] == "1":
            invalid += 1
            continue
        allow_one = True
        for i in bin_mask:
            if i == "0" and allow_one:
                allow_one = False
            if i == "1" and not allow_one:
                pass_this = True
                break
        if pass_this:
            invalid += 1
            continue

        num_mask = to_dec(bin_mask)
        num_addr = addr_to_num(addr)
        masked_addr = num_addr & num_mask
        if (masked_addr >= private_bounds[0][0] and masked_addr <= private_bounds[0][1]) \
            or (masked_addr >= private_bounds[1][0] and masked_addr <= private_bounds[1][1]) \
            or (masked_addr >= private_bounds[2][0] and masked_addr <= private_bounds[2][1]):
            private += 1
        
        if masked_addr >= a_bounds[0] and masked_addr <= a_bounds[1]:
            a += 1
        elif masked_addr >= b_bounds[0] and masked_addr <= b_bounds[1]:
            b += 1
        elif masked_addr >= c_bounds[0] and masked_addr <= c_bounds[1]:
            c += 1
        elif masked_addr >= d_bounds[0] and masked_addr <= d_bounds[1]:
            d += 1
        elif masked_addr >= e_bounds[0] and masked_addr <= e_bounds[1]:
            e += 1

        
        
    except:
        invalid += 1
        continue

print("{} {} {} {} {} {} {}".format(a, b, c, d, e, invalid, private))
    



全部评论

相关推荐

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

创作者周榜

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