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

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

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

我的思路:

# 1、先判断IP地址合法,再判断掩码合法,对错误IP或掩码计数

# 2、计数可以采用一个类属性

# 3、判断IP合法的方式:点分十进制转二进制,然后判断是否在A/B/C/D/E类地址里面,判断私有IP地址类似

# 4、判断掩码合法的方式:点分十进制转二进制,然后判断是否开头为连续的1(至少有一个1)后续均为0(至少有一个0)

以下测试用例中应该有14个非法掩码,给的标答是13个,手工确认了2遍仍然是14个不知道有没有大佬能帮忙解答下

225.240.129.203~255.110.255.255 1

183.181.49.4~255.0.0.0

172.177.113.45~255.0.0.0

176.134.46.246~255.0.0.0

153.63.21.56~255.255.58.255 2

23.135.167.228~255.0.0.0

204.58.47.149~255.0.0.0

113.33.181.46~255.255.255.0

73.245.52.119~255.255.154.0 3

23.214.47.71~255.0.0.0

139.124.188.91~255.255.255.100 4

142.94.192.197~255.0.0.0

53.173.252.202~255.0.0.0

127.201.56.50~255.255.111.255 5

118.251.84.111~255.0.0.0

130.27.73.170~255.0.0.0

253.237.54.56~255.86.0.0 6

64.189.222.111~255.255.255.139 7

148.77.44.147~255.0.0.0

59.213.5.253~255.255.0.0

3.52.119.131~255.255.0.0

213.208.164.145~255.255.0.0

24.22.21.206~255.255.90.255 8

89.43.34.31~255.0.0.0

9.64.214.75~255.0.0.0

110.156.20.173~255.153.0.0 9

71.183.242.53~255.255.0.0

119.152.129.100~255.0.0.0

38.187.119.201~255.0.0.0

73.81.221.180~255.255.255.255 10

73.198.13.199~255.0.15.0 11

99.42.142.145~255.255.255.0

196.121.115.160~255.0.0.0

226.30.29.206~255.0.0.0

244.248.31.171~255.255.255.255 12

59.116.159.246~255.0.0.0

121.124.37.157~255.0.0.226 13

103.42.94.71~255.255.0.0

125.88.217.249~255.255.74.255 14

73.44.250.101~255.255.255.0

class Counter:

    ip_A_class = 0

    ip_B_class = 0

    ip_C_class = 0

    ip_D_class = 0

    ip_E_class = 0

    error_ip_or_mask = 0

    private_ip = 0

def point_seq_deci_to_bin(ip_or_mask:str):

    tmp = ip_or_mask.split(".")

    result = ""

    # print(tmp)

    if len(tmp) != 4:

        return None

    for num_str in tmp:

        if not num_str:

            return None

        if num_str.startswith('0') and len(num_str) > 1:

            return None

        if eval(num_str) > 255 or eval(num_str) < 0 :

            return None

        # print(bin(eval(num_str)))

        result += '{:0>8s}'.format(bin(eval(num_str))[2:])

    # print(result)

    return result

point_seq_deci_to_bin('10.70.44.68')

def is_A_class_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('1.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('126.255.255.255') ):

        return True

    return False

def is_B_class_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('128.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('191.255.255.255') ):

        return True

    return False

def is_C_class_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('192.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('223.255.255.255') ):

        return True

    return False

def is_D_class_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('224.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('239.255.255.255') ):

        return True

    return False

def is_E_class_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('240.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('255.255.255.255') ):

        return True

    return False

def is_private_ip(bin_ip):

    if not bin_ip:

        return False

    if (bin_ip >= point_seq_deci_to_bin('10.0.0.0') )and (bin_ip <= point_seq_deci_to_bin('10.255.255.255') ):

        return True

    if (bin_ip >= point_seq_deci_to_bin('172.16.0.0') )and (bin_ip <= point_seq_deci_to_bin('172.31.255.255') ):

        return True

    if (bin_ip >= point_seq_deci_to_bin('192.168.0.0') )and (bin_ip <= point_seq_deci_to_bin('192.168.255.255') ):

        return True

    return False

def is_special_ip(bin_ip:str):

    if bin_ip[:8] in ('0'*8, '{:0>8s}'.format(bin(127)[2:])):

        return True

def is_legal_mask(bin_mask:str):

    if not bin_mask:

        return False

    ch_set = set(bin_mask)

    if (ch_set == {'0'}) or (ch_set == {'1'}):

        return False

    zero_start = False

    for ch in bin_mask:

        if zero_start and ch == '1':

            return False

        if ch == '0':

            zero_start = True

    return True

# print(is_legal_mask('0000000'))

# print(is_private_ip(point_seq_deci_to_bin('192.168.255.255')))

# print(is_special_ip(point_seq_deci_to_bin('0.2.0.0')))

import sys

lines = sys.stdin.read().split("\n")

lines = lines[:-1]

for line in lines:

    ip, mask = line.split("~")

    bin_ip = point_seq_deci_to_bin(ip)

    bin_mask = point_seq_deci_to_bin(mask)

    if bin_mask:

        if not is_legal_mask(bin_mask):

            Counter.error_ip_or_mask += 1

            continue

    else:

        Counter.error_ip_or_mask += 1

        continue

    if bin_ip:

        if is_A_class_ip(bin_ip):

            Counter.ip_A_class += 1

        elif is_B_class_ip(bin_ip):

            Counter.ip_B_class += 1

        elif is_C_class_ip(bin_ip):

            Counter.ip_C_class += 1

        elif is_D_class_ip(bin_ip):

            Counter.ip_D_class += 1

        elif is_E_class_ip(bin_ip):

            Counter.ip_E_class += 1

        elif is_special_ip(bin_ip):

            pass

        else:

            Counter.error_ip_or_mask += 1

            continue

        if is_private_ip(bin_ip):

            Counter.private_ip += 1

    else:

        Counter.error_ip_or_mask += 1

print(Counter.ip_A_class, Counter.ip_B_class, Counter.ip_C_class, Counter.ip_D_class,

      Counter.ip_E_class, Counter.error_ip_or_mask, Counter.private_ip)

全部评论

相关推荐

点赞 评论 收藏
分享
07-18 13:49
门头沟学院 Java
26小林不会梦到感谢...:这个点还在面暑期嘛不是马上开秋招了
点赞 评论 收藏
分享
08-29 10:07
已编辑
门头沟学院 嵌入式工程师
是海笔吗
鼠鼠能上岸吗:有没有可能这是美的(
投递美团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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